2018-02-01 14:13:24 +01:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
2020-11-05 17:59:59 +01:00
|
|
|
"context"
|
2018-02-01 14:13:24 +01:00
|
|
|
"sync"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2018-02-01 14:13:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// inProgress holds a synchronized map of in progress transfers
|
|
|
|
type inProgress struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
m map[string]*Account
|
|
|
|
}
|
|
|
|
|
|
|
|
// newInProgress makes a new inProgress object
|
2020-11-05 17:59:59 +01:00
|
|
|
func newInProgress(ctx context.Context) *inProgress {
|
2020-11-05 12:33:32 +01:00
|
|
|
ci := fs.GetConfig(ctx)
|
2018-02-01 14:13:24 +01:00
|
|
|
return &inProgress{
|
2020-11-05 12:33:32 +01:00
|
|
|
m: make(map[string]*Account, ci.Transfers),
|
2018-02-01 14:13:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set marks the name as in progress
|
|
|
|
func (ip *inProgress) set(name string, acc *Account) {
|
|
|
|
ip.mu.Lock()
|
|
|
|
defer ip.mu.Unlock()
|
|
|
|
ip.m[name] = acc
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear marks the name as no longer in progress
|
|
|
|
func (ip *inProgress) clear(name string) {
|
|
|
|
ip.mu.Lock()
|
|
|
|
defer ip.mu.Unlock()
|
|
|
|
delete(ip.m, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get gets the account for name, of nil if not found
|
|
|
|
func (ip *inProgress) get(name string) *Account {
|
|
|
|
ip.mu.Lock()
|
|
|
|
defer ip.mu.Unlock()
|
|
|
|
return ip.m[name]
|
|
|
|
}
|
2019-07-18 12:13:54 +02:00
|
|
|
|
|
|
|
// merge adds items from another inProgress
|
|
|
|
func (ip *inProgress) merge(m *inProgress) {
|
|
|
|
ip.mu.Lock()
|
|
|
|
defer ip.mu.Unlock()
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
for key, val := range m.m {
|
|
|
|
ip.m[key] = val
|
|
|
|
}
|
|
|
|
}
|