Skip to content

Commit 6c8e3ab

Browse files
authored
docs: Fix docs and examples related to r.Context() usage (#477)
Contributes to #474
1 parent faf23b7 commit 6c8e3ab

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

README.md

Lines changed: 3 additions & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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,8 +51,8 @@ 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 {
55-
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
54+
func echo(c *websocket.Conn, l *rate.Limiter) error {
55+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
5656
defer cancel()
5757

5858
err := l.Wait(ctx)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy