Skip to content

Commit 8be79e1

Browse files
committed
rate: restore Go 1.6 support
https://golang.org/cl/41194 updatd the x/time/rate package to use the standard library's context package for golang/go#16745 but App Engine is stuck 14 months in the past and can only run Go 1.6, which lacks context. This CL restores Go 1.6 support. The Go build system stopped testing packages against Go 1.6 once Go 1.8 came out, so just disable the tests for Go 1.6 rather than jumping through hoops to make them work. Change-Id: I271dcd492dd0ca53961340d63b26facb5dbdf025 Reviewed-on: https://go-review.googlesource.com/41624 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Chris Broadfoot <[email protected]>
1 parent c06e80d commit 8be79e1

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

rate/rate.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package rate
77

88
import (
9-
"context"
109
"fmt"
1110
"math"
1211
"sync"
@@ -213,16 +212,27 @@ func (lim *Limiter) ReserveN(now time.Time, n int) *Reservation {
213212
return &r
214213
}
215214

215+
// contextContext is a temporary(?) copy of the context.Context type
216+
// to support both Go 1.6 using golang.org/x/net/context and Go 1.7+
217+
// with the built-in context package. If people ever stop using Go 1.6
218+
// we can remove this.
219+
type contextContext interface {
220+
Deadline() (deadline time.Time, ok bool)
221+
Done() <-chan struct{}
222+
Err() error
223+
Value(key interface{}) interface{}
224+
}
225+
216226
// Wait is shorthand for WaitN(ctx, 1).
217-
func (lim *Limiter) Wait(ctx context.Context) (err error) {
227+
func (lim *Limiter) wait(ctx contextContext) (err error) {
218228
return lim.WaitN(ctx, 1)
219229
}
220230

221231
// WaitN blocks until lim permits n events to happen.
222232
// It returns an error if n exceeds the Limiter's burst size, the Context is
223233
// canceled, or the expected wait time exceeds the Context's Deadline.
224234
// The burst limit is ignored if the rate limit is Inf.
225-
func (lim *Limiter) WaitN(ctx context.Context, n int) (err error) {
235+
func (lim *Limiter) waitN(ctx contextContext, n int) (err error) {
226236
if n > lim.burst && lim.limit != Inf {
227237
return fmt.Errorf("rate: Wait(n=%d) exceeds limiter's burst %d", n, lim.burst)
228238
}

rate/rate_go16.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !go1.7
6+
7+
package rate
8+
9+
import "golang.org/x/net/context"
10+
11+
// Wait is shorthand for WaitN(ctx, 1).
12+
func (lim *Limiter) Wait(ctx context.Context) (err error) {
13+
return lim.waitN(ctx, 1)
14+
}
15+
16+
// WaitN blocks until lim permits n events to happen.
17+
// It returns an error if n exceeds the Limiter's burst size, the Context is
18+
// canceled, or the expected wait time exceeds the Context's Deadline.
19+
func (lim *Limiter) WaitN(ctx context.Context, n int) (err error) {
20+
return lim.waitN(ctx, n)
21+
}

rate/rate_go17.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build go1.7
6+
7+
package rate
8+
9+
import "context"
10+
11+
// Wait is shorthand for WaitN(ctx, 1).
12+
func (lim *Limiter) Wait(ctx context.Context) (err error) {
13+
return lim.waitN(ctx, 1)
14+
}
15+
16+
// WaitN blocks until lim permits n events to happen.
17+
// It returns an error if n exceeds the Limiter's burst size, the Context is
18+
// canceled, or the expected wait time exceeds the Context's Deadline.
19+
func (lim *Limiter) WaitN(ctx context.Context, n int) (err error) {
20+
return lim.waitN(ctx, n)
21+
}

rate/rate_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// +build go1.7
6+
57
package rate
68

79
import (
@@ -163,6 +165,9 @@ func TestSimultaneousRequests(t *testing.T) {
163165
}
164166

165167
func TestLongRunningQPS(t *testing.T) {
168+
if testing.Short() {
169+
t.Skip("skipping in short mode")
170+
}
166171
if runtime.GOOS == "openbsd" {
167172
t.Skip("low resolution time.Sleep invalidates test (golang.org/issue/14183)")
168173
return

0 commit comments

Comments
 (0)