From 2f6ab0d451ec3cbc1f93147573a6ab2e9bfc58ea Mon Sep 17 00:00:00 2001 From: diamondburned Date: Thu, 31 Dec 2020 10:24:51 -0800 Subject: [PATCH] API: Fix atomic SIGSEGV in ARM --- api/rate/rate.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/api/rate/rate.go b/api/rate/rate.go index 9a358e4..1a19753 100644 --- a/api/rate/rate.go +++ b/api/rate/rate.go @@ -31,7 +31,8 @@ type Limiter struct { Prefix string - global int64 // atomic guarded, unixnano + // global is a pointer to prevent ARM-compatibility alignment. + global *int64 // atomic guarded, unixnano bucketMu sync.Mutex buckets map[string]*bucket @@ -62,6 +63,7 @@ func newBucket() *bucket { func NewLimiter(prefix string) *Limiter { return &Limiter{ Prefix: prefix, + global: new(int64), buckets: map[string]*bucket{}, CustomLimits: []*CustomRateLimit{}, } @@ -111,7 +113,7 @@ func (l *Limiter) Acquire(ctx context.Context, path string) error { until = b.reset } else { // maybe global rate limit has it - until = time.Unix(0, atomic.LoadInt64(&l.global)) + until = time.Unix(0, atomic.LoadInt64(l.global)) } if until.After(now) { @@ -181,8 +183,8 @@ func (l *Limiter) Release(path string, headers http.Header) error { at := time.Now().Add(time.Duration(i) * time.Second) - if global != "" { // probably true - atomic.StoreInt64(&l.global, at.UnixNano()) + if global != "" { // probably "true" + atomic.StoreInt64(l.global, at.UnixNano()) } else { b.reset = at }