Skip to content

Commit

Permalink
引入短暂的休眠,减少自旋锁行为 .ehang-io#9
Browse files Browse the repository at this point in the history
  • Loading branch information
aohanhongzhi committed Aug 2, 2024
1 parent 4afa0c1 commit f42ed60
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
11 changes: 9 additions & 2 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,10 @@ func (s *Mux) Close() (err error) {
// while target host close socket without finish steps, conn.Close method maybe blocked
// and tcp status change to CLOSE WAIT or TIME WAIT, so we close it in other goroutine
_ = s.conn.SetDeadline(time.Now().Add(time.Second * 5))
go s.conn.Close()
go func() {
s.conn.Close()
s.bw.Close()
}()
s.release()
return
}
Expand All @@ -361,7 +364,7 @@ func (s *Mux) release() {
s.newConnQueue.Stop()
}

//Get New connId as unique flag
// Get New connId as unique flag
func (s *Mux) getId() (id int32) {
//Avoid going beyond the scope
if (math.MaxInt32 - s.id) < 10000 {
Expand Down Expand Up @@ -428,6 +431,10 @@ func (Self *bandwidth) Get() (bw float64) {
return
}

func (Self *bandwidth) Close() error {
return Self.fd.Close()
}

const counterBits = 4
const counterMask = 1<<counterBits - 1

Expand Down
3 changes: 3 additions & 0 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,9 @@ func (c *bufChain) popTail() (unsafe.Pointer, bool) {
// dequeue and so popHead doesn't back up
// further than necessary.
storePoolChainElt(&d2.prev, nil)
} else {
// 引入短暂的休眠,减少自旋锁行为
time.Sleep(100 * time.Microsecond)
}
d = d2
}
Expand Down

0 comments on commit f42ed60

Please sign in to comment.