Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use standard library's context.Context #131

Merged
merged 1 commit into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions groupcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ limitations under the License.
package groupcache

import (
"context"
"errors"
"math/rand"
"strconv"
Expand All @@ -44,13 +45,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)
}

Expand Down Expand Up @@ -204,7 +205,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 {
Expand Down Expand Up @@ -233,7 +234,7 @@ func (g *Group) Get(ctx Context, key string, dest Sink) 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
Expand Down Expand Up @@ -292,15 +293,15 @@ 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
}
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,
Expand Down
13 changes: 7 additions & 6 deletions groupcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
package groupcache

import (
"context"
"errors"
"fmt"
"hash/crc32"
Expand All @@ -41,7 +42,7 @@ var (

stringc = make(chan string)

dummyCtx Context
dummyCtx = context.TODO()

// cacheFills is the number of times stringGroup or
// protoGroup's Getter have been called. Read using the
Expand All @@ -58,15 +59,15 @@ 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
}
cacheFills.Add(1)
return dest.SetString("ECHO:" + key)
}))

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
}
Expand Down Expand Up @@ -230,7 +231,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")
Expand Down Expand Up @@ -259,7 +260,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)
}
Expand Down Expand Up @@ -387,7 +388,7 @@ func (g *orderedFlightGroup) Do(key string, fn func() (interface{}, error)) (int
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)
}), nil)

Expand Down
15 changes: 9 additions & 6 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package groupcache

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
Expand All @@ -38,13 +39,13 @@ const defaultReplicas = 50
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
// If nil, the server uses the request's 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
Expand Down Expand Up @@ -157,9 +158,11 @@ 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)
} else {
ctx = r.Context()
}

group.Stats.ServerRequests.Add(1)
Expand All @@ -181,15 +184,15 @@ 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
}

var bufferPool = sync.Pool{
New: func() interface{} { return new(bytes.Buffer) },
}

func (h *httpGetter) Get(context Context, in *pb.GetRequest, out *pb.GetResponse) error {
func (h *httpGetter) Get(context context.Context, in *pb.GetRequest, out *pb.GetResponse) error {
u := fmt.Sprintf(
"%v%v/%v",
h.baseURL,
Expand Down
5 changes: 3 additions & 2 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package groupcache

import (
"context"
"errors"
"flag"
"log"
Expand Down Expand Up @@ -85,7 +86,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)
Expand Down Expand Up @@ -116,7 +117,7 @@ func beChildForTestHTTPPool() {
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 {
dest.SetString(strconv.Itoa(*peerIndex) + ":" + key)
return nil
})
Expand Down
9 changes: 3 additions & 6 deletions peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@ limitations under the License.
package groupcache

import (
"context"

pb "github.com/golang/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
Get(context context.Context, in *pb.GetRequest, out *pb.GetResponse) error
}

// PeerPicker is the interface that must be implemented to locate
Expand Down