Some streampool fixes
This commit is contained in:
parent
98b8584efc
commit
a103c960a4
@ -61,6 +61,12 @@ func (q *actionQueue) read() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, msg := range actions {
|
for _, msg := range actions {
|
||||||
|
select {
|
||||||
|
case <-q.ctx.Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
<-limiter
|
<-limiter
|
||||||
go func(action ActionFunc) {
|
go func(action ActionFunc) {
|
||||||
err = action()
|
err = action()
|
||||||
|
|||||||
@ -247,50 +247,41 @@ func (s *streamPool) Close() (err error) {
|
|||||||
|
|
||||||
func (s *streamPool) readPeerLoop(peerId string, stream spacesyncproto.ObjectSyncStream) (err error) {
|
func (s *streamPool) readPeerLoop(peerId string, stream spacesyncproto.ObjectSyncStream) (err error) {
|
||||||
var (
|
var (
|
||||||
msg *spacesyncproto.ObjectSyncMessage
|
log = log.With(zap.String("peerId", peerId))
|
||||||
mx = sync.Mutex{}
|
queue = NewActionQueue()
|
||||||
maxWaitMsgs = 500
|
|
||||||
queue = make([]*spacesyncproto.ObjectSyncMessage, 0, 10)
|
|
||||||
readers = 0
|
|
||||||
log = log.With(zap.String("peerId", peerId))
|
|
||||||
)
|
)
|
||||||
|
queue.Run()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
log.Debug("stopped reading stream from peer")
|
log.Debug("stopped reading stream from peer")
|
||||||
s.removePeer(peerId, stream)
|
s.removePeer(peerId, stream)
|
||||||
|
queue.Close()
|
||||||
s.wg.Done()
|
s.wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
log.Debug("started reading stream from peer")
|
log.Debug("started reading stream from peer")
|
||||||
|
|
||||||
readQueue := func() {
|
stopWaiter := func(msg *spacesyncproto.ObjectSyncMessage) bool {
|
||||||
defer func() {
|
s.waitersMx.Lock()
|
||||||
mx.Lock()
|
waiter, exists := s.waiters[msg.ReplyId]
|
||||||
readers--
|
if exists {
|
||||||
mx.Unlock()
|
delete(s.waiters, msg.ReplyId)
|
||||||
}()
|
s.waitersMx.Unlock()
|
||||||
|
waiter.ch <- msg
|
||||||
for {
|
return true
|
||||||
select {
|
|
||||||
case <-stream.Context().Done():
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
mx.Lock()
|
|
||||||
if len(queue) == 0 {
|
|
||||||
mx.Unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
newEl := queue[0]
|
|
||||||
queue = queue[1:]
|
|
||||||
mx.Unlock()
|
|
||||||
|
|
||||||
log := log.With(zap.String("replyId", newEl.ReplyId), zap.String("object id", newEl.ObjectId))
|
|
||||||
log.Debug("getting message with reply id")
|
|
||||||
err = s.messageHandler(stream.Context(), peerId, newEl)
|
|
||||||
if err != nil {
|
|
||||||
log.With(zap.Error(err)).Debug("message handling failed")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
s.waitersMx.Unlock()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
process := func(msg *spacesyncproto.ObjectSyncMessage) error {
|
||||||
|
log := log.With(zap.String("replyId", msg.ReplyId), zap.String("object id", msg.ObjectId))
|
||||||
|
log.Debug("getting message with reply id")
|
||||||
|
err = s.messageHandler(stream.Context(), peerId, msg)
|
||||||
|
if err != nil {
|
||||||
|
log.With(zap.Error(err)).Debug("message handling failed")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@ -299,43 +290,25 @@ func (s *streamPool) readPeerLoop(peerId string, stream spacesyncproto.ObjectSyn
|
|||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
var msg *spacesyncproto.ObjectSyncMessage
|
||||||
msg, err = stream.Recv()
|
msg, err = stream.Recv()
|
||||||
s.lastUsage.Store(time.Now().Unix())
|
s.lastUsage.Store(time.Now().Unix())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
stream.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg.ReplyId != "" {
|
if msg.ReplyId != "" {
|
||||||
// then we can send it directly to waiters without adding to queue or starting a reader
|
// then we can send it directly to waiters without adding to queue or starting a reader
|
||||||
s.waitersMx.Lock()
|
if stopWaiter(msg) {
|
||||||
waiter, exists := s.waiters[msg.ReplyId]
|
|
||||||
if exists {
|
|
||||||
delete(s.waiters, msg.ReplyId)
|
|
||||||
s.waitersMx.Unlock()
|
|
||||||
waiter.ch <- msg
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
s.waitersMx.Unlock()
|
|
||||||
|
|
||||||
log.With(zap.String("replyId", msg.ReplyId)).Debug("reply id does not exist")
|
log.With(zap.String("replyId", msg.ReplyId)).Debug("reply id does not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
mx.Lock()
|
queue.Send(func() error {
|
||||||
queue = append(queue, msg)
|
return process(msg)
|
||||||
if len(queue) > maxWaitMsgs {
|
})
|
||||||
queue = queue[len(queue)-maxWaitMsgs:]
|
|
||||||
}
|
|
||||||
// if we already have max goroutines reading the queue in parallel
|
|
||||||
if readers >= maxStreamReaders {
|
|
||||||
mx.Unlock()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
readers++
|
|
||||||
mx.Unlock()
|
|
||||||
|
|
||||||
// starting another reader
|
|
||||||
go readQueue()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user