Skip to content

Commit

Permalink
relay errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mleku committed Jan 3, 2024
1 parent 14a63e3 commit 65bd239
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
6 changes: 3 additions & 3 deletions pkg/relay/serve-req.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 10 additions & 13 deletions pkg/relay/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package relay

import (
"context"
"errors"
"net"
"net/http"
"strconv"
Expand All @@ -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),
Expand All @@ -31,28 +31,25 @@ 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
}
}

// 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
})
Expand Down

0 comments on commit 65bd239

Please sign in to comment.