Commit 1b29e2c9 authored by Janoš Guljaš's avatar Janoš Guljaš Committed by GitHub

perf(pullsync): release reader buffer in handler (#1917)

parent 224cdae9
...@@ -260,7 +260,7 @@ func (s *Syncer) SyncInterval(ctx context.Context, peer swarm.Address, bin uint8 ...@@ -260,7 +260,7 @@ func (s *Syncer) SyncInterval(ctx context.Context, peer swarm.Address, bin uint8
// handler handles an incoming request to sync an interval // handler handles an incoming request to sync an interval
func (s *Syncer) handler(ctx context.Context, p p2p.Peer, stream p2p.Stream) (err error) { func (s *Syncer) handler(ctx context.Context, p p2p.Peer, stream p2p.Stream) (err error) {
w, r := protobuf.NewWriterAndReader(stream) r := protobuf.NewReader(stream)
defer func() { defer func() {
if err != nil { if err != nil {
_ = stream.Reset() _ = stream.Reset()
...@@ -311,6 +311,11 @@ func (s *Syncer) handler(ctx context.Context, p p2p.Peer, stream p2p.Stream) (er ...@@ -311,6 +311,11 @@ func (s *Syncer) handler(ctx context.Context, p p2p.Peer, stream p2p.Stream) (er
return fmt.Errorf("make offer: %w", err) return fmt.Errorf("make offer: %w", err)
} }
// recreate the reader to allow the first one to be garbage collected
// before the makeOffer function call, to reduce the total memory allocated
// while makeOffer is executing (waiting for the new chunks)
w, r := protobuf.NewWriterAndReader(stream)
if err := w.WriteMsgWithContext(ctx, offer); err != nil { if err := w.WriteMsgWithContext(ctx, offer); err != nil {
return fmt.Errorf("write offer: %w", err) return fmt.Errorf("write offer: %w", err)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment