From 84290910eaaa5b44f1aaaf11a081fcdf371eb528 Mon Sep 17 00:00:00 2001 From: Pavel Tatarskiy Date: Wed, 30 Oct 2024 18:55:47 +0300 Subject: [PATCH] fix go routines leak --- go.mod | 2 +- server/services/stat.go | 3 +++ server/services/stat_web.go | 9 +++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 75540f0..1124244 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/anacrolix/chansync v0.6.0 // indirect github.com/anacrolix/log v0.16.0 github.com/anacrolix/sync v0.5.2 // indirect - github.com/aws/aws-sdk-go v1.44.163 github.com/bakins/logrus-middleware v0.0.0-20180426214643-ce4c6f8deb07 github.com/bakins/test-helpers v0.0.0-20141028124846-af83df64dc31 // indirect github.com/dustin/go-humanize v1.0.1 @@ -55,6 +54,7 @@ require ( github.com/anacrolix/stm v0.5.0 // indirect github.com/anacrolix/upnp v0.1.4 // indirect github.com/anacrolix/utp v0.2.0 // indirect + github.com/aws/aws-sdk-go v1.44.163 // indirect github.com/bahlo/generic-list-go v0.2.0 // indirect github.com/benbjohnson/immutable v0.4.3 // indirect github.com/beorn7/perks v1.0.1 // indirect diff --git a/server/services/stat.go b/server/services/stat.go index aa036d4..3ac58b3 100644 --- a/server/services/stat.go +++ b/server/services/stat.go @@ -229,6 +229,9 @@ func (s *Stat) StatStream(in *pb.StatRequest, stream pb.TorrentWebSeeder_StatStr log.WithField("path", in.GetPath()).Info("sending stats completed") } return err + case <-time.After(30 * time.Minute): + log.WithField("path", in.GetPath()).Info("sending stats timeout") + return nil case err := <-errCh: if err != nil { return status.Errorf(codes.Internal, "got error=%v", err) diff --git a/server/services/stat_web.go b/server/services/stat_web.go index 97cc2cc..097de03 100644 --- a/server/services/stat_web.go +++ b/server/services/stat_web.go @@ -48,8 +48,13 @@ func (s *StatWeb) Serve(w http.ResponseWriter, r *http.Request, h string, p stri stream := NewStatStreamServer(ctx, ha, f) ticker := time.NewTicker(10 * time.Second) go func() { - for range ticker.C { - stream.Ping() + for { + select { + case <-ticker.C: + stream.Ping() + case <-ctx.Done(): + return + } } }() err := s.st.StatStream(&pb.StatRequest{Path: p}, stream)