Skip to content

Commit

Permalink
docs: Fix docs and examples related to r.Context() usage
Browse files Browse the repository at this point in the history
Fixes #474
  • Loading branch information
mafredri committed Sep 6, 2024
1 parent 3dd723a commit f77d0a4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
}
defer c.CloseNow()

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

var v interface{}
Expand Down
3 changes: 3 additions & 0 deletions accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func (opts *AcceptOptions) cloneWithDefaults() *AcceptOptions {
// See the InsecureSkipVerify and OriginPatterns options to allow cross origin requests.
//
// Accept will write a response to w on all errors.
//
// Note that using the http.Request Context after Accept returns may lead to
// unexpected behavior (see http.Hijacker).
func Accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn, error) {
return accept(w, r, opts)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/examples/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (cs *chatServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// subscribeHandler accepts the WebSocket connection and then subscribes
// it to all future messages.
func (cs *chatServer) subscribeHandler(w http.ResponseWriter, r *http.Request) {
err := cs.subscribe(r.Context(), w, r)
err := cs.subscribe(w, r)
if errors.Is(err, context.Canceled) {
return
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func (cs *chatServer) publishHandler(w http.ResponseWriter, r *http.Request) {
//
// It uses CloseRead to keep reading from the connection to process control
// messages and cancel the context if the connection drops.
func (cs *chatServer) subscribe(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
func (cs *chatServer) subscribe(w http.ResponseWriter, r *http.Request) error {
var mu sync.Mutex
var c *websocket.Conn
var closed bool
Expand Down Expand Up @@ -142,7 +142,7 @@ func (cs *chatServer) subscribe(ctx context.Context, w http.ResponseWriter, r *h
mu.Unlock()
defer c.CloseNow()

ctx = c.CloseRead(ctx)
ctx := c.CloseRead(context.Background())

for {
select {
Expand Down
6 changes: 3 additions & 3 deletions internal/examples/echo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s echoServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {

l := rate.NewLimiter(rate.Every(time.Millisecond*100), 10)
for {
err = echo(r.Context(), c, l)
err = echo(c, l)
if websocket.CloseStatus(err) == websocket.StatusNormalClosure {
return
}
Expand All @@ -51,8 +51,8 @@ func (s echoServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// echo reads from the WebSocket connection and then writes
// the received message back to it.
// The entire function has 10s to complete.
func echo(ctx context.Context, c *websocket.Conn, l *rate.Limiter) error {
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
func echo(c *websocket.Conn, l *rate.Limiter) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

err := l.Wait(ctx)
Expand Down

0 comments on commit f77d0a4

Please sign in to comment.