Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaef0e79

Browse files
committed
docs: Fix docs and examples related to r.Context() usage
Fixes#474
1 parent3dd723a commitaef0e79

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
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
defercancel()
6870

6971
varvinterface{}

‎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
funcAccept(w http.ResponseWriter,r*http.Request,opts*AcceptOptions) (*Conn,error) {
8386
returnaccept(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
iferrors.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
varmu sync.Mutex
116116
varc*websocket.Conn
117117
varclosedbool
@@ -142,7 +142,7 @@ func (cs *chatServer) subscribe(ctx context.Context, w http.ResponseWriter, r *h
142142
mu.Unlock()
143143
deferc.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: 2 additions & 2 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
ifwebsocket.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-
funcecho(ctx context.Context,c*websocket.Conn,l*rate.Limiter)error {
54+
funcecho(c*websocket.Conn,l*rate.Limiter)error {
5555
ctx,cancel:=context.WithTimeout(ctx,time.Second*10)
5656
defercancel()
5757

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp