1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-09 17:45:03 +09:00

Update limits

This commit is contained in:
mcrakhman 2024-07-03 21:47:38 +02:00
parent c229ba1da1
commit e7598966ca
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
4 changed files with 15 additions and 7 deletions

View file

@ -3,22 +3,29 @@ package sync
import "sync"
type guard struct {
mu sync.Mutex
taken map[string]struct{}
mu sync.Mutex
taken map[string]struct{}
limit int
takenCount int
}
func newGuard() *guard {
func newGuard(limit int) *guard {
return &guard{
taken: make(map[string]struct{}),
limit: limit,
}
}
func (g *guard) TryTake(id string) bool {
g.mu.Lock()
defer g.mu.Unlock()
if g.limit != 0 && g.takenCount >= g.limit {
return false
}
if _, exists := g.taken[id]; exists {
return false
}
g.takenCount++
g.taken[id] = struct{}{}
return true
}
@ -26,5 +33,6 @@ func (g *guard) TryTake(id string) bool {
func (g *guard) Release(id string) {
g.mu.Lock()
defer g.mu.Unlock()
g.takenCount--
delete(g.taken, id)
}

View file

@ -40,7 +40,7 @@ func NewRequestManager(handler syncdeps.SyncHandler, metric syncdeps.QueueSizeUp
return &requestManager{
requestPool: NewRequestPool(),
handler: handler,
incomingGuard: newGuard(),
incomingGuard: newGuard(1000),
metric: metric,
}
}

View file

@ -27,7 +27,7 @@ func NewRequestPool() RequestPool {
ctx: ctx,
cancel: cancel,
pools: make(map[string]*tryAddQueue),
peerGuard: newGuard(),
peerGuard: newGuard(0),
}
}
@ -57,7 +57,7 @@ func (rp *requestPool) QueueRequestAction(peerId, objectId string, action func(c
)
pool, exists = rp.pools[peerId]
if !exists {
pool = newTryAddQueue(100, 100)
pool = newTryAddQueue(10, 100)
rp.pools[peerId] = pool
pool.Run()
}