Skip to content

Commit d008872

Browse files
authored
refactor: rename ServeHandle (#69)
A quick rename, because this high level API could be used for either the client or the server end of a websocket connection.
1 parent 912ea1b commit d008872

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

autobahn_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestAutobahn(t *testing.T) {
9797
http.Error(w, err.Error(), http.StatusBadRequest)
9898
return
9999
}
100-
_ = ws.Serve(r.Context(), websocket.EchoHandler)
100+
_ = ws.Handle(r.Context(), websocket.EchoHandler)
101101
}))
102102
defer srv.Close()
103103
targetURL = srv.URL

examples/benchserver/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func main() {
6161
logger.ErrorContext(r.Context(), "websocket handshake failed", "err", err)
6262
return
6363
}
64-
ws.Serve(r.Context(), websocket.EchoHandler)
64+
ws.Handle(r.Context(), websocket.EchoHandler)
6565
})
6666

6767
// 3. JSON message test
@@ -82,7 +82,7 @@ func main() {
8282
logger.ErrorContext(r.Context(), "websocket handshake failed", "err", err)
8383
return
8484
}
85-
ws.Serve(r.Context(), func(_ context.Context, msg *websocket.Message) (*websocket.Message, error) {
85+
ws.Handle(r.Context(), func(_ context.Context, msg *websocket.Message) (*websocket.Message, error) {
8686
var m jsonMessage
8787
if err := json.Unmarshal(msg.Payload, &m); err != nil {
8888
return nil, err
@@ -111,7 +111,7 @@ func main() {
111111
logger.ErrorContext(r.Context(), "websocket handshake failed", "err", err)
112112
return
113113
}
114-
ws.Serve(r.Context(), func(_ context.Context, msg *websocket.Message) (*websocket.Message, error) {
114+
ws.Handle(r.Context(), func(_ context.Context, msg *websocket.Message) (*websocket.Message, error) {
115115
if len(msg.Payload) != 4 {
116116
return nil, fmt.Errorf("invalid payload length: %d", len(msg.Payload))
117117
}

examples/echoserver/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func main() {
4949
return
5050
}
5151
logger.InfoContext(r.Context(), "websocket handshake completed, starting echo handler", "client-key", ws.ClientKey())
52-
ws.Serve(r.Context(), websocket.EchoHandler)
52+
ws.Handle(r.Context(), websocket.EchoHandler)
5353
})
5454

5555
if pprof {

websocket.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,27 +291,27 @@ func (ws *Websocket) writeFrame(frame *Frame) error {
291291
return nil
292292
}
293293

294-
// Serve is a high-level convienience method for request-response style
294+
// Handle is a high-level convienience method for request-response style
295295
// websocket connections, where the given [Handler] is called for each
296296
// incoming message read from the peer.
297297
//
298298
// If the handler returns a non-nil message, that message is written back
299-
// to the peer. Nil messages indicate that there is no reply and are skipped.
299+
// to the peer. A nil message indicates that there is no reply to send.
300300
//
301301
// If the handler returns a non-nil error, its message will be sent to the
302302
// client as the start of the two-way closing handshake with a status of
303-
// internal error. NOTE: This means errors returned by the handler should not
303+
// Internal Error. NOTE: This means errors returned by the handler should not
304304
// contain sensitive information that should not be exposed to peers.
305305
//
306306
// If there is an error reading from or writing to the underlying connection,
307307
// the connection is assumed to be unrecoverable and is closed immediately,
308308
// without waiting for the full two-way handshake. For more control over
309309
// error handling and closing behavior, use [ReadMessage], [WriteMessage], and
310-
// [Close] directly.
310+
// [CloseWithStatus] directly.
311311
//
312312
// See also [EchoHandler], a minimal handler that echoes each incoming message
313313
// verbatim.
314-
func (ws *Websocket) Serve(ctx context.Context, handler Handler) error {
314+
func (ws *Websocket) Handle(ctx context.Context, handler Handler) error {
315315
for {
316316
msg, err := ws.ReadMessage(ctx)
317317
if err != nil {
@@ -497,7 +497,7 @@ func (ws *Websocket) ClientKey() ClientKey {
497497
}
498498

499499
// Handler handles a single websocket [Message] as part of the high level
500-
// [Serve] request-response API.
500+
// [Handle] request-response API.
501501
//
502502
// If the returned message is non-nil, it will be sent to the client. If an
503503
// error is returned, the connection will be closed.

websocket_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func TestHandshake(t *testing.T) {
128128
http.Error(w, err.Error(), http.StatusBadRequest)
129129
return
130130
}
131-
_ = ws.Serve(r.Context(), websocket.EchoHandler)
131+
_ = ws.Handle(r.Context(), websocket.EchoHandler)
132132
}))
133133
defer srv.Close()
134134

@@ -229,7 +229,7 @@ func TestConnectionLimits(t *testing.T) {
229229
},
230230
serverTest: func(t testing.TB, ws *websocket.Websocket, _ net.Conn) {
231231
start := time.Now()
232-
err := ws.Serve(t.Context(), websocket.EchoHandler)
232+
err := ws.Handle(t.Context(), websocket.EchoHandler)
233233
elapsed := time.Since(start)
234234
assert.Equal(t, elapsed >= maxDuration, true, "not enough time passed")
235235
assert.Error(t, err, os.ErrDeadlineExceeded)
@@ -374,7 +374,7 @@ func TestProtocolOkay(t *testing.T) {
374374

375375
// server just runs EchoHandler to reply to client
376376
serverTest: func(t testing.TB, ws *websocket.Websocket, _ net.Conn) {
377-
assert.NilError(t, ws.Serve(t.Context(), websocket.EchoHandler))
377+
assert.NilError(t, ws.Handle(t.Context(), websocket.EchoHandler))
378378
},
379379
}.Run(t)
380380
})
@@ -459,7 +459,7 @@ func TestProtocolOkay(t *testing.T) {
459459

460460
// server just echoes messages from the client
461461
serverTest: func(t testing.TB, ws *websocket.Websocket, _ net.Conn) {
462-
assert.NilError(t, ws.Serve(t.Context(), websocket.EchoHandler))
462+
assert.NilError(t, ws.Handle(t.Context(), websocket.EchoHandler))
463463
},
464464
}.Run(t)
465465
})
@@ -482,7 +482,7 @@ func TestProtocolOkay(t *testing.T) {
482482
},
483483
// server just echoes messages from the client
484484
serverTest: func(t testing.TB, ws *websocket.Websocket, _ net.Conn) {
485-
assert.NilError(t, ws.Serve(t.Context(), websocket.EchoHandler))
485+
assert.NilError(t, ws.Handle(t.Context(), websocket.EchoHandler))
486486
},
487487
}.Run(t)
488488
})
@@ -606,7 +606,7 @@ func TestProtocolErrors(t *testing.T) {
606606
// protocol errors automatically
607607
serverOpts: newOpts(t),
608608
serverTest: func(t testing.TB, ws *websocket.Websocket, conn net.Conn) {
609-
assert.Error(t, ws.Serve(t.Context(), websocket.EchoHandler), tc.wantCloseReason)
609+
assert.Error(t, ws.Handle(t.Context(), websocket.EchoHandler), tc.wantCloseReason)
610610
},
611611
}.Run(t)
612612
})
@@ -675,7 +675,7 @@ func TestCloseFrameValidation(t *testing.T) {
675675
// server just runs echo handler, which should process all
676676
// protocol errors automatically
677677
serverTest: func(t testing.TB, ws *websocket.Websocket, conn net.Conn) {
678-
assert.Error(t, ws.Serve(t.Context(), websocket.EchoHandler), tc.wantErr)
678+
assert.Error(t, ws.Handle(t.Context(), websocket.EchoHandler), tc.wantErr)
679679
},
680680
}.Run(t)
681681
})
@@ -916,7 +916,7 @@ func TestErrorHandling(t *testing.T) {
916916
}
917917
},
918918
serverTest: func(t testing.TB, ws *websocket.Websocket, _ net.Conn) {
919-
err := ws.Serve(t.Context(), websocket.EchoHandler)
919+
err := ws.Handle(t.Context(), websocket.EchoHandler)
920920
assert.Error(t, err, writeErr)
921921
},
922922
}.Run(t)
@@ -941,7 +941,7 @@ func TestErrorHandling(t *testing.T) {
941941
// server tries to read message with the cancelable context, which
942942
// should be canceled while waiting for the continuation frame.
943943
serverTest: func(t testing.TB, ws *websocket.Websocket, _ net.Conn) {
944-
assert.Error(t, ws.Serve(ctx, websocket.EchoHandler), context.Canceled)
944+
assert.Error(t, ws.Handle(ctx, websocket.EchoHandler), context.Canceled)
945945
},
946946
}.Run(t)
947947
})
@@ -1143,7 +1143,7 @@ func TestServeLoop(t *testing.T) {
11431143
// server runs Serve with a custom handler that returns an error
11441144
// when it receives a "fail" message
11451145
serverTest: func(t testing.TB, ws *websocket.Websocket, _ net.Conn) {
1146-
err := ws.Serve(t.Context(), func(ctx context.Context, msg *websocket.Message) (*websocket.Message, error) {
1146+
err := ws.Handle(t.Context(), func(ctx context.Context, msg *websocket.Message) (*websocket.Message, error) {
11471147
if bytes.Equal(msg.Payload, []byte("fail")) {
11481148
return nil, wantErr
11491149
}
@@ -1179,7 +1179,7 @@ func TestServeLoop(t *testing.T) {
11791179
}
11801180
},
11811181
serverTest: func(t testing.TB, ws *websocket.Websocket, conn net.Conn) {
1182-
err := ws.Serve(t.Context(), func(ctx context.Context, msg *websocket.Message) (*websocket.Message, error) {
1182+
err := ws.Handle(t.Context(), func(ctx context.Context, msg *websocket.Message) (*websocket.Message, error) {
11831183
return nil, appErr
11841184
})
11851185
// the error returned wraps both the application-level error
@@ -1566,7 +1566,7 @@ var (
15661566
// ============================================================================
15671567
// Examples
15681568
// ============================================================================
1569-
func ExampleServe() {
1569+
func ExampleHandle() {
15701570
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
15711571
ws, err := websocket.Accept(w, r, websocket.Options{
15721572
ReadTimeout: 500 * time.Millisecond,
@@ -1578,10 +1578,10 @@ func ExampleServe() {
15781578
http.Error(w, err.Error(), http.StatusBadRequest)
15791579
return
15801580
}
1581-
if err := ws.Serve(r.Context(), websocket.EchoHandler); err != nil {
1582-
// an error returned by Serve is strictly informational; the
1581+
if err := ws.Handle(r.Context(), websocket.EchoHandler); err != nil {
1582+
// an error returned by Handle is strictly informational; the
15831583
// connection will already be closed at this point.
1584-
log.Printf("error serving websocket connection: %s", err)
1584+
log.Printf("error handling websocket connection: %s", err)
15851585
}
15861586
})
15871587
if err := http.ListenAndServe(":8080", handler); err != nil {

0 commit comments

Comments
 (0)