transport

package
v3.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2024 License: Apache-2.0 Imports: 18 Imported by: 2

Documentation

Index

Constants

View Source
const (
	DefaultBasePath = "/_groupcache/"
)

Variables

This section is empty.

Functions

func SetSinkView

func SetSinkView(s Sink, v ByteView) error

Types

type ByteView

type ByteView struct {
	// contains filtered or unexported fields
}

A ByteView holds an immutable view of bytes. Internally it wraps either a []byte or a string, but that detail is invisible to callers.

A ByteView is meant to be used as a value type, not a pointer (like a time.Time).

func ByteViewFrom

func ByteViewFrom(x any) ByteView

func ByteViewWithExpire

func ByteViewWithExpire(b []byte, expire time.Time) ByteView

func (ByteView) At

func (v ByteView) At(i int) byte

At returns the byte at index i.

func (ByteView) ByteSlice

func (v ByteView) ByteSlice() []byte

ByteSlice returns a copy of the data as a byte slice.

func (ByteView) Copy

func (v ByteView) Copy(dest []byte) int

Copy copies b into dest and returns the number of bytes copied.

func (ByteView) Equal

func (v ByteView) Equal(b2 ByteView) bool

Equal returns whether the bytes in b are the same as the bytes in b2.

func (ByteView) EqualBytes

func (v ByteView) EqualBytes(b2 []byte) bool

EqualBytes returns whether the bytes in b are the same as the bytes in b2.

func (ByteView) EqualString

func (v ByteView) EqualString(s string) bool

EqualString returns whether the bytes in b are the same as the bytes in s.

func (ByteView) Expire

func (v ByteView) Expire() time.Time

Expire returns the expiration time associated with this view

func (ByteView) Len

func (v ByteView) Len() int

Len returns the view's length.

func (ByteView) ReadAt

func (v ByteView) ReadAt(p []byte, off int64) (n int, err error)

ReadAt implements io.ReaderAt on the bytes in v.

func (ByteView) Reader

func (v ByteView) Reader() io.ReadSeeker

Reader returns an io.ReadSeeker for the bytes in v.

func (ByteView) Slice

func (v ByteView) Slice(from, to int) ByteView

Slice slices the view between the provided from and to indices.

func (ByteView) SliceFrom

func (v ByteView) SliceFrom(from int) ByteView

SliceFrom slices the view from the provided index until the end.

func (ByteView) String

func (v ByteView) String() string

String returns the data as a string, making a copy if necessary.

func (ByteView) WriteTo

func (v ByteView) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements io.WriterTo on the bytes in v.

type ErrNotFound

type ErrNotFound struct {
	Msg string
}

ErrNotFound should be returned from an implementation of `GetterFunc` to indicate the requested value is not available. When remote HTTP calls are made to retrieve values from other groupcache instances, returning this error will indicate to groupcache that the value requested is not available, and it should NOT attempt to call `GetterFunc` locally.

func (*ErrNotFound) Error

func (e *ErrNotFound) Error() string

func (*ErrNotFound) Is

func (e *ErrNotFound) Is(target error) bool

type ErrRemoteCall

type ErrRemoteCall struct {
	Msg string
}

ErrRemoteCall is returned from `group.Get()` when an error that is not a `ErrNotFound` is returned during a remote HTTP instance call

func (*ErrRemoteCall) Error

func (e *ErrRemoteCall) Error() string

func (*ErrRemoteCall) Is

func (e *ErrRemoteCall) Is(target error) bool

type Group

type Group interface {
	Set(context.Context, string, []byte, time.Time, bool) error
	Get(context.Context, string, Sink) error
	Remove(context.Context, string) error
	UsedBytes() (int64, int64)
	Name() string
}

type GroupCacheInstance

type GroupCacheInstance interface {
	GetGroup(string) Group
}

type HttpClient

type HttpClient struct {
	// contains filtered or unexported fields
}

HttpClient represents an HTTP client used to make requests to a specific peer.

