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

Commitaf0fd9d

Browse files
committed
examples/chat: Fix race condition
Tricky tricky.
1 parentff3ea39 commitaf0fd9d

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

‎internal/examples/chat/chat.go‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"io"
77
"log"
8+
"net"
89
"net/http"
910
"sync"
1011
"time"
@@ -69,14 +70,7 @@ func (cs *chatServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6970
// subscribeHandler accepts the WebSocket connection and then subscribes
7071
// it to all future messages.
7172
func (cs*chatServer)subscribeHandler(w http.ResponseWriter,r*http.Request) {
72-
c,err:=websocket.Accept(w,r,nil)
73-
iferr!=nil {
74-
cs.logf("%v",err)
75-
return
76-
}
77-
deferc.CloseNow()
78-
79-
err=cs.subscribe(r.Context(),c)
73+
err:=cs.subscribe(r.Context(),w,r)
8074
iferrors.Is(err,context.Canceled) {
8175
return
8276
}
@@ -117,18 +111,39 @@ func (cs *chatServer) publishHandler(w http.ResponseWriter, r *http.Request) {
117111
//
118112
// It uses CloseRead to keep reading from the connection to process control
119113
// messages and cancel the context if the connection drops.
120-
func (cs*chatServer)subscribe(ctx context.Context,c*websocket.Conn)error {
121-
ctx=c.CloseRead(ctx)
122-
114+
func (cs*chatServer)subscribe(ctx context.Context,w http.ResponseWriter,r*http.Request)error {
115+
varmu sync.Mutex
116+
varc*websocket.Conn
117+
varclosedbool
123118
s:=&subscriber{
124119
msgs:make(chan []byte,cs.subscriberMessageBuffer),
125120
closeSlow:func() {
126-
c.Close(websocket.StatusPolicyViolation,"connection too slow to keep up with messages")
121+
mu.Lock()
122+
defermu.Unlock()
123+
closed=true
124+
ifc!=nil {
125+
c.Close(websocket.StatusPolicyViolation,"connection too slow to keep up with messages")
126+
}
127127
},
128128
}
129129
cs.addSubscriber(s)
130130
defercs.deleteSubscriber(s)
131131

132+
c2,err:=websocket.Accept(w,r,nil)
133+
iferr!=nil {
134+
returnerr
135+
}
136+
mu.Lock()
137+
ifclosed {
138+
mu.Unlock()
139+
returnnet.ErrClosed
140+
}
141+
c=c2
142+
mu.Unlock()
143+
deferc.CloseNow()
144+
145+
ctx=c.CloseRead(ctx)
146+
132147
for {
133148
select {
134149
casemsg:=<-s.msgs:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp