autocache

package module
v0.0.0-...-73ca9f7 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2023 License: Apache-2.0 Imports: 8 Imported by: 1

README

Build Status codecov Go Report Card GoDoc LICENSE pomerium chat

Autocache

Groupcache enhanced with memberlist for distributed peer discovery.

TL;DR

See /_example/ for usage.

Run

docker-compose -f _example/docker-compose.yaml up --scale autocache=5

Client
for i in`seq 10`; do curl "http://autocache.localhost/get/?key=hunter2";echo; done

$2a$14$1CCq.8WOxEmLY3jdkwZKIeR1bN/B0jnWwwSKc1VTf60A57VOXKblC
$2a$14$1CCq.8WOxEmLY3jdkwZKIeR1bN/B0jnWwwSKc1VTf60A57VOXKblC
$2a$14$1CCq.8WOxEmLY3jdkwZKIeR1bN/B0jnWwwSKc1VTf60A57VOXKblC
$2a$14$1CCq.8WOxEmLY3jdkwZKIeR1bN/B0jnWwwSKc1VTf60A57VOXKblC
$2a$14$1CCq.8WOxEmLY3jdkwZKIeR1bN/B0jnWwwSKc1VTf60A57VOXKblC
$2a$14$1CCq.8WOxEmLY3jdkwZKIeR1bN/B0jnWwwSKc1VTf60A57VOXKblC
$2a$14$1CCq.8WOxEmLY3jdkwZKIeR1bN/B0jnWwwSKc1VTf60A57VOXKblC
$2a$14$1CCq.8WOxEmLY3jdkwZKIeR1bN/B0jnWwwSKc1VTf60A57VOXKblC
$2a$14$1CCq.8WOxEmLY3jdkwZKIeR1bN/B0jnWwwSKc1VTf60A57VOXKblC
$2a$14$1CCq.8WOxEmLY3jdkwZKIeR1bN/B0jnWwwSKc1VTf60A57VOXKblC
Server
autocache_2  | 2020/01/06 06:10:51 bcryptKey/key:"hunter2"	time:969.8645ms
autocache_2  | 2020/01/06 06:10:51 cacheHandler: group[bcrypt]	key["hunter2"]	time[969.9474ms]
autocache_1  | 2020/01/06 06:10:51 cacheHandler: group[bcrypt]	key["hunter2"]	time[1.3559ms]
autocache_3  | 2020/01/06 06:10:51 cacheHandler: group[bcrypt]	key["hunter2"]	time[1.1236ms]
autocache_4  | 2020/01/06 06:10:51 cacheHandler: group[bcrypt]	key["hunter2"]	time[1.2935ms]
autocache_5  | 2020/01/06 06:10:51 cacheHandler: group[bcrypt]	key["hunter2"]	time[985.2µs]
autocache_6  | 2020/01/06 06:10:51 cacheHandler: group[bcrypt]	key["hunter2"]	time[1.2163ms]
autocache_2  | 2020/01/06 06:10:51 cacheHandler: group[bcrypt]	key["hunter2"]	time[23.3µs]
autocache_1  | 2020/01/06 06:10:51 cacheHandler: group[bcrypt]	key["hunter2"]	time[495.3µs]
autocache_3  | 2020/01/06 06:10:51 cacheHandler: group[bcrypt]	key["hunter2"]	time[497.3µs]
autocache_4  | 2020/01/06 06:10:52 cacheHandler: group[bcrypt]	key["hunter2"]	time[770.5µs]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Autocache

type Autocache struct {
	GroupcachePool *groupcache.HTTPPool
	Memberlist     *memberlist.Memberlist
	// contains filtered or unexported fields
}

Autocache implements automatic, distributed membership for a cluster of cache pool peers.

func New

func New(o *Options) (*Autocache, error)

New creates a new Autocache instance, setups memberlist, and invokes groupcache's peer pooling handlers. Note, by design a groupcache pool can only be made _once_.

func (*Autocache) Join

func (ac *Autocache) Join(existing []string) (int, error)

Join is used to take an existing Memberlist and attempt to join a cluster by contacting all the given hosts and performing a state sync. Initially, the Memberlist only contains our own state, so doing this will cause remote nodes to become aware of the existence of this node, effectively joining the cluster.

This returns the number of hosts successfully contacted and an error if none could be reached. If an error is returned, the node did not successfully join the cluster.

If an empty list is provided to this function, the memberlist will join itself.

func (*Autocache) NotifyJoin

func (ac *Autocache) NotifyJoin(node *memberlist.Node)

NotifyJoin is invoked when a node is detected to have joined. The Node argument must not be modified. Implements memberlist's EventDelegate's interface.

func (*Autocache) NotifyLeave

func (ac *Autocache) NotifyLeave(node *memberlist.Node)

NotifyLeave is invoked when a node is detected to have left. The Node argument must not be modified. Implements memberlist's EventDelegate's interface.

func (*Autocache) NotifyUpdate

func (ac *Autocache) NotifyUpdate(node *memberlist.Node)

NotifyUpdate is invoked when a node is detected to have updated, usually involving the meta data. The Node argument must not be modified. Implements memberlist EventDelegate's interface.

func (*Autocache) ServeHTTP

func (ac *Autocache) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Options

type Options struct {
	// Groupcache related
	//
	// Groupcache's pool is a HTTP handler. Scheme and port should be set
	// such that group cache's internal http client, used to fetch, distributed
	// keys, knows how to build the request URL.
	PoolOptions *groupcache.HTTPPoolOptions
	PoolScheme  string
	PoolPort    int
	// Transport optionally specifies an http.RoundTripper for the client
	// to use when it makes a request to another groupcache node.
	// If nil, the client uses http.DefaultTransport.
	PoolTransportFn func(context.Context) http.RoundTripper
	// Context optionally specifies a context for the server to use when it
	// receives a request.
	// If nil, the server uses the request's context
	PoolContext func(*http.Request) context.Context

	// Memberlist related
	//
	// MemberlistConfig ist he memberlist configuration to use.
	// If empty, `DefaultLANConfig` is used.
	MemberlistConfig *memberlist.Config

	// Logger is a custom logger which you provide.
	Logger *log.Logger
}

Options are the configurations of a Autocache.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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