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:
parent
c229ba1da1
commit
e7598966ca
4 changed files with 15 additions and 7 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue