Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reduce timer in write_control #879

Merged
merged 2 commits into from
Jan 23, 2024
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 @@
}
}

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

Check warning on line 465 in conn.go

View check run for this annotation

Codecov / codecov/patch

conn.go#L464-L465

Added lines #L464 - L465 were not covered by tests
}
}

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
Loading