summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby DeSimone <desimone@users.noreply.github.com>2019-12-26 21:28:52 -0800
committerBrad Fitzpatrick <brad@danga.com>2019-12-26 21:28:52 -0800
commit215e87163ea771ffa998a96c611387313bb5a403 (patch)
tree5b88648cd906235edc7ad080bc900db53e0c2cd3
parent611e8accdfc92c4187d399e95ce826046d4c8d73 (diff)
downloadgolang-groupcache-215e87163ea771ffa998a96c611387313bb5a403.tar.gz
use std library context.Context (#131)
Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
-rw-r--r--groupcache.go15
-rw-r--r--groupcache_test.go13
-rw-r--r--http.go15
-rw-r--r--http_test.go5
-rw-r--r--peers.go9
5 files changed, 30 insertions, 27 deletions
diff --git a/groupcache.go b/groupcache.go
index 8cf714a..9e81087 100644
--- a/groupcache.go
+++ b/groupcache.go
@@ -25,6 +25,7 @@ limitations under the License.
package groupcache
import (
+ "context"
"errors"
"math/rand"
"strconv"
@@ -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)
}
@@ -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 {
@@ -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
@@ -292,7 +293,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
@@ -300,7 +301,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,
diff --git a/groupcache_test.go b/groupcache_test.go
index ffbd0b9..85bcc56 100644
--- a/groupcache_test.go
+++ b/groupcache_test.go
@@ -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.TODO()
// cacheFills is the number of times stringGroup or
// protoGroup's Getter have been called. Read using the
@@ -58,7 +59,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
}
@@ -66,7 +67,7 @@ func testSetup() {
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
}
@@ -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")
@@ -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)
}
@@ -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)
diff --git a/http.go b/http.go
index f37467a..56c9406 100644
--- a/http.go
+++ b/http.go
@@ -18,6 +18,7 @@ package groupcache
import (
"bytes"
+ "context"
"fmt"
"io"
"net/http"
@@ -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
@@ -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)
@@ -181,7 +184,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
}
@@ -189,7 +192,7 @@ 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,
diff --git a/http_test.go b/http_test.go
index b42edd7..e2c9093 100644
--- a/http_test.go
+++ b/http_test.go
@@ -17,6 +17,7 @@ limitations under the License.
package groupcache
import (
+ "context"
"errors"
"flag"
"log"
@@ -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)
@@ -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
})
diff --git a/peers.go b/peers.go
index 1625ff0..f42e907 100644
--- a/peers.go
+++ b/peers.go
@@ -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