From f01629e7ea03b4e4f9433436a50fd4bdc290271c Mon Sep 17 00:00:00 2001 From: rfyiamcool Date: Fri, 8 Dec 2023 17:24:58 +0800 Subject: [PATCH] perf: reduce timer in write_control Signed-off-by: rfyiamcool --- conn.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/conn.go b/conn.go index 13c86b2..a3979da 100644 --- a/conn.go +++ b/conn.go @@ -446,13 +446,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()