Skip to content

Commit aef0e79

Browse files
committed
docs: Fix docs and examples related to r.Context() usage
Fixes #474
1 parent 3dd723a commit aef0e79

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
6363
}
6464
defer c.CloseNow()
6565

66-
ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
66+
// Set the context as needed. Use of r.Context() is not recommended
67+
// to avoid surprising behavior (see http.Hijacker).
68+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
6769
defer cancel()
6870

6971
var v interface{}

accept.go

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ func (opts *AcceptOptions) cloneWithDefaults() *AcceptOptions {
7979
// See the InsecureSkipVerify and OriginPatterns options to allow cross origin requests.
8080
//
8181
// Accept will write a response to w on all errors.
82+
//
83+
// Note that using the http.Request Context after Accept returns may lead to
84+
// unexpected behavior (see http.Hijacker).
8285
func Accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn, error) {
8386
return accept(w, r, opts)
8487
}

internal/examples/chat/chat.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (cs *chatServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7070
// subscribeHandler accepts the WebSocket connection and then subscribes
7171
// it to all future messages.
7272
func (cs *chatServer) subscribeHandler(w http.ResponseWriter, r *http.Request) {
73-
err := cs.subscribe(r.Context(), w, r)
73+
err := cs.subscribe(w, r)
7474
if errors.Is(err, context.Canceled) {
7575
return
7676
}
@@ -111,7 +111,7 @@ func (cs *chatServer) publishHandler(w http.ResponseWriter, r *http.Request) {
111111
//
112112
// It uses CloseRead to keep reading from the connection to process control
113113
// messages and cancel the context if the connection drops.
114-
func (cs *chatServer) subscribe(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
114+
func (cs *chatServer) subscribe(w http.ResponseWriter, r *http.Request) error {
115115
var mu sync.Mutex
116116
var c *websocket.Conn
117117
var closed bool
@@ -142,7 +142,7 @@ func (cs *chatServer) subscribe(ctx context.Context, w http.ResponseWriter, r *h
142142
mu.Unlock()
143143
defer c.CloseNow()
144144

145-
ctx = c.CloseRead(ctx)
145+
ctx := c.CloseRead(context.Background())
146146

147147
for {
148148
select {

internal/examples/echo/server.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (s echoServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3737

3838
l := rate.NewLimiter(rate.Every(time.Millisecond*100), 10)
3939
for {
40-
err = echo(r.Context(), c, l)
40+
err = echo(c, l)
4141
if websocket.CloseStatus(err) == websocket.StatusNormalClosure {
4242
return
4343
}
@@ -51,7 +51,7 @@ func (s echoServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5151
// echo reads from the WebSocket connection and then writes
5252
// the received message back to it.
5353
// The entire function has 10s to complete.
54-
func echo(ctx context.Context, c *websocket.Conn, l *rate.Limiter) error {
54+
func echo(c *websocket.Conn, l *rate.Limiter) error {
5555
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
5656
defer cancel()
5757

0 commit comments

Comments
 (0)