2017-01-30 04:35:57 +08:00
|
|
|
package oauthutil
|
|
|
|
|
|
|
|
import (
|
2023-11-30 14:16:22 +08:00
|
|
|
"sync"
|
2017-01-30 04:35:57 +08:00
|
|
|
"sync/atomic"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2017-01-30 04:35:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Renew allows tokens to be renewed on expiry if uploads are in progress.
|
|
|
|
type Renew struct {
|
2023-11-30 14:16:22 +08:00
|
|
|
name string // name to use in logs
|
|
|
|
ts *TokenSource // token source that needs renewing
|
|
|
|
uploads atomic.Int32 // number of uploads in progress
|
|
|
|
run func() error // a transaction to run to renew the token on
|
|
|
|
done chan any // channel to end the go routine
|
|
|
|
shutdown sync.Once
|
2017-01-30 04:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRenew creates a new Renew struct and starts a background process
|
|
|
|
// which renews the token whenever it expires. It uses the run() call
|
|
|
|
// to run a transaction to do this.
|
|
|
|
//
|
|
|
|
// It will only renew the token if the number of uploads > 0
|
|
|
|
func NewRenew(name string, ts *TokenSource, run func() error) *Renew {
|
|
|
|
r := &Renew{
|
|
|
|
name: name,
|
|
|
|
ts: ts,
|
|
|
|
run: run,
|
2023-11-30 14:16:22 +08:00
|
|
|
done: make(chan any),
|
2017-01-30 04:35:57 +08:00
|
|
|
}
|
|
|
|
go r.renewOnExpiry()
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// renewOnExpiry renews the token whenever it expires. Useful when there
|
|
|
|
// are lots of uploads in progress and the token doesn't get renewed.
|
|
|
|
// Amazon seem to cancel your uploads if you don't renew your token
|
|
|
|
// for 2hrs.
|
|
|
|
func (r *Renew) renewOnExpiry() {
|
|
|
|
expiry := r.ts.OnExpiry()
|
|
|
|
for {
|
2023-11-30 14:16:22 +08:00
|
|
|
select {
|
|
|
|
case <-expiry:
|
|
|
|
case <-r.done:
|
|
|
|
return
|
|
|
|
}
|
2023-08-19 05:05:17 +08:00
|
|
|
uploads := r.uploads.Load()
|
2017-01-30 04:35:57 +08:00
|
|
|
if uploads != 0 {
|
2017-02-09 19:01:20 +08:00
|
|
|
fs.Debugf(r.name, "Token expired - %d uploads in progress - refreshing", uploads)
|
2017-01-30 04:35:57 +08:00
|
|
|
// Do a transaction
|
|
|
|
err := r.run()
|
|
|
|
if err == nil {
|
2017-02-09 19:01:20 +08:00
|
|
|
fs.Debugf(r.name, "Token refresh successful")
|
2017-01-30 04:35:57 +08:00
|
|
|
} else {
|
2017-02-09 19:01:20 +08:00
|
|
|
fs.Errorf(r.name, "Token refresh failed: %v", err)
|
2017-01-30 04:35:57 +08:00
|
|
|
}
|
|
|
|
} else {
|
2017-02-09 19:01:20 +08:00
|
|
|
fs.Debugf(r.name, "Token expired but no uploads in progress - doing nothing")
|
2017-01-30 04:35:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start should be called before starting an upload
|
|
|
|
func (r *Renew) Start() {
|
2023-08-19 05:05:17 +08:00
|
|
|
r.uploads.Add(1)
|
2017-01-30 04:35:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop should be called after finishing an upload
|
|
|
|
func (r *Renew) Stop() {
|
2023-08-19 05:05:17 +08:00
|
|
|
r.uploads.Add(-1)
|
2017-01-30 04:35:57 +08:00
|
|
|
}
|
2017-02-12 01:49:59 +08:00
|
|
|
|
|
|
|
// Invalidate invalidates the token source
|
|
|
|
func (r *Renew) Invalidate() {
|
|
|
|
r.ts.Invalidate()
|
|
|
|
}
|
2021-03-03 18:33:29 +08:00
|
|
|
|
|
|
|
// Expire expires the token source
|
|
|
|
func (r *Renew) Expire() error {
|
|
|
|
return r.ts.Expire()
|
|
|
|
}
|
2023-11-30 14:16:22 +08:00
|
|
|
|
|
|
|
// Shutdown stops the timer and no more renewal will take place.
|
|
|
|
func (r *Renew) Shutdown() {
|
|
|
|
if r == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// closing a channel can only be done once
|
|
|
|
r.shutdown.Do(func() {
|
|
|
|
r.ts.expiryTimer.Stop()
|
|
|
|
close(r.done)
|
|
|
|
})
|
|
|
|
}
|