func (*HttpClient) Get

func (h *HttpClient) Get(ctx context.Context, in *pb.GetRequest, out *pb.GetResponse) error

func (*HttpClient) HashKey

func (h *HttpClient) HashKey() string

func (*HttpClient) PeerInfo

func (h *HttpClient) PeerInfo() peer.Info

func (*HttpClient) Remove

func (h *HttpClient) Remove(ctx context.Context, in *pb.GetRequest) error

func (*HttpClient) Set

func (h *HttpClient) Set(ctx context.Context, in *pb.SetRequest) error

type HttpTransport

type HttpTransport struct {
	// contains filtered or unexported fields
}

func NewHttpTransport

func NewHttpTransport(opts HttpTransportOptions) *HttpTransport

NewHttpTransport returns a new HttpTransport instance based on the provided HttpTransportOptions. Example usage:

	transport := groupcache.NewHttpTransport(groupcache.HttpTransportOptions{
	   BasePath: "/_groupcache/",
	   Scheme:   "http",
	   Client:   nil,
	})

 instance := groupcache.New(.....)

 // Must register the groupcache instance before using transport
 transport.Register(instance)

func (*HttpTransport) ListenAddress

func (t *HttpTransport) ListenAddress() string

ListenAddress returns the address the server is listening on after calling ListenAndServe().

func (*HttpTransport) ListenAndServe

func (t *HttpTransport) ListenAndServe(ctx context.Context, address string) error

ListenAndServe starts a new http server listening on the provided address:port

func (*HttpTransport) New

func (t *HttpTransport) New() Transport

New creates a new unregistered HttpTransport, using the same options as its parent.

func (*HttpTransport) NewClient

func (t *HttpTransport) NewClient(_ context.Context, p peer.Info) (peer.Client, error)

NewClient creates a new http client for the provided peer

func (*HttpTransport) Register

func (t *HttpTransport) Register(instance GroupCacheInstance)

Register registers the provided instance with this transport.

func (*HttpTransport) ServeHTTP

func (t *HttpTransport) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles all incoming HTTP requests received by the server spawned by ListenAndServe()

func (*HttpTransport) Shutdown

func (t *HttpTransport) Shutdown(ctx context.Context) error

Shutdown shuts down the server started when calling ListenAndServe()

type HttpTransportOptions

type HttpTransportOptions struct {
	// Context (Optional) specifies a context for the server to use when it
	// receives a request.
	// defaults to http.Request.Context()
	Context func(*http.Request) context.Context

	// Client (Optional) provide a custom http client with TLS config.
	// defaults to http.DefaultClient
	Client *http.Client

	// Scheme (Optional) is either `http` or `https`. Should always be 'http' as
	// 'https' is not currently supported. `Scheme` is reserved here for future use.
	// defaults to `http`
	Scheme string

	// BasePath (Optional) specifies the HTTP path that will serve groupcache requests.
	// defaults to "/_groupcache/".
	BasePath string

	// Logger
	Logger Logger
}

HttpTransportOptions options for creating a new HttpTransport

type Logger

type Logger interface {
	Info(msg string, args ...any)
	Error(msg string, args ...any)
}

type MockClient

type MockClient struct {
	// contains filtered or unexported fields
}

func (*MockClient) Get

func (c *MockClient) Get(ctx context.Context, in *pb.GetRequest, out *pb.GetResponse) error

func (*MockClient) HashKey

func (c *MockClient) HashKey() string

func (*MockClient) PeerInfo

func (c *MockClient) PeerInfo() peer.Info

func (*MockClient) Remove

func (c *MockClient) Remove(ctx context.Context, in *pb.GetRequest) error

func (*MockClient) Set

func (c *MockClient) Set(ctx context.Context, in *pb.SetRequest) error

type MockTransport

type MockTransport struct {
	// contains filtered or unexported fields
}

MockTransport is intended to be used as a singleton. Pass a new instance of this singleton into groupcache.New() by calling MockTransport.New(). You can then inspect the parent MockTransport for call statistics of the children in tests. This Transport is NOT THREAD SAFE Example usage:

