Skip to content

Commit

Permalink
fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Sep 24, 2024
1 parent ef3835c commit b217f09
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ require (
go.opentelemetry.io/otel v1.23.1 // indirect
go.opentelemetry.io/otel/metric v1.23.1 // indirect
go.opentelemetry.io/otel/trace v1.23.1 // indirect
go.uber.org/goleak v1.3.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/net v0.21.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ go.opentelemetry.io/proto/otlp v0.9.0 h1:C0g6TWmQYvjKRnljRULLWUVJGy8Uvu0NEL/5frY
go.opentelemetry.io/proto/otlp v0.9.0/go.mod h1:1vKfU9rv61e9EVGthD1zNvUbiwPcimSsOPU9brfSHJg=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.17.0 h1:MTjgFu6ZLKvY6Pvaqk97GlxNBuMpV4Hy/3P6tRGlI2U=
Expand Down
24 changes: 13 additions & 11 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,19 +251,21 @@ func (s *Server) doReq(ctx context.Context, ws *WebSocket, request []json.RawMes
filter.Limit = 9999999999
}
i := 0
for event := range events {
if s.options.skipEventFunc != nil && s.options.skipEventFunc(event) {
continue
}
ws.WriteJSON(nostr.EventEnvelope{SubscriptionID: &id, Event: *event})
i++
if i > filter.Limit {
break
if events != nil {
for event := range events {
if s.options.skipEventFunc != nil && s.options.skipEventFunc(event) {
continue
}
ws.WriteJSON(nostr.EventEnvelope{SubscriptionID: &id, Event: *event})
i++
if i > filter.Limit {
break
}
}
}

// exhaust the channel (in case we broke out of it early) so it is closed by the storage
for range events {
// exhaust the channel (in case we broke out of it early) so it is closed by the storage
for range events {
}
}
}

Expand Down
30 changes: 28 additions & 2 deletions start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package relayer
import (
"context"
"errors"
"fmt"
"net/http"
"testing"
"time"

"github.com/fiatjaf/eventstore/slicestore"
"github.com/gobwas/ws/wsutil"
"github.com/nbd-wtf/go-nostr"
"go.uber.org/goleak"
)

func TestServerStartShutdown(t *testing.T) {
defer goleak.VerifyNone(t)
var (
inited bool
storeInited bool
Expand All @@ -29,6 +33,7 @@ func TestServerStartShutdown(t *testing.T) {
},
}
srv, _ := NewServer(rl)
defer srv.Shutdown(context.TODO())
ready := make(chan bool)
done := make(chan error)
go func() { done <- srv.Start("127.0.0.1", 0, ready); close(done) }()
Expand All @@ -43,8 +48,10 @@ func TestServerStartShutdown(t *testing.T) {
}

// check that http requests are served
if _, err := http.Get("http://" + srv.Addr); err != nil {
if resp, err := http.Get("http://" + srv.Addr); err != nil {
t.Errorf("GET %s: %v", srv.Addr, err)
} else {
resp.Body.Close()
}

// verify server shuts down
Expand All @@ -65,8 +72,10 @@ func TestServerStartShutdown(t *testing.T) {
}

func TestServerShutdownWebsocket(t *testing.T) {
defer goleak.VerifyNone(t)
// set up a new relay server
srv := startTestRelay(t, &testRelay{storage: &testStorage{}})
srv := startTestRelay(t, &testRelay{storage: &slicestore.SliceStore{}})
defer srv.Shutdown(context.TODO())

// connect a client to it
ctx1, cancel := context.WithTimeout(context.Background(), 2*time.Second)
Expand All @@ -76,6 +85,23 @@ func TestServerShutdownWebsocket(t *testing.T) {
t.Fatalf("nostr.RelayConnectContext: %v", err)
}

sk := nostr.GeneratePrivateKey()

var ev nostr.Event
ev.Kind = nostr.KindTextNote
ev.Content = "test"
ev.CreatedAt = nostr.Now()
ev.Sign(sk)
client.Publish(ctx1, ev)

var filter nostr.Filter
filter.Kinds = []int{nostr.KindTextNote}
evs, err := client.QuerySync(ctx1, filter)
if err != nil {
t.Fatalf("client.QuerySync: %v", err)
}
fmt.Println(evs)

// now, shut down the server
ctx2, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
Expand Down

0 comments on commit b217f09

Please sign in to comment.