Skip to content

Commit

Permalink
rate: restore Go 1.6 support
Browse files Browse the repository at this point in the history
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 <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Chris Broadfoot <cbro@golang.org>
  • Loading branch information
bradfitz committed Apr 24, 2017
1 parent c06e80d commit 8be79e1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
16 changes: 13 additions & 3 deletions rate/rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package rate

import (
"context"
"fmt"
"math"
"sync"
Expand Down Expand Up @@ -213,16 +212,27 @@ func (lim *Limiter) ReserveN(now time.Time, n int) *Reservation {
return &r
}

// contextContext is a temporary(?) copy of the context.Context type
// to support both Go 1.6 using golang.org/x/net/context and Go 1.7+
// with the built-in context package. If people ever stop using Go 1.6
// we can remove this.
type contextContext interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key interface{}) interface{}
}

// Wait is shorthand for WaitN(ctx, 1).
func (lim *Limiter) Wait(ctx context.Context) (err error) {
func (lim *Limiter) wait(ctx contextContext) (err error) {
return lim.WaitN(ctx, 1)
}

// WaitN blocks until lim permits n events to happen.
// It returns an error if n exceeds the Limiter's burst size, the Context is
// canceled, or the expected wait time exceeds the Context's Deadline.
// The burst limit is ignored if the rate limit is Inf.
func (lim *Limiter) WaitN(ctx context.Context, n int) (err error) {
func (lim *Limiter) waitN(ctx contextContext, n int) (err error) {
if n > lim.burst && lim.limit != Inf {
return fmt.Errorf("rate: Wait(n=%d) exceeds limiter's burst %d", n, lim.burst)
}
Expand Down
21 changes: 21 additions & 0 deletions rate/rate_go16.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !go1.7

package rate

import "golang.org/x/net/context"

// Wait is shorthand for WaitN(ctx, 1).
func (lim *Limiter) Wait(ctx context.Context) (err error) {
return lim.waitN(ctx, 1)
}

// WaitN blocks until lim permits n events to happen.
// It returns an error if n exceeds the Limiter's burst size, the Context is
// canceled, or the expected wait time exceeds the Context's Deadline.
func (lim *Limiter) WaitN(ctx context.Context, n int) (err error) {
return lim.waitN(ctx, n)
}
21 changes: 21 additions & 0 deletions rate/rate_go17.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build go1.7

package rate

import "context"

// Wait is shorthand for WaitN(ctx, 1).
func (lim *Limiter) Wait(ctx context.Context) (err error) {
return lim.waitN(ctx, 1)
}

// WaitN blocks until lim permits n events to happen.
// It returns an error if n exceeds the Limiter's burst size, the Context is
// canceled, or the expected wait time exceeds the Context's Deadline.
func (lim *Limiter) WaitN(ctx context.Context, n int) (err error) {
return lim.waitN(ctx, n)
}
5 changes: 5 additions & 0 deletions rate/rate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build go1.7

package rate

import (
Expand Down Expand Up @@ -163,6 +165,9 @@ func TestSimultaneousRequests(t *testing.T) {
}

func TestLongRunningQPS(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
if runtime.GOOS == "openbsd" {
t.Skip("low resolution time.Sleep invalidates test (golang.org/issue/14183)")
return
Expand Down

0 comments on commit 8be79e1

Please sign in to comment.