Skip to content

Commit 215e871

Browse files
desimonebradfitz
authored andcommitted
use std library context.Context (#131)
Signed-off-by: Bobby DeSimone <[email protected]>
1 parent 611e8ac commit 215e871

File tree

5 files changed

+30
-27
lines changed

5 files changed

+30
-27
lines changed

groupcache.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ limitations under the License.
2525
package groupcache
2626

2727
import (
28+
"context"
2829
"errors"
2930
"math/rand"
3031
"strconv"
@@ -44,13 +45,13 @@ type Getter interface {
4445
// uniquely describe the loaded data, without an implicit
4546
// current time, and without relying on cache expiration
4647
// mechanisms.
47-
Get(ctx Context, key string, dest Sink) error
48+
Get(ctx context.Context, key string, dest Sink) error
4849
}
4950

5051
// A GetterFunc implements Getter with a function.
51-
type GetterFunc func(ctx Context, key string, dest Sink) error
52+
type GetterFunc func(ctx context.Context, key string, dest Sink) error
5253

53-
func (f GetterFunc) Get(ctx Context, key string, dest Sink) error {
54+
func (f GetterFunc) Get(ctx context.Context, key string, dest Sink) error {
5455
return f(ctx, key, dest)
5556
}
5657

@@ -204,7 +205,7 @@ func (g *Group) initPeers() {
204205
}
205206
}
206207

207-
func (g *Group) Get(ctx Context, key string, dest Sink) error {
208+
func (g *Group) Get(ctx context.Context, key string, dest Sink) error {
208209
g.peersOnce.Do(g.initPeers)
209210
g.Stats.Gets.Add(1)
210211
if dest == nil {
@@ -233,7 +234,7 @@ func (g *Group) Get(ctx Context, key string, dest Sink) error {
233234
}
234235

235236
// load loads key either by invoking the getter locally or by sending it to another machine.
236-
func (g *Group) load(ctx Context, key string, dest Sink) (value ByteView, destPopulated bool, err error) {
237+
func (g *Group) load(ctx context.Context, key string, dest Sink) (value ByteView, destPopulated bool, err error) {
237238
g.Stats.Loads.Add(1)
238239
viewi, err := g.loadGroup.Do(key, func() (interface{}, error) {
239240
// Check the cache again because singleflight can only dedup calls
@@ -292,15 +293,15 @@ func (g *Group) load(ctx Context, key string, dest Sink) (value ByteView, destPo
292293
return
293294
}
294295

295-
func (g *Group) getLocally(ctx Context, key string, dest Sink) (ByteView, error) {
296+
func (g *Group) getLocally(ctx context.Context, key string, dest Sink) (ByteView, error) {
296297
err := g.getter.Get(ctx, key, dest)
297298
if err != nil {
298299
return ByteView{}, err
299300
}
300301
return dest.view()
301302
}
302303

303-
func (g *Group) getFromPeer(ctx Context, peer ProtoGetter, key string) (ByteView, error) {
304+
func (g *Group) getFromPeer(ctx context.Context, peer ProtoGetter, key string) (ByteView, error) {
304305
req := &pb.GetRequest{
305306
Group: &g.name,
306307
Key: &key,

groupcache_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
package groupcache
2020

2121
import (
22+
"context"
2223
"errors"
2324
"fmt"
2425
"hash/crc32"
@@ -41,7 +42,7 @@ var (
4142

4243
stringc = make(chan string)
4344

44-
dummyCtx Context
45+
dummyCtx = context.TODO()
4546

4647
// cacheFills is the number of times stringGroup or
4748
// protoGroup's Getter have been called. Read using the
@@ -58,15 +59,15 @@ const (
5859
)
5960

6061
func testSetup() {
61-
stringGroup = NewGroup(stringGroupName, cacheSize, GetterFunc(func(_ Context, key string, dest Sink) error {
62+
stringGroup = NewGroup(stringGroupName, cacheSize, GetterFunc(func(_ context.Context, key string, dest Sink) error {
6263
if key == fromChan {
6364
key = <-stringc
6465
}
6566
cacheFills.Add(1)
6667
return dest.SetString("ECHO:" + key)
6768
}))
6869

69-
protoGroup = NewGroup(protoGroupName, cacheSize, GetterFunc(func(_ Context, key string, dest Sink) error {
70+
protoGroup = NewGroup(protoGroupName, cacheSize, GetterFunc(func(_ context.Context, key string, dest Sink) error {
7071
if key == fromChan {
7172
key = <-stringc
7273
}
@@ -230,7 +231,7 @@ type fakePeer struct {
230231
fail bool
231232
}
232233

233-
func (p *fakePeer) Get(_ Context, in *pb.GetRequest, out *pb.GetResponse) error {
234+
func (p *fakePeer) Get(_ context.Context, in *pb.GetRequest, out *pb.GetResponse) error {
234235
p.hits++
235236
if p.fail {
236237
return errors.New("simulated error from peer")
@@ -259,7 +260,7 @@ func TestPeers(t *testing.T) {
259260
peerList := fakePeers([]ProtoGetter{peer0, peer1, peer2, nil})
260261
const cacheSize = 0 // disabled
261262
localHits := 0
262-
getter := func(_ Context, key string, dest Sink) error {
263+
getter := func(_ context.Context, key string, dest Sink) error {
263264
localHits++
264265
return dest.SetString("got:" + key)
265266
}
@@ -387,7 +388,7 @@ func (g *orderedFlightGroup) Do(key string, fn func() (interface{}, error)) (int
387388
func TestNoDedup(t *testing.T) {
388389
const testkey = "testkey"
389390
const testval = "testval"
390-
g := newGroup("testgroup", 1024, GetterFunc(func(_ Context, key string, dest Sink) error {
391+
g := newGroup("testgroup", 1024, GetterFunc(func(_ context.Context, key string, dest Sink) error {
391392
return dest.SetString(testval)
392393
}), nil)
393394

http.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package groupcache
1818

1919
import (
2020
"bytes"
21+
"context"
2122
"fmt"
2223
"io"
2324
"net/http"
@@ -38,13 +39,13 @@ const defaultReplicas = 50
3839
type HTTPPool struct {
3940
// Context optionally specifies a context for the server to use when it
4041
// receives a request.
41-
// If nil, the server uses a nil Context.
42-
Context func(*http.Request) Context
42+
// If nil, the server uses the request's context
43+
Context func(*http.Request) context.Context
4344

4445
// Transport optionally specifies an http.RoundTripper for the client
4546
// to use when it makes a request.
4647
// If nil, the client uses http.DefaultTransport.
47-
Transport func(Context) http.RoundTripper
48+
Transport func(context.Context) http.RoundTripper
4849

4950
// this peer's base URL, e.g. "https://example.net:8000"
5051
self string
@@ -157,9 +158,11 @@ func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {
157158
http.Error(w, "no such group: "+groupName, http.StatusNotFound)
158159
return
159160
}
160-
var ctx Context
161+
var ctx context.Context
161162
if p.Context != nil {
162163
ctx = p.Context(r)
164+
} else {
165+
ctx = r.Context()
163166
}
164167

165168
group.Stats.ServerRequests.Add(1)
@@ -181,15 +184,15 @@ func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {
181184
}
182185

183186
type httpGetter struct {
184-
transport func(Context) http.RoundTripper
187+
transport func(context.Context) http.RoundTripper
185188
baseURL string
186189
}
187190

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

192-
func (h *httpGetter) Get(context Context, in *pb.GetRequest, out *pb.GetResponse) error {
195+
func (h *httpGetter) Get(context context.Context, in *pb.GetRequest, out *pb.GetResponse) error {
193196
u := fmt.Sprintf(
194197
"%v%v/%v",
195198
h.baseURL,

http_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package groupcache
1818

1919
import (
20+
"context"
2021
"errors"
2122
"flag"
2223
"log"
@@ -85,7 +86,7 @@ func TestHTTPPool(t *testing.T) {
8586
// Dummy getter function. Gets should go to children only.
8687
// The only time this process will handle a get is when the
8788
// children can't be contacted for some reason.
88-
getter := GetterFunc(func(ctx Context, key string, dest Sink) error {
89+
getter := GetterFunc(func(ctx context.Context, key string, dest Sink) error {
8990
return errors.New("parent getter called; something's wrong")
9091
})
9192
g := NewGroup("httpPoolTest", 1<<20, getter)
@@ -116,7 +117,7 @@ func beChildForTestHTTPPool() {
116117
p := NewHTTPPool("http://" + addrs[*peerIndex])
117118
p.Set(addrToURL(addrs)...)
118119

119-
getter := GetterFunc(func(ctx Context, key string, dest Sink) error {
120+
getter := GetterFunc(func(ctx context.Context, key string, dest Sink) error {
120121
dest.SetString(strconv.Itoa(*peerIndex) + ":" + key)
121122
return nil
122123
})

peers.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@ limitations under the License.
1919
package groupcache
2020

2121
import (
22+
"context"
23+
2224
pb "github.com/golang/groupcache/groupcachepb"
2325
)
2426

25-
// Context is an opaque value passed through calls to the
26-
// ProtoGetter. It may be nil if your ProtoGetter implementation does
27-
// not require a context.
28-
type Context interface{}
29-
3027
// ProtoGetter is the interface that must be implemented by a peer.
3128
type ProtoGetter interface {
32-
Get(context Context, in *pb.GetRequest, out *pb.GetResponse) error
29+
Get(context context.Context, in *pb.GetRequest, out *pb.GetResponse) error
3330
}
3431

3532
// PeerPicker is the interface that must be implemented to locate

0 commit comments

Comments
 (0)