mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
add quota
This commit is contained in:
parent
db6c90b8dd
commit
e57b9f235b
67
pkg/bbgo/quota.go
Normal file
67
pkg/bbgo/quota.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package bbgo
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
)
|
||||
|
||||
type Quota struct {
|
||||
mu sync.Mutex
|
||||
Available fixedpoint.Value
|
||||
Locked fixedpoint.Value
|
||||
}
|
||||
|
||||
func (q *Quota) Add(fund fixedpoint.Value) {
|
||||
q.mu.Lock()
|
||||
q.Available += fund
|
||||
q.mu.Unlock()
|
||||
}
|
||||
|
||||
func (q *Quota) Lock(fund fixedpoint.Value) bool {
|
||||
if fund > q.Available {
|
||||
return false
|
||||
}
|
||||
|
||||
q.mu.Lock()
|
||||
q.Available -= fund
|
||||
q.Locked += fund
|
||||
q.mu.Unlock()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (q *Quota) Commit() {
|
||||
q.mu.Lock()
|
||||
q.Locked = 0
|
||||
q.mu.Unlock()
|
||||
}
|
||||
|
||||
func (q *Quota) Rollback() {
|
||||
q.mu.Lock()
|
||||
q.Available += q.Locked
|
||||
q.Locked = 0
|
||||
q.mu.Unlock()
|
||||
}
|
||||
|
||||
type QuotaTransaction struct {
|
||||
mu sync.Mutex
|
||||
BaseAsset Quota
|
||||
QuoteAsset Quota
|
||||
}
|
||||
|
||||
func (m *QuotaTransaction) Commit() bool {
|
||||
m.mu.Lock()
|
||||
m.BaseAsset.Commit()
|
||||
m.QuoteAsset.Commit()
|
||||
m.mu.Unlock()
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *QuotaTransaction) Rollback() bool {
|
||||
m.mu.Lock()
|
||||
m.BaseAsset.Rollback()
|
||||
m.QuoteAsset.Rollback()
|
||||
m.mu.Unlock()
|
||||
return true
|
||||
}
|
Loading…
Reference in New Issue
Block a user