mirror of
https://github.com/mailgun/groupcache.git
synced 2024-07-01 02:19:12 +00:00
Now using context.Context instead of groupcache.Context
This commit is contained in:
parent
a5eee224aa
commit
f352930de5
|
@ -25,6 +25,7 @@ limitations under the License.
|
|||
package groupcache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
|
@ -45,13 +46,13 @@ type Getter interface {
|
|||
// uniquely describe the loaded data, without an implicit
|
||||
// current time, and without relying on cache expiration
|
||||
// mechanisms.
|
||||
Get(ctx Context, key string, dest Sink) error
|
||||
Get(ctx context.Context, key string, dest Sink) error
|
||||
}
|
||||
|
||||
// A GetterFunc implements Getter with a function.
|
||||
type GetterFunc func(ctx Context, key string, dest Sink) error
|
||||
type GetterFunc func(ctx context.Context, key string, dest Sink) error
|
||||
|
||||
func (f GetterFunc) Get(ctx Context, key string, dest Sink) error {
|
||||
func (f GetterFunc) Get(ctx context.Context, key string, dest Sink) error {
|
||||
return f(ctx, key, dest)
|
||||
}
|
||||
|
||||
|
@ -210,7 +211,7 @@ func (g *Group) initPeers() {
|
|||
}
|
||||
}
|
||||
|
||||
func (g *Group) Get(ctx Context, key string, dest Sink) error {
|
||||
func (g *Group) Get(ctx context.Context, key string, dest Sink) error {
|
||||
g.peersOnce.Do(g.initPeers)
|
||||
g.Stats.Gets.Add(1)
|
||||
if dest == nil {
|
||||
|
@ -240,7 +241,7 @@ func (g *Group) Get(ctx Context, key string, dest Sink) error {
|
|||
|
||||
// Remove clears the key from our cache then forwards the remove
|
||||
// request to all peers.
|
||||
func (g *Group) Remove(ctx Context, key string) error {
|
||||
func (g *Group) Remove(ctx context.Context, key string) error {
|
||||
g.peersOnce.Do(g.initPeers)
|
||||
|
||||
_, err := g.removeGroup.Do(key, func() (interface{}, error) {
|
||||
|
@ -288,7 +289,7 @@ func (g *Group) Remove(ctx Context, key string) error {
|
|||
}
|
||||
|
||||
// load loads key either by invoking the getter locally or by sending it to another machine.
|
||||
func (g *Group) load(ctx Context, key string, dest Sink) (value ByteView, destPopulated bool, err error) {
|
||||
func (g *Group) load(ctx context.Context, key string, dest Sink) (value ByteView, destPopulated bool, err error) {
|
||||
g.Stats.Loads.Add(1)
|
||||
viewi, err := g.loadGroup.Do(key, func() (interface{}, error) {
|
||||
// Check the cache again because singleflight can only dedup calls
|
||||
|
@ -347,7 +348,7 @@ func (g *Group) load(ctx Context, key string, dest Sink) (value ByteView, destPo
|
|||
return
|
||||
}
|
||||
|
||||
func (g *Group) getLocally(ctx Context, key string, dest Sink) (ByteView, error) {
|
||||
func (g *Group) getLocally(ctx context.Context, key string, dest Sink) (ByteView, error) {
|
||||
err := g.getter.Get(ctx, key, dest)
|
||||
if err != nil {
|
||||
return ByteView{}, err
|
||||
|
@ -355,7 +356,7 @@ func (g *Group) getLocally(ctx Context, key string, dest Sink) (ByteView, error)
|
|||
return dest.view()
|
||||
}
|
||||
|
||||
func (g *Group) getFromPeer(ctx Context, peer ProtoGetter, key string) (ByteView, error) {
|
||||
func (g *Group) getFromPeer(ctx context.Context, peer ProtoGetter, key string) (ByteView, error) {
|
||||
req := &pb.GetRequest{
|
||||
Group: &g.name,
|
||||
Key: &key,
|
||||
|
@ -384,7 +385,7 @@ func (g *Group) getFromPeer(ctx Context, peer ProtoGetter, key string) (ByteView
|
|||
return value, nil
|
||||
}
|
||||
|
||||
func (g *Group) removeFromPeer(ctx Context, peer ProtoGetter, key string) error {
|
||||
func (g *Group) removeFromPeer(ctx context.Context, peer ProtoGetter, key string) error {
|
||||
req := &pb.GetRequest{
|
||||
Group: &g.name,
|
||||
Key: &key,
|
||||
|
|
|
@ -19,6 +19,7 @@ limitations under the License.
|
|||
package groupcache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
|
@ -41,7 +42,7 @@ var (
|
|||
|
||||
stringc = make(chan string)
|
||||
|
||||
dummyCtx Context
|
||||
dummyCtx context.Context
|
||||
|
||||
// cacheFills is the number of times stringGroup or
|
||||
// protoGroup's Getter have been called. Read using the
|
||||
|
@ -59,7 +60,7 @@ const (
|
|||
)
|
||||
|
||||
func testSetup() {
|
||||
stringGroup = NewGroup(stringGroupName, cacheSize, GetterFunc(func(_ Context, key string, dest Sink) error {
|
||||
stringGroup = NewGroup(stringGroupName, cacheSize, GetterFunc(func(_ context.Context, key string, dest Sink) error {
|
||||
if key == fromChan {
|
||||
key = <-stringc
|
||||
}
|
||||
|
@ -67,7 +68,7 @@ func testSetup() {
|
|||
return dest.SetString("ECHO:"+key, time.Time{})
|
||||
}))
|
||||
|
||||
protoGroup = NewGroup(protoGroupName, cacheSize, GetterFunc(func(_ Context, key string, dest Sink) error {
|
||||
protoGroup = NewGroup(protoGroupName, cacheSize, GetterFunc(func(_ context.Context, key string, dest Sink) error {
|
||||
if key == fromChan {
|
||||
key = <-stringc
|
||||
}
|
||||
|
@ -78,7 +79,7 @@ func testSetup() {
|
|||
}, time.Time{})
|
||||
}))
|
||||
|
||||
expireGroup = NewGroup(expireGroupName, cacheSize, GetterFunc(func(_ Context, key string, dest Sink) error {
|
||||
expireGroup = NewGroup(expireGroupName, cacheSize, GetterFunc(func(_ context.Context, key string, dest Sink) error {
|
||||
cacheFills.Add(1)
|
||||
return dest.SetString("ECHO:"+key, time.Now().Add(time.Millisecond*100))
|
||||
}))
|
||||
|
@ -254,7 +255,7 @@ type fakePeer struct {
|
|||
fail bool
|
||||
}
|
||||
|
||||
func (p *fakePeer) Get(_ Context, in *pb.GetRequest, out *pb.GetResponse) error {
|
||||
func (p *fakePeer) Get(_ context.Context, in *pb.GetRequest, out *pb.GetResponse) error {
|
||||
p.hits++
|
||||
if p.fail {
|
||||
return errors.New("simulated error from peer")
|
||||
|
@ -263,7 +264,7 @@ func (p *fakePeer) Get(_ Context, in *pb.GetRequest, out *pb.GetResponse) error
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *fakePeer) Remove(_ Context, in *pb.GetRequest) error {
|
||||
func (p *fakePeer) Remove(_ context.Context, in *pb.GetRequest) error {
|
||||
p.hits++
|
||||
if p.fail {
|
||||
return errors.New("simulated error from peer")
|
||||
|
@ -295,7 +296,7 @@ func TestPeers(t *testing.T) {
|
|||
peerList := fakePeers([]ProtoGetter{peer0, peer1, peer2, nil})
|
||||
const cacheSize = 0 // disabled
|
||||
localHits := 0
|
||||
getter := func(_ Context, key string, dest Sink) error {
|
||||
getter := func(_ context.Context, key string, dest Sink) error {
|
||||
localHits++
|
||||
return dest.SetString("got:"+key, time.Time{})
|
||||
}
|
||||
|
@ -427,7 +428,7 @@ func (g *orderedFlightGroup) Lock(fn func()) {
|
|||
func TestNoDedup(t *testing.T) {
|
||||
const testkey = "testkey"
|
||||
const testval = "testval"
|
||||
g := newGroup("testgroup", 1024, GetterFunc(func(_ Context, key string, dest Sink) error {
|
||||
g := newGroup("testgroup", 1024, GetterFunc(func(_ context.Context, key string, dest Sink) error {
|
||||
return dest.SetString(testval, time.Time{})
|
||||
}), nil)
|
||||
|
||||
|
|
20
http.go
20
http.go
|
@ -41,12 +41,12 @@ type HTTPPool struct {
|
|||
// Context optionally specifies a context for the server to use when it
|
||||
// receives a request.
|
||||
// If nil, the server uses a nil Context.
|
||||
Context func(*http.Request) Context
|
||||
Context func(*http.Request) context.Context
|
||||
|
||||
// Transport optionally specifies an http.RoundTripper for the client
|
||||
// to use when it makes a request.
|
||||
// If nil, the client uses http.DefaultTransport.
|
||||
Transport func(Context) http.RoundTripper
|
||||
Transport func(context.Context) http.RoundTripper
|
||||
|
||||
// this peer's base URL, e.g. "https://example.net:8000"
|
||||
self string
|
||||
|
@ -173,7 +173,7 @@ func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
http.Error(w, "no such group: "+groupName, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
var ctx Context
|
||||
var ctx context.Context
|
||||
if p.Context != nil {
|
||||
ctx = p.Context(r)
|
||||
}
|
||||
|
@ -216,7 +216,7 @@ func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
type httpGetter struct {
|
||||
transport func(Context) http.RoundTripper
|
||||
transport func(context.Context) http.RoundTripper
|
||||
baseURL string
|
||||
}
|
||||
|
||||
|
@ -224,7 +224,7 @@ var bufferPool = sync.Pool{
|
|||
New: func() interface{} { return new(bytes.Buffer) },
|
||||
}
|
||||
|
||||
func (h *httpGetter) makeRequest(ctx Context, method string, in *pb.GetRequest, out *http.Response) error {
|
||||
func (h *httpGetter) makeRequest(ctx context.Context, method string, in *pb.GetRequest, out *http.Response) error {
|
||||
u := fmt.Sprintf(
|
||||
"%v%v/%v",
|
||||
h.baseURL,
|
||||
|
@ -236,10 +236,8 @@ func (h *httpGetter) makeRequest(ctx Context, method string, in *pb.GetRequest,
|
|||
return err
|
||||
}
|
||||
|
||||
// If user passed a standard context object, use it in the request.
|
||||
if stdCtx, ok := ctx.(context.Context); ok {
|
||||
req = req.WithContext(stdCtx)
|
||||
}
|
||||
// Pass along the context to the RoundTripper
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
tr := http.DefaultTransport
|
||||
if h.transport != nil {
|
||||
|
@ -253,7 +251,7 @@ func (h *httpGetter) makeRequest(ctx Context, method string, in *pb.GetRequest,
|
|||
return nil
|
||||
}
|
||||
|
||||
func (h *httpGetter) Get(ctx Context, in *pb.GetRequest, out *pb.GetResponse) error {
|
||||
func (h *httpGetter) Get(ctx context.Context, in *pb.GetRequest, out *pb.GetResponse) error {
|
||||
var res http.Response
|
||||
if err := h.makeRequest(ctx, http.MethodGet, in, &res); err != nil {
|
||||
return err
|
||||
|
@ -276,7 +274,7 @@ func (h *httpGetter) Get(ctx Context, in *pb.GetRequest, out *pb.GetResponse) er
|
|||
return nil
|
||||
}
|
||||
|
||||
func (h *httpGetter) Remove(ctx Context, in *pb.GetRequest) error {
|
||||
func (h *httpGetter) Remove(ctx context.Context, in *pb.GetRequest) error {
|
||||
var res http.Response
|
||||
if err := h.makeRequest(ctx, http.MethodDelete, in, &res); err != nil {
|
||||
return err
|
||||
|
|
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||
package groupcache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
|
@ -96,7 +97,7 @@ func TestHTTPPool(t *testing.T) {
|
|||
// Dummy getter function. Gets should go to children only.
|
||||
// The only time this process will handle a get is when the
|
||||
// children can't be contacted for some reason.
|
||||
getter := GetterFunc(func(ctx Context, key string, dest Sink) error {
|
||||
getter := GetterFunc(func(ctx context.Context, key string, dest Sink) error {
|
||||
return errors.New("parent getter called; something's wrong")
|
||||
})
|
||||
g := NewGroup("httpPoolTest", 1<<20, getter)
|
||||
|
@ -162,7 +163,7 @@ func beChildForTestHTTPPool(t *testing.T) {
|
|||
p := NewHTTPPool("http://" + addrs[*peerIndex])
|
||||
p.Set(addrToURL(addrs)...)
|
||||
|
||||
getter := GetterFunc(func(ctx Context, key string, dest Sink) error {
|
||||
getter := GetterFunc(func(ctx context.Context, key string, dest Sink) error {
|
||||
if _, err := http.Get(*serverAddr); err != nil {
|
||||
t.Logf("HTTP request from getter failed with '%s'", err)
|
||||
}
|
||||
|
|
10
peers.go
10
peers.go
|
@ -19,18 +19,14 @@ limitations under the License.
|
|||
package groupcache
|
||||
|
||||
import (
|
||||
"context"
|
||||
pb "github.com/mailgun/groupcache/groupcachepb"
|
||||
)
|
||||
|
||||
// Context is an opaque value passed through calls to the
|
||||
// ProtoGetter. It may be nil if your ProtoGetter implementation does
|
||||
// not require a context.
|
||||
type Context interface{}
|
||||
|
||||
// ProtoGetter is the interface that must be implemented by a peer.
|
||||
type ProtoGetter interface {
|
||||
Get(context Context, in *pb.GetRequest, out *pb.GetResponse) error
|
||||
Remove(context Context, in *pb.GetRequest) error
|
||||
Get(context context.Context, in *pb.GetRequest, out *pb.GetResponse) error
|
||||
Remove(context context.Context, in *pb.GetRequest) error
|
||||
}
|
||||
|
||||
// PeerPicker is the interface that must be implemented to locate
|
||||
|
|
Loading…
Reference in New Issue
Block a user