t := NewMockTransport() i := groupcache.New(groupcache.Options{Transport: t.New()})

func NewMockTransport

func NewMockTransport() *MockTransport

func (*MockTransport) ListenAddress

func (t *MockTransport) ListenAddress() string

func (*MockTransport) ListenAndServe

func (t *MockTransport) ListenAndServe(_ context.Context, address string) error

func (*MockTransport) New

func (t *MockTransport) New() Transport

func (*MockTransport) NewClient

func (t *MockTransport) NewClient(ctx context.Context, peer peer.Info) (peer.Client, error)

func (*MockTransport) Register

func (t *MockTransport) Register(i GroupCacheInstance)

func (*MockTransport) Report

func (t *MockTransport) Report(method string) string

func (*MockTransport) Reset

func (t *MockTransport) Reset()

func (*MockTransport) Shutdown

func (t *MockTransport) Shutdown(_ context.Context) error

type Sink

type Sink interface {
	// SetString sets the value to s.
	SetString(s string, e time.Time) error

	// SetBytes sets the value to the contents of v.
	// The caller retains ownership of v.
	SetBytes(v []byte, e time.Time) error

	// SetProto sets the value to the encoded version of m.
	// The caller retains ownership of m.
	SetProto(m proto.Message, e time.Time) error

	// View returns a frozen view of the bytes for caching.
	View() (ByteView, error)
}

A Sink receives data from a Get call.

Implementation of Getter must call exactly one of the Set methods on success.

`e` sets an optional time in the future when the value will expire. If you don't want expiration, pass the zero value for `time.Time` (for instance, `time.Time{}`).

func AllocatingByteSliceSink

func AllocatingByteSliceSink(dst *[]byte) Sink

AllocatingByteSliceSink returns a Sink that allocates a byte slice to hold the received value and assigns it to *dst. The memory is not retained by groupcache.

func ByteViewSink

func ByteViewSink(dst *ByteView) Sink

ByteViewSink returns a Sink that populates a ByteView.

func ProtoSink

func ProtoSink(m proto.Message) Sink

ProtoSink returns a sink that unmarshals binary proto values into m.

func StringSink

func StringSink(sp *string) Sink

StringSink returns a Sink that populates the provided string pointer.

func TruncatingByteSliceSink

func TruncatingByteSliceSink(dst *[]byte) Sink

TruncatingByteSliceSink returns a Sink that writes up to len(*dst) bytes to *dst. If more bytes are available, they're silently truncated. If fewer bytes are available than len(*dst), *dst is shrunk to fit the number of bytes available.

type Transport

type Transport interface {
	// New returns a clone of this instance suitable for passing to groupcache.New()
	// Example usage:
	//
	// transport := groupcache.NewHttpTransport(groupcache.HttpTransportOptions{})
	// groupcache.New(groupcache.Config{Transport: transport.New()})
	New() Transport

	// Register registers the provided *Instance with the HttpTransport.
	//
	// This method sets the instance field of the HttpTransport to the provided instance.
	// The instance is used by the ServeHTTP method to serve groupcache requests.
	Register(instance GroupCacheInstance)

	// NewClient returns a new Client suitable for the transport implementation. The client returned is used to communicate
	// with a specific peer. This method will be called for each peer in the peer list when groupcache.Instance.SetPeers() is
	// called.
	NewClient(ctx context.Context, peer peer.Info) (peer.Client, error)

	// ListenAndServe spawns a server that will handle incoming requests for this transport
	// This is used by daemon and cluster packages to create a cluster of instances using
	// this specific transport.
	ListenAndServe(ctx context.Context, address string) error

	// Shutdown shuts down the server started when calling ListenAndServe()
	Shutdown(ctx context.Context) error

	// ListenAddress returns the address the server is listening on after calling ListenAndServe().
	ListenAddress() string
}

Directories

Path Synopsis
pb

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL