good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
websocket
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open
websocket
Commits
af0fd9d4
Unverified
Commit
af0fd9d4
authored
Oct 19, 2023
by
Anmol Sethi
Browse files
Options
Downloads
Patches
Plain Diff
examples/chat: Fix race condition
Tricky tricky.
parent
ff3ea39b
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
internal/examples/chat/chat.go
+27
-12
27 additions, 12 deletions
internal/examples/chat/chat.go
with
27 additions
and
12 deletions
internal/examples/chat/chat.go
+
27
−
12
View file @
af0fd9d4
...
@@ -5,6 +5,7 @@ import (
...
@@ -5,6 +5,7 @@ import (
"errors"
"errors"
"io"
"io"
"log"
"log"
"net"
"net/http"
"net/http"
"sync"
"sync"
"time"
"time"
...
@@ -69,14 +70,7 @@ func (cs *chatServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
...
@@ -69,14 +70,7 @@ func (cs *chatServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// subscribeHandler accepts the WebSocket connection and then subscribes
// subscribeHandler accepts the WebSocket connection and then subscribes
// it to all future messages.
// it to all future messages.
func
(
cs
*
chatServer
)
subscribeHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
cs
*
chatServer
)
subscribeHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
c
,
err
:=
websocket
.
Accept
(
w
,
r
,
nil
)
err
:=
cs
.
subscribe
(
r
.
Context
(),
w
,
r
)
if
err
!=
nil
{
cs
.
logf
(
"%v"
,
err
)
return
}
defer
c
.
CloseNow
()
err
=
cs
.
subscribe
(
r
.
Context
(),
c
)
if
errors
.
Is
(
err
,
context
.
Canceled
)
{
if
errors
.
Is
(
err
,
context
.
Canceled
)
{
return
return
}
}
...
@@ -117,18 +111,39 @@ func (cs *chatServer) publishHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -117,18 +111,39 @@ func (cs *chatServer) publishHandler(w http.ResponseWriter, r *http.Request) {
//
//
// It uses CloseRead to keep reading from the connection to process control
// It uses CloseRead to keep reading from the connection to process control
// messages and cancel the context if the connection drops.
// messages and cancel the context if the connection drops.
func
(
cs
*
chatServer
)
subscribe
(
ctx
context
.
Context
,
c
*
websocket
.
Conn
)
error
{
func
(
cs
*
chatServer
)
subscribe
(
ctx
context
.
Context
,
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
error
{
ctx
=
c
.
CloseRead
(
ctx
)
var
mu
sync
.
Mutex
var
c
*
websocket
.
Conn
var
closed
bool
s
:=
&
subscriber
{
s
:=
&
subscriber
{
msgs
:
make
(
chan
[]
byte
,
cs
.
subscriberMessageBuffer
),
msgs
:
make
(
chan
[]
byte
,
cs
.
subscriberMessageBuffer
),
closeSlow
:
func
()
{
closeSlow
:
func
()
{
mu
.
Lock
()
defer
mu
.
Unlock
()
closed
=
true
if
c
!=
nil
{
c
.
Close
(
websocket
.
StatusPolicyViolation
,
"connection too slow to keep up with messages"
)
c
.
Close
(
websocket
.
StatusPolicyViolation
,
"connection too slow to keep up with messages"
)
}
},
},
}
}
cs
.
addSubscriber
(
s
)
cs
.
addSubscriber
(
s
)
defer
cs
.
deleteSubscriber
(
s
)
defer
cs
.
deleteSubscriber
(
s
)
c2
,
err
:=
websocket
.
Accept
(
w
,
r
,
nil
)
if
err
!=
nil
{
return
err
}
mu
.
Lock
()
if
closed
{
mu
.
Unlock
()
return
net
.
ErrClosed
}
c
=
c2
mu
.
Unlock
()
defer
c
.
CloseNow
()
ctx
=
c
.
CloseRead
(
ctx
)
for
{
for
{
select
{
select
{
case
msg
:=
<-
s
.
msgs
:
case
msg
:=
<-
s
.
msgs
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment