lib: use atomic types

This commit is contained in:
Roberto Ricci
2023-08-18 23:05:17 +02:00
committed by Nick Craig-Wood
parent 01a155fb00
commit 552b6c47ff
3 changed files with 13 additions and 13 deletions

View File

@ -10,7 +10,7 @@ import (
type Renew struct {
name string // name to use in logs
ts *TokenSource // token source that needs renewing
uploads int32 // number of uploads in progress - atomic access required
uploads atomic.Int32 // number of uploads in progress
run func() error // a transaction to run to renew the token on
}
@ -37,7 +37,7 @@ func (r *Renew) renewOnExpiry() {
expiry := r.ts.OnExpiry()
for {
<-expiry
uploads := atomic.LoadInt32(&r.uploads)
uploads := r.uploads.Load()
if uploads != 0 {
fs.Debugf(r.name, "Token expired - %d uploads in progress - refreshing", uploads)
// Do a transaction
@ -55,12 +55,12 @@ func (r *Renew) renewOnExpiry() {
// Start should be called before starting an upload
func (r *Renew) Start() {
atomic.AddInt32(&r.uploads, 1)
r.uploads.Add(1)
}
// Stop should be called after finishing an upload
func (r *Renew) Stop() {
atomic.AddInt32(&r.uploads, -1)
r.uploads.Add(-1)
}
// Invalidate invalidates the token source