summaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/time
diff options
context:
space:
mode:
authorWim <wim@42.be>2023-01-28 22:57:53 +0100
committerGitHub <noreply@github.com>2023-01-28 22:57:53 +0100
commit880586bac42817ffcfea5d9f746f503fa29915b8 (patch)
treea89374cba6f88975f12316ec8d1b8aa1d4c6ba79 /vendor/golang.org/x/time
parenteac2a8c8dc831f946970d327e2a80b26b0684255 (diff)
downloadmatterbridge-msglm-880586bac42817ffcfea5d9f746f503fa29915b8.tar.gz
matterbridge-msglm-880586bac42817ffcfea5d9f746f503fa29915b8.tar.bz2
matterbridge-msglm-880586bac42817ffcfea5d9f746f503fa29915b8.zip
Update dependencies (#1951)
Diffstat (limited to 'vendor/golang.org/x/time')
-rw-r--r--vendor/golang.org/x/time/AUTHORS3
-rw-r--r--vendor/golang.org/x/time/CONTRIBUTORS3
-rw-r--r--vendor/golang.org/x/time/rate/rate.go154
-rw-r--r--vendor/golang.org/x/time/rate/sometimes.go67
4 files changed, 159 insertions, 68 deletions
diff --git a/vendor/golang.org/x/time/AUTHORS b/vendor/golang.org/x/time/AUTHORS
deleted file mode 100644
index 15167cd7..00000000
--- a/vendor/golang.org/x/time/AUTHORS
+++ /dev/null
@@ -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.
diff --git a/vendor/golang.org/x/time/CONTRIBUTORS b/vendor/golang.org/x/time/CONTRIBUTORS
deleted file mode 100644
index 1c4577e9..00000000
--- a/vendor/golang.org/x/time/CONTRIBUTORS
+++ /dev/null
@@ -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.
diff --git a/vendor/golang.org/x/time/rate/rate.go b/vendor/golang.org/x/time/rate/rate.go
index a98fe778..8f7c29f1 100644
--- a/vendor/golang.org/x/time/rate/rate.go
+++ b/vendor/golang.org/x/time/rate/rate.go
@@ -80,6 +80,19 @@ func (lim *Limiter) Burst() int {
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
// bursts of at most b tokens.
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 {
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.
// Otherwise use Reserve or Wait.
-func (lim *Limiter) AllowN(now time.Time, n int) bool {
- return lim.reserveN(now, n, 0).ok
+func (lim *Limiter) AllowN(t time.Time, n int) bool {
+ return lim.reserveN(t, n, 0).ok
}
// 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.
-const InfDuration = time.Duration(1<<63 - 1)
+const InfDuration = time.Duration(math.MaxInt64)
// DelayFrom returns the duration for which the reservation holder must wait
// before taking the reserved action. Zero duration means act immediately.
// InfDuration means the limiter cannot grant the tokens requested in this
// 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 {
return InfDuration
}
- delay := r.timeToAct.Sub(now)
+ delay := r.timeToAct.Sub(t)
if delay < 0 {
return 0
}
@@ -145,13 +158,12 @@ func (r *Reservation) DelayFrom(now time.Time) time.Duration {
// Cancel is shorthand for CancelAt(time.Now()).
func (r *Reservation) Cancel() {
r.CancelAt(time.Now())
- return
}
// 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,
// 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 {
return
}
@@ -159,7 +171,7 @@ func (r *Reservation) CancelAt(now time.Time) {
r.lim.mu.Lock()
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
}
@@ -171,23 +183,21 @@ func (r *Reservation) CancelAt(now time.Time) {
return
}
// advance time to now
- now, _, tokens := r.lim.advance(now)
+ t, _, tokens := r.lim.advance(t)
// calculate new number of tokens
tokens += restoreTokens
if burst := float64(r.lim.burst); tokens > burst {
tokens = burst
}
// update state
- r.lim.last = now
+ r.lim.last = t
r.lim.tokens = tokens
if r.timeToAct == r.lim.lastEvent {
prevEvent := r.timeToAct.Add(r.limit.durationFromTokens(float64(-r.tokens)))
- if !prevEvent.Before(now) {
+ if !prevEvent.Before(t) {
r.lim.lastEvent = prevEvent
}
}
-
- return
}
// Reserve is shorthand for ReserveN(time.Now(), 1).
@@ -199,18 +209,20 @@ func (lim *Limiter) Reserve() *Reservation {
// The Limiter takes this Reservation into account when allowing future events.
// The returned Reservation’s OK() method returns false if n exceeds the Limiter's burst size.
// Usage example:
-// r := lim.ReserveN(time.Now(), 1)
-// if !r.OK() {
-// // Not allowed to act! Did you remember to set lim.burst to be > 0 ?
-// return
-// }
-// time.Sleep(r.Delay())
-// Act()
+//
+// r := lim.ReserveN(time.Now(), 1)
+// if !r.OK() {
+// // Not allowed to act! Did you remember to set lim.burst to be > 0 ?
+// return
+// }
+// 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.
// 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.
-func (lim *Limiter) ReserveN(now time.Time, n int) *Reservation {
- r := lim.reserveN(now, n, InfDuration)
+func (lim *Limiter) ReserveN(t time.Time, n int) *Reservation {
+ r := lim.reserveN(t, n, InfDuration)
return &r
}
@@ -224,6 +236,18 @@ func (lim *Limiter) Wait(ctx context.Context) (err error) {
// 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) {
+ // 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()
burst := lim.burst
limit := lim.limit
@@ -239,25 +263,25 @@ func (lim *Limiter) WaitN(ctx context.Context, n int) (err error) {
default:
}
// Determine wait limit
- now := time.Now()
waitLimit := InfDuration
if deadline, ok := ctx.Deadline(); ok {
- waitLimit = deadline.Sub(now)
+ waitLimit = deadline.Sub(t)
}
// Reserve
- r := lim.reserveN(now, n, waitLimit)
+ r := lim.reserveN(t, n, waitLimit)
if !r.ok {
return fmt.Errorf("rate: Wait(n=%d) would exceed context deadline", n)
}
// Wait if necessary
- delay := r.DelayFrom(now)
+ delay := r.DelayFrom(t)
if delay == 0 {
return nil
}
- t := time.NewTimer(delay)
- defer t.Stop()
+ ch, stop, advance := newTimer(delay)
+ defer stop()
+ advance() // only has an effect when testing
select {
- case <-t.C:
+ case <-ch:
// We can proceed.
return nil
case <-ctx.Done():
@@ -276,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
// or underutilized by those which reserved (using Reserve or Wait) but did not yet act
// 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()
defer lim.mu.Unlock()
- now, _, tokens := lim.advance(now)
+ t, _, tokens := lim.advance(t)
- lim.last = now
+ lim.last = t
lim.tokens = tokens
lim.limit = newLimit
}
@@ -293,13 +317,13 @@ func (lim *Limiter) SetBurst(newBurst int) {
}
// 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()
defer lim.mu.Unlock()
- now, _, tokens := lim.advance(now)
+ t, _, tokens := lim.advance(t)
- lim.last = now
+ lim.last = t
lim.tokens = tokens
lim.burst = newBurst
}
@@ -307,20 +331,32 @@ func (lim *Limiter) SetBurstAt(now time.Time, newBurst int) {
// reserveN is a helper method for AllowN, ReserveN, and WaitN.
// maxFutureReserve specifies the maximum reservation wait duration allowed.
// 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()
+ defer lim.mu.Unlock()
if lim.limit == Inf {
- lim.mu.Unlock()
return Reservation{
ok: true,
lim: lim,
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.
tokens -= float64(n)
@@ -342,61 +378,55 @@ func (lim *Limiter) reserveN(now time.Time, n int, maxFutureReserve time.Duratio
}
if ok {
r.tokens = n
- r.timeToAct = now.Add(waitDuration)
+ r.timeToAct = t.Add(waitDuration)
}
// Update state
if ok {
- lim.last = now
+ lim.last = t
lim.tokens = tokens
lim.lastEvent = r.timeToAct
} else {
lim.last = last
}
- lim.mu.Unlock()
return r
}
// advance calculates and returns an updated state for lim resulting from the passage of time.
// lim is not changed.
// 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
- if now.Before(last) {
- last = now
- }
-
- // Avoid making delta overflow below when last is very old.
- maxElapsed := lim.limit.durationFromTokens(float64(lim.burst) - lim.tokens)
- elapsed := now.Sub(last)
- if elapsed > maxElapsed {
- elapsed = maxElapsed
+ if t.Before(last) {
+ last = t
}
// Calculate the new number of tokens, due to time that passed.
+ elapsed := t.Sub(last)
delta := lim.limit.tokensFromDuration(elapsed)
tokens := lim.tokens + delta
if burst := float64(lim.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
// of time it takes to accumulate them at a rate of limit tokens per second.
func (limit Limit) durationFromTokens(tokens float64) time.Duration {
+ if limit <= 0 {
+ return InfDuration
+ }
seconds := tokens / float64(limit)
- return time.Nanosecond * time.Duration(1e9*seconds)
+ return time.Duration(float64(time.Second) * seconds)
}
// 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.
func (limit Limit) tokensFromDuration(d time.Duration) float64 {
- // Split the integer and fractional parts ourself to minimize rounding errors.
- // See golang.org/issues/34861.
- sec := float64(d/time.Second) * float64(limit)
- nsec := float64(d%time.Second) * float64(limit)
- return sec + nsec/1e9
+ if limit <= 0 {
+ return 0
+ }
+ return d.Seconds() * float64(limit)
}
diff --git a/vendor/golang.org/x/time/rate/sometimes.go b/vendor/golang.org/x/time/rate/sometimes.go
new file mode 100644
index 00000000..6ba99ddb
--- /dev/null
+++ b/vendor/golang.org/x/time/rate/sometimes.go
@@ -0,0 +1,67 @@
+// Copyright 2022 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.
+
+package rate
+
+import (
+ "sync"
+ "time"
+)
+
+// Sometimes will perform an action occasionally. The First, Every, and
+// Interval fields govern the behavior of Do, which performs the action.
+// A zero Sometimes value will perform an action exactly once.
+//
+// # Example: logging with rate limiting
+//
+// var sometimes = rate.Sometimes{First: 3, Interval: 10*time.Second}
+// func Spammy() {
+// sometimes.Do(func() { log.Info("here I am!") })
+// }
+type Sometimes struct {
+ First int // if non-zero, the first N calls to Do will run f.
+ Every int // if non-zero, every Nth call to Do will run f.
+ Interval time.Duration // if non-zero and Interval has elapsed since f's last run, Do will run f.
+
+ mu sync.Mutex
+ count int // number of Do calls
+ last time.Time // last time f was run
+}
+
+// Do runs the function f as allowed by First, Every, and Interval.
+//
+// The model is a union (not intersection) of filters. The first call to Do
+// always runs f. Subsequent calls to Do run f if allowed by First or Every or
+// Interval.
+//
+// A non-zero First:N causes the first N Do(f) calls to run f.
+//
+// A non-zero Every:M causes every Mth Do(f) call, starting with the first, to
+// run f.
+//
+// A non-zero Interval causes Do(f) to run f if Interval has elapsed since
+// Do last ran f.
+//
+// Specifying multiple filters produces the union of these execution streams.
+// For example, specifying both First:N and Every:M causes the first N Do(f)
+// calls and every Mth Do(f) call, starting with the first, to run f. See
+// Examples for more.
+//
+// If Do is called multiple times simultaneously, the calls will block and run
+// serially. Therefore, Do is intended for lightweight operations.
+//
+// Because a call to Do may block until f returns, if f causes Do to be called,
+// it will deadlock.
+func (s *Sometimes) Do(f func()) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ if s.count == 0 ||
+ (s.First > 0 && s.count < s.First) ||
+ (s.Every > 0 && s.count%s.Every == 0) ||
+ (s.Interval > 0 && time.Since(s.last) >= s.Interval) {
+ f()
+ s.last = time.Now()
+ }
+ s.count++
+}