mirror of https://github.com/cbeuw/Cloak
Syncing in-memory credit with db regularly
This commit is contained in:
parent
fe19d31158
commit
0702e37391
|
|
@ -146,7 +146,7 @@ func main() {
|
||||||
log.Fatal("TicketTimeHint cannot be empty or 0")
|
log.Fatal("TicketTimeHint cannot be empty or 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
valve := mux.MakeValve(1e9, 1e9, 1e9, 1e9)
|
valve := mux.MakeValve(1e12, 1e12, 1e12, 1e12)
|
||||||
obfs := util.MakeObfs(sta.UID)
|
obfs := util.MakeObfs(sta.UID)
|
||||||
deobfs := util.MakeDeobfs(sta.UID)
|
deobfs := util.MakeDeobfs(sta.UID)
|
||||||
sesh := mux.MakeSession(0, valve, obfs, deobfs, util.ReadTLS)
|
sesh := mux.MakeSession(0, valve, obfs, deobfs, util.ReadTLS)
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,14 @@ func (v *Valve) txWait(n int) {
|
||||||
v.txtb.Load().(*ratelimit.Bucket).Wait(int64(n))
|
v.txtb.Load().(*ratelimit.Bucket).Wait(int64(n))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *Valve) GetRxCredit() int64 {
|
||||||
|
return atomic.LoadInt64(&v.rxCredit)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Valve) GetTxCredit() int64 {
|
||||||
|
return atomic.LoadInt64(&v.txCredit)
|
||||||
|
}
|
||||||
|
|
||||||
// n can be negative
|
// n can be negative
|
||||||
func (v *Valve) AddRxCredit(n int64) int64 {
|
func (v *Valve) AddRxCredit(n int64) int64 {
|
||||||
return atomic.AddInt64(&v.rxCredit, n)
|
return atomic.AddInt64(&v.rxCredit, n)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/boltdb/bolt"
|
"github.com/boltdb/bolt"
|
||||||
)
|
)
|
||||||
|
|
@ -24,6 +25,10 @@ func MakeUserpanel(dbPath string) (*Userpanel, error) {
|
||||||
db: db,
|
db: db,
|
||||||
activeUsers: make(map[[32]byte]*User),
|
activeUsers: make(map[[32]byte]*User),
|
||||||
}
|
}
|
||||||
|
go func() {
|
||||||
|
time.Sleep(time.Second * 10)
|
||||||
|
up.updateCredits()
|
||||||
|
}()
|
||||||
return up, nil
|
return up, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -147,3 +152,27 @@ func (up *Userpanel) SetSessionsCap(UID [32]byte, newSessionsCap uint32) error {
|
||||||
err := up.updateDBEntryUint32(UID, "sessionsCap", newSessionsCap)
|
err := up.updateDBEntryUint32(UID, "sessionsCap", newSessionsCap)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (up *Userpanel) updateCredits() {
|
||||||
|
up.activeUsersM.RLock()
|
||||||
|
defer u.activeUsersM.RUnlock()
|
||||||
|
for _, user := range up.activeUsers {
|
||||||
|
up.db.Update(func(tx *bolt.Tx) error {
|
||||||
|
b := tx.Bucket(u.uid[:])
|
||||||
|
if b == nil {
|
||||||
|
return ErrUserNotFound
|
||||||
|
}
|
||||||
|
oct := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(oct, uint64(u.valve.GetRxCredit()))
|
||||||
|
if err := b.Put([]byte("rxCredit"), oct); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
binary.BigEndian.PutUint64(oct, uint64(u.valve.GetTxCredit()))
|
||||||
|
if err := b.Put([]byte("txCredit"), oct); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue