From 75cce1836dcf88d247f821ff039959c7235c569c Mon Sep 17 00:00:00 2001 From: Termina1 Date: Mon, 23 Sep 2024 18:42:05 +0300 Subject: [PATCH] try to optimize selectgo --- protocol/peer.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/protocol/peer.go b/protocol/peer.go index 7e93fb9..fb10ba4 100644 --- a/protocol/peer.go +++ b/protocol/peer.go @@ -41,17 +41,28 @@ func (p *Peer) keepRead(ctx context.Context) error { time := time.After(p.readAccumtTimeLimit) var buff Records for len(buff) <= p.readBatchSize { - select { - case <-time: - break - case <-ctx.Done(): + if len(time) > 0 || ctx.Err() != nil { break - case recs, ok := <-reading: - // closed + } + if len(reading) > 0 { + recs, ok := <-reading if !ok { break } buff = append(buff, recs...) + } else { + select { + case <-time: + break + case <-ctx.Done(): + break + case recs, ok := <-reading: + // closed + if !ok { + break + } + buff = append(buff, recs...) + } } } if err := p.inout.Drain(ctx, buff); err != nil {