Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,18 @@ func (c *Conn) WriteControl(messageType int, data []byte, deadline time.Time) er
}
}

timer := time.NewTimer(d)
select {
case <-c.mu:
timer.Stop()
case <-timer.C:
return errWriteTimeout
default:
timer := time.NewTimer(d)
select {
case <-c.mu:
timer.Stop()
case <-timer.C:
return errWriteTimeout
}
}

defer func() { c.mu <- struct{}{} }()

c.writeErrMu.Lock()
Expand Down
25 changes: 25 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,31 @@ func TestFraming(t *testing.T) {
}
}

func TestConcurrencyWriteControl(t *testing.T) {
const message = "this is a ping/pong messsage"
loop := 10
workers := 10
for i := 0; i < loop; i++ {
var connBuf bytes.Buffer

wg := sync.WaitGroup{}
wc := newTestConn(nil, &connBuf, true)

for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if err := wc.WriteControl(PongMessage, []byte(message), time.Now().Add(time.Second)); err != nil {
t.Errorf("concurrently wc.WriteControl() returned %v", err)
}
}()
}

wg.Wait()
wc.Close()
}
}

func TestControl(t *testing.T) {
t.Parallel()
const message = "this is a ping/pong messsage"
Expand Down