vendor: golang.org/x/time v0.1.0

The golang.org/x/ projects are now doing tagged releases.

full diff: https://github.com/golang/time/compare/1f47c861a9ac...v0.1.0

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-11-16 15:53:35 +01:00
parent 7b3900145e
commit 57b6ed34cc
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
6 changed files with 94 additions and 56 deletions

View File

@ -70,7 +70,7 @@ require (
go.etcd.io/etcd/raft/v3 v3.5.2 // indirect go.etcd.io/etcd/raft/v3 v3.5.2 // indirect
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
golang.org/x/net v0.0.0-20220906165146-f3363e06e74c // indirect; updated for CVE-2022-27664 golang.org/x/net v0.0.0-20220906165146-f3363e06e74c // indirect; updated for CVE-2022-27664
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect golang.org/x/time v0.1.0 // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/grpc v1.45.0 // indirect google.golang.org/grpc v1.45.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect google.golang.org/protobuf v1.27.1 // indirect

View File

@ -585,8 +585,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs= golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA=
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

3
vendor/golang.org/x/time/AUTHORS generated vendored
View File

@ -1,3 +0,0 @@
# This source code refers to The Go Authors for copyright purposes.
# The master list of authors is in the main Go distribution,
# visible at http://tip.golang.org/AUTHORS.

View File

@ -1,3 +0,0 @@
# This source code was written by the Go contributors.
# The master list of contributors is in the main Go distribution,
# visible at http://tip.golang.org/CONTRIBUTORS.

136
vendor/golang.org/x/time/rate/rate.go generated vendored
View File

@ -80,6 +80,19 @@ func (lim *Limiter) Burst() int {
return lim.burst return lim.burst
} }
// TokensAt returns the number of tokens available at time t.
func (lim *Limiter) TokensAt(t time.Time) float64 {
lim.mu.Lock()
_, _, tokens := lim.advance(t) // does not mutute lim
lim.mu.Unlock()
return tokens
}
// Tokens returns the number of tokens available now.
func (lim *Limiter) Tokens() float64 {
return lim.TokensAt(time.Now())
}
// NewLimiter returns a new Limiter that allows events up to rate r and permits // NewLimiter returns a new Limiter that allows events up to rate r and permits
// bursts of at most b tokens. // bursts of at most b tokens.
func NewLimiter(r Limit, b int) *Limiter { func NewLimiter(r Limit, b int) *Limiter {
@ -89,16 +102,16 @@ func NewLimiter(r Limit, b int) *Limiter {
} }
} }
// Allow is shorthand for AllowN(time.Now(), 1). // Allow reports whether an event may happen now.
func (lim *Limiter) Allow() bool { func (lim *Limiter) Allow() bool {
return lim.AllowN(time.Now(), 1) return lim.AllowN(time.Now(), 1)
} }
// AllowN reports whether n events may happen at time now. // AllowN reports whether n events may happen at time t.
// Use this method if you intend to drop / skip events that exceed the rate limit. // Use this method if you intend to drop / skip events that exceed the rate limit.
// Otherwise use Reserve or Wait. // Otherwise use Reserve or Wait.
func (lim *Limiter) AllowN(now time.Time, n int) bool { func (lim *Limiter) AllowN(t time.Time, n int) bool {
return lim.reserveN(now, n, 0).ok return lim.reserveN(t, n, 0).ok
} }
// A Reservation holds information about events that are permitted by a Limiter to happen after a delay. // A Reservation holds information about events that are permitted by a Limiter to happen after a delay.
@ -125,17 +138,17 @@ func (r *Reservation) Delay() time.Duration {
} }
// InfDuration is the duration returned by Delay when a Reservation is not OK. // InfDuration is the duration returned by Delay when a Reservation is not OK.
const InfDuration = time.Duration(1<<63 - 1) const InfDuration = time.Duration(math.MaxInt64)
// DelayFrom returns the duration for which the reservation holder must wait // DelayFrom returns the duration for which the reservation holder must wait
// before taking the reserved action. Zero duration means act immediately. // before taking the reserved action. Zero duration means act immediately.
// InfDuration means the limiter cannot grant the tokens requested in this // InfDuration means the limiter cannot grant the tokens requested in this
// Reservation within the maximum wait time. // Reservation within the maximum wait time.
func (r *Reservation) DelayFrom(now time.Time) time.Duration { func (r *Reservation) DelayFrom(t time.Time) time.Duration {
if !r.ok { if !r.ok {
return InfDuration return InfDuration
} }
delay := r.timeToAct.Sub(now) delay := r.timeToAct.Sub(t)
if delay < 0 { if delay < 0 {
return 0 return 0
} }
@ -150,7 +163,7 @@ func (r *Reservation) Cancel() {
// CancelAt indicates that the reservation holder will not perform the reserved action // CancelAt indicates that the reservation holder will not perform the reserved action
// and reverses the effects of this Reservation on the rate limit as much as possible, // and reverses the effects of this Reservation on the rate limit as much as possible,
// considering that other reservations may have already been made. // considering that other reservations may have already been made.
func (r *Reservation) CancelAt(now time.Time) { func (r *Reservation) CancelAt(t time.Time) {
if !r.ok { if !r.ok {
return return
} }
@ -158,7 +171,7 @@ func (r *Reservation) CancelAt(now time.Time) {
r.lim.mu.Lock() r.lim.mu.Lock()
defer r.lim.mu.Unlock() defer r.lim.mu.Unlock()
if r.lim.limit == Inf || r.tokens == 0 || r.timeToAct.Before(now) { if r.lim.limit == Inf || r.tokens == 0 || r.timeToAct.Before(t) {
return return
} }
@ -170,18 +183,18 @@ func (r *Reservation) CancelAt(now time.Time) {
return return
} }
// advance time to now // advance time to now
now, _, tokens := r.lim.advance(now) t, _, tokens := r.lim.advance(t)
// calculate new number of tokens // calculate new number of tokens
tokens += restoreTokens tokens += restoreTokens
if burst := float64(r.lim.burst); tokens > burst { if burst := float64(r.lim.burst); tokens > burst {
tokens = burst tokens = burst
} }
// update state // update state
r.lim.last = now r.lim.last = t
r.lim.tokens = tokens r.lim.tokens = tokens
if r.timeToAct == r.lim.lastEvent { if r.timeToAct == r.lim.lastEvent {
prevEvent := r.timeToAct.Add(r.limit.durationFromTokens(float64(-r.tokens))) prevEvent := r.timeToAct.Add(r.limit.durationFromTokens(float64(-r.tokens)))
if !prevEvent.Before(now) { if !prevEvent.Before(t) {
r.lim.lastEvent = prevEvent r.lim.lastEvent = prevEvent
} }
} }
@ -196,18 +209,20 @@ func (lim *Limiter) Reserve() *Reservation {
// The Limiter takes this Reservation into account when allowing future events. // The Limiter takes this Reservation into account when allowing future events.
// The returned Reservations OK() method returns false if n exceeds the Limiter's burst size. // The returned Reservations OK() method returns false if n exceeds the Limiter's burst size.
// Usage example: // Usage example:
// r := lim.ReserveN(time.Now(), 1) //
// if !r.OK() { // r := lim.ReserveN(time.Now(), 1)
// // Not allowed to act! Did you remember to set lim.burst to be > 0 ? // if !r.OK() {
// return // // Not allowed to act! Did you remember to set lim.burst to be > 0 ?
// } // return
// time.Sleep(r.Delay()) // }
// Act() // time.Sleep(r.Delay())
// Act()
//
// Use this method if you wish to wait and slow down in accordance with the rate limit without dropping events. // Use this method if you wish to wait and slow down in accordance with the rate limit without dropping events.
// If you need to respect a deadline or cancel the delay, use Wait instead. // If you need to respect a deadline or cancel the delay, use Wait instead.
// To drop or skip events exceeding rate limit, use Allow instead. // To drop or skip events exceeding rate limit, use Allow instead.
func (lim *Limiter) ReserveN(now time.Time, n int) *Reservation { func (lim *Limiter) ReserveN(t time.Time, n int) *Reservation {
r := lim.reserveN(now, n, InfDuration) r := lim.reserveN(t, n, InfDuration)
return &r return &r
} }
@ -221,6 +236,18 @@ func (lim *Limiter) Wait(ctx context.Context) (err error) {
// canceled, or the expected wait time exceeds the Context's Deadline. // canceled, or the expected wait time exceeds the Context's Deadline.
// The burst limit is ignored if the rate limit is Inf. // 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 context.Context, n int) (err error) {
// The test code calls lim.wait with a fake timer generator.
// This is the real timer generator.
newTimer := func(d time.Duration) (<-chan time.Time, func() bool, func()) {
timer := time.NewTimer(d)
return timer.C, timer.Stop, func() {}
}
return lim.wait(ctx, n, time.Now(), newTimer)
}
// wait is the internal implementation of WaitN.
func (lim *Limiter) wait(ctx context.Context, n int, t time.Time, newTimer func(d time.Duration) (<-chan time.Time, func() bool, func())) error {
lim.mu.Lock() lim.mu.Lock()
burst := lim.burst burst := lim.burst
limit := lim.limit limit := lim.limit
@ -236,25 +263,25 @@ func (lim *Limiter) WaitN(ctx context.Context, n int) (err error) {
default: default:
} }
// Determine wait limit // Determine wait limit
now := time.Now()
waitLimit := InfDuration waitLimit := InfDuration
if deadline, ok := ctx.Deadline(); ok { if deadline, ok := ctx.Deadline(); ok {
waitLimit = deadline.Sub(now) waitLimit = deadline.Sub(t)
} }
// Reserve // Reserve
r := lim.reserveN(now, n, waitLimit) r := lim.reserveN(t, n, waitLimit)
if !r.ok { if !r.ok {
return fmt.Errorf("rate: Wait(n=%d) would exceed context deadline", n) return fmt.Errorf("rate: Wait(n=%d) would exceed context deadline", n)
} }
// Wait if necessary // Wait if necessary
delay := r.DelayFrom(now) delay := r.DelayFrom(t)
if delay == 0 { if delay == 0 {
return nil return nil
} }
t := time.NewTimer(delay) ch, stop, advance := newTimer(delay)
defer t.Stop() defer stop()
advance() // only has an effect when testing
select { select {
case <-t.C: case <-ch:
// We can proceed. // We can proceed.
return nil return nil
case <-ctx.Done(): case <-ctx.Done():
@ -273,13 +300,13 @@ func (lim *Limiter) SetLimit(newLimit Limit) {
// SetLimitAt sets a new Limit for the limiter. The new Limit, and Burst, may be violated // SetLimitAt sets a new Limit for the limiter. The new Limit, and Burst, may be violated
// or underutilized by those which reserved (using Reserve or Wait) but did not yet act // or underutilized by those which reserved (using Reserve or Wait) but did not yet act
// before SetLimitAt was called. // before SetLimitAt was called.
func (lim *Limiter) SetLimitAt(now time.Time, newLimit Limit) { func (lim *Limiter) SetLimitAt(t time.Time, newLimit Limit) {
lim.mu.Lock() lim.mu.Lock()
defer lim.mu.Unlock() defer lim.mu.Unlock()
now, _, tokens := lim.advance(now) t, _, tokens := lim.advance(t)
lim.last = now lim.last = t
lim.tokens = tokens lim.tokens = tokens
lim.limit = newLimit lim.limit = newLimit
} }
@ -290,13 +317,13 @@ func (lim *Limiter) SetBurst(newBurst int) {
} }
// SetBurstAt sets a new burst size for the limiter. // SetBurstAt sets a new burst size for the limiter.
func (lim *Limiter) SetBurstAt(now time.Time, newBurst int) { func (lim *Limiter) SetBurstAt(t time.Time, newBurst int) {
lim.mu.Lock() lim.mu.Lock()
defer lim.mu.Unlock() defer lim.mu.Unlock()
now, _, tokens := lim.advance(now) t, _, tokens := lim.advance(t)
lim.last = now lim.last = t
lim.tokens = tokens lim.tokens = tokens
lim.burst = newBurst lim.burst = newBurst
} }
@ -304,20 +331,32 @@ func (lim *Limiter) SetBurstAt(now time.Time, newBurst int) {
// reserveN is a helper method for AllowN, ReserveN, and WaitN. // reserveN is a helper method for AllowN, ReserveN, and WaitN.
// maxFutureReserve specifies the maximum reservation wait duration allowed. // maxFutureReserve specifies the maximum reservation wait duration allowed.
// reserveN returns Reservation, not *Reservation, to avoid allocation in AllowN and WaitN. // reserveN returns Reservation, not *Reservation, to avoid allocation in AllowN and WaitN.
func (lim *Limiter) reserveN(now time.Time, n int, maxFutureReserve time.Duration) Reservation { func (lim *Limiter) reserveN(t time.Time, n int, maxFutureReserve time.Duration) Reservation {
lim.mu.Lock() lim.mu.Lock()
defer lim.mu.Unlock()
if lim.limit == Inf { if lim.limit == Inf {
lim.mu.Unlock()
return Reservation{ return Reservation{
ok: true, ok: true,
lim: lim, lim: lim,
tokens: n, tokens: n,
timeToAct: now, timeToAct: t,
}
} else if lim.limit == 0 {
var ok bool
if lim.burst >= n {
ok = true
lim.burst -= n
}
return Reservation{
ok: ok,
lim: lim,
tokens: lim.burst,
timeToAct: t,
} }
} }
now, last, tokens := lim.advance(now) t, last, tokens := lim.advance(t)
// Calculate the remaining number of tokens resulting from the request. // Calculate the remaining number of tokens resulting from the request.
tokens -= float64(n) tokens -= float64(n)
@ -339,44 +378,46 @@ func (lim *Limiter) reserveN(now time.Time, n int, maxFutureReserve time.Duratio
} }
if ok { if ok {
r.tokens = n r.tokens = n
r.timeToAct = now.Add(waitDuration) r.timeToAct = t.Add(waitDuration)
} }
// Update state // Update state
if ok { if ok {
lim.last = now lim.last = t
lim.tokens = tokens lim.tokens = tokens
lim.lastEvent = r.timeToAct lim.lastEvent = r.timeToAct
} else { } else {
lim.last = last lim.last = last
} }
lim.mu.Unlock()
return r return r
} }
// advance calculates and returns an updated state for lim resulting from the passage of time. // advance calculates and returns an updated state for lim resulting from the passage of time.
// lim is not changed. // lim is not changed.
// advance requires that lim.mu is held. // advance requires that lim.mu is held.
func (lim *Limiter) advance(now time.Time) (newNow time.Time, newLast time.Time, newTokens float64) { func (lim *Limiter) advance(t time.Time) (newT time.Time, newLast time.Time, newTokens float64) {
last := lim.last last := lim.last
if now.Before(last) { if t.Before(last) {
last = now last = t
} }
// Calculate the new number of tokens, due to time that passed. // Calculate the new number of tokens, due to time that passed.
elapsed := now.Sub(last) elapsed := t.Sub(last)
delta := lim.limit.tokensFromDuration(elapsed) delta := lim.limit.tokensFromDuration(elapsed)
tokens := lim.tokens + delta tokens := lim.tokens + delta
if burst := float64(lim.burst); tokens > burst { if burst := float64(lim.burst); tokens > burst {
tokens = burst tokens = burst
} }
return now, last, tokens return t, last, tokens
} }
// durationFromTokens is a unit conversion function from the number of tokens to the duration // durationFromTokens is a unit conversion function from the number of tokens to the duration
// of time it takes to accumulate them at a rate of limit tokens per second. // of time it takes to accumulate them at a rate of limit tokens per second.
func (limit Limit) durationFromTokens(tokens float64) time.Duration { func (limit Limit) durationFromTokens(tokens float64) time.Duration {
if limit <= 0 {
return InfDuration
}
seconds := tokens / float64(limit) seconds := tokens / float64(limit)
return time.Duration(float64(time.Second) * seconds) return time.Duration(float64(time.Second) * seconds)
} }
@ -384,5 +425,8 @@ func (limit Limit) durationFromTokens(tokens float64) time.Duration {
// tokensFromDuration is a unit conversion function from a time duration to the number of tokens // tokensFromDuration is a unit conversion function from a time duration to the number of tokens
// which could be accumulated during that duration at a rate of limit tokens per second. // which could be accumulated during that duration at a rate of limit tokens per second.
func (limit Limit) tokensFromDuration(d time.Duration) float64 { func (limit Limit) tokensFromDuration(d time.Duration) float64 {
if limit <= 0 {
return 0
}
return d.Seconds() * float64(limit) return d.Seconds() * float64(limit)
} }

2
vendor/modules.txt vendored
View File

@ -289,7 +289,7 @@ golang.org/x/text/transform
golang.org/x/text/unicode/bidi golang.org/x/text/unicode/bidi
golang.org/x/text/unicode/norm golang.org/x/text/unicode/norm
golang.org/x/text/width golang.org/x/text/width
# golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac # golang.org/x/time v0.1.0
## explicit ## explicit
golang.org/x/time/rate golang.org/x/time/rate
# google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa # google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa