mirror of
https://github.com/mailgun/groupcache.git
synced 2024-07-05 12:28:49 +00:00
Merge pull request #33 from fumin/master
allow all options of HTTPPool to be specified by users
This commit is contained in:
commit
d2a1805917
52
http.go
52
http.go
|
@ -29,10 +29,8 @@ import (
|
||||||
pb "github.com/golang/groupcache/groupcachepb"
|
pb "github.com/golang/groupcache/groupcachepb"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: make this configurable?
|
|
||||||
const defaultBasePath = "/_groupcache/"
|
const defaultBasePath = "/_groupcache/"
|
||||||
|
|
||||||
// TODO: make this configurable as well.
|
|
||||||
const defaultReplicas = 50
|
const defaultReplicas = 50
|
||||||
|
|
||||||
// HTTPPool implements PeerPicker for a pool of HTTP peers.
|
// HTTPPool implements PeerPicker for a pool of HTTP peers.
|
||||||
|
@ -57,21 +55,59 @@ type HTTPPool struct {
|
||||||
peers *consistenthash.Map
|
peers *consistenthash.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
var httpPoolMade bool
|
// HTTPPoolOptions are the configurations of a HTTPPool.
|
||||||
|
type HTTPPoolOptions struct {
|
||||||
|
// BasePath specifies the HTTP path that will serve groupcache requests.
|
||||||
|
// If blank, it defaults to "/_groupcache/".
|
||||||
|
BasePath string
|
||||||
|
|
||||||
// NewHTTPPool initializes an HTTP pool of peers.
|
// Replicas specifies the number of key replicas on the consistent hash.
|
||||||
// It registers itself as a PeerPicker and as an HTTP handler with the
|
// If blank, it defaults to 50.
|
||||||
// http.DefaultServeMux.
|
Replicas int
|
||||||
|
|
||||||
|
// HashFn specifies the hash function of the consistent hash.
|
||||||
|
// If blank, it defaults to crc32.ChecksumIEEE.
|
||||||
|
HashFn consistenthash.Hash
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHTTPPool initializes an HTTP pool of peers, and registers itself as a PeerPicker.
|
||||||
|
// For convenience, it also registers itself as an http.Handler with http.DefaultServeMux.
|
||||||
// The self argument be a valid base URL that points to the current server,
|
// The self argument be a valid base URL that points to the current server,
|
||||||
// for example "http://example.net:8000".
|
// for example "http://example.net:8000".
|
||||||
func NewHTTPPool(self string) *HTTPPool {
|
func NewHTTPPool(self string) *HTTPPool {
|
||||||
|
p := NewHTTPPoolOpts(self, nil)
|
||||||
|
http.Handle(p.basePath, p)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
var httpPoolMade bool
|
||||||
|
|
||||||
|
// NewHTTPPoolOpts initializes an HTTP pool of peers with the given options.
|
||||||
|
// Unlike NewHTTPPool, this function does not register the created pool as an HTTP handler.
|
||||||
|
// The returned *HTTPPool implements http.Handler and must be registered using http.Handle.
|
||||||
|
func NewHTTPPoolOpts(self string, o *HTTPPoolOptions) *HTTPPool {
|
||||||
if httpPoolMade {
|
if httpPoolMade {
|
||||||
panic("groupcache: NewHTTPPool must be called only once")
|
panic("groupcache: NewHTTPPool must be called only once")
|
||||||
}
|
}
|
||||||
httpPoolMade = true
|
httpPoolMade = true
|
||||||
p := &HTTPPool{basePath: defaultBasePath, self: self, peers: consistenthash.New(defaultReplicas, nil)}
|
|
||||||
|
opts := HTTPPoolOptions{}
|
||||||
|
if o != nil {
|
||||||
|
opts = *o
|
||||||
|
}
|
||||||
|
if opts.BasePath == "" {
|
||||||
|
opts.BasePath = defaultBasePath
|
||||||
|
}
|
||||||
|
if opts.Replicas == 0 {
|
||||||
|
opts.Replicas = defaultReplicas
|
||||||
|
}
|
||||||
|
|
||||||
|
p := &HTTPPool{
|
||||||
|
basePath: opts.BasePath,
|
||||||
|
self: self,
|
||||||
|
peers: consistenthash.New(opts.Replicas, opts.HashFn),
|
||||||
|
}
|
||||||
RegisterPeerPicker(func() PeerPicker { return p })
|
RegisterPeerPicker(func() PeerPicker { return p })
|
||||||
http.Handle(defaultBasePath, p)
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user