From 65bd239567abc221cdc2f290b6a1fb3168361349 Mon Sep 17 00:00:00 2001 From: mleku Date: Wed, 3 Jan 2024 06:57:43 +0000 Subject: [PATCH] relay errors --- pkg/relay/serve-req.go | 6 +++--- pkg/relay/start.go | 23 ++++++++++------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/pkg/relay/serve-req.go b/pkg/relay/serve-req.go index 374062fc..d64feabd 100644 --- a/pkg/relay/serve-req.go +++ b/pkg/relay/serve-req.go @@ -34,9 +34,9 @@ func (rl *Relay) handleRequest(ctx context.Context, id nip1.SubscriptionID, // but we might be fetching stuff from multiple places) eose.Add(len(rl.QueryEvents)) for _, query := range rl.QueryEvents { - ch, err := query(ctx, filter) - if err != nil { - rl.Log.D.Chk(ws.WriteJSON(nip1.NoticeEnvelope{Text: err.Error()})) + var ch chan *nip1.Event + if ch, e = query(ctx, filter); rl.Log.E.Chk(e) { + rl.Log.D.Chk(ws.WriteJSON(nip1.NoticeEnvelope{Text: e.Error()})) eose.Done() continue } diff --git a/pkg/relay/start.go b/pkg/relay/start.go index 241ff6f9..3ea4b3fe 100644 --- a/pkg/relay/start.go +++ b/pkg/relay/start.go @@ -2,6 +2,7 @@ package relay import ( "context" + "errors" "net" "net/http" "strconv" @@ -18,11 +19,10 @@ func (rl *Relay) Router() *http.ServeMux { // Start creates an http server and starts listening on given host and port. func (rl *Relay) Start(host string, port int, started ...chan bool) (e error) { addr := net.JoinHostPort(host, strconv.Itoa(port)) - ln, err := net.Listen("tcp", addr) - if err != nil { - return err + var ln net.Listener + if ln, e = net.Listen("tcp", addr); rl.Log.E.Chk(e) { + return } - rl.Addr = ln.Addr().String() rl.httpServer = &http.Server{ Handler: cors.Default().Handler(rl), @@ -31,16 +31,14 @@ func (rl *Relay) Start(host string, port int, started ...chan bool) (e error) { ReadTimeout: 2 * time.Second, IdleTimeout: 30 * time.Second, } - // notify caller that we're starting for _, started := range started { close(started) } - - if err := rl.httpServer.Serve(ln); err == http.ErrServerClosed { + if e = rl.httpServer.Serve(ln); errors.Is(e, http.ErrServerClosed) { return nil - } else if err != nil { - return err + } else if rl.Log.E.Chk(e) { + return } else { return nil } @@ -48,11 +46,10 @@ func (rl *Relay) Start(host string, port int, started ...chan bool) (e error) { // Shutdown sends a websocket close control message to all connected clients. func (rl *Relay) Shutdown(ctx context.Context) { - rl.httpServer.Shutdown(ctx) - + rl.Log.E.Chk(rl.httpServer.Shutdown(ctx)) rl.clients.Range(func(conn *websocket.Conn, _ struct{}) bool { - conn.WriteControl(websocket.CloseMessage, nil, time.Now().Add(time.Second)) - conn.Close() + rl.Log.E.Chk(conn.WriteControl(websocket.CloseMessage, nil, time.Now().Add(time.Second))) + rl.Log.E.Chk(conn.Close()) rl.clients.Delete(conn) return true })