Working on High-Performance Golang Client Library — Remove the Bad Busy Loops With the Sync.Cond | by Ruian | Feb, 2022

Use sync.Cond with and with out the sync.Mutex

Photograph by Paul MARSAN on Unsplash
  1. The writing goroutine calls NextRequestToSend in a busy loop as a result of it doesn’t have blocking habits whereas the Golang channel has.
  1. Take away the dangerous busy loop by the sync.Cond with the sync.Mutex
  2. Reduce the dangerous busy loop by the sync.Cond with out the sync.Mutex

Golang is understood for making concurrent programming simply, and its runtime scheduler does a fantastic job of scheduling goroutines on the processes of the working system. However the actual concurrency of a Go program continues to be restricted by the CPU cores you’ve got.

Benchmark supply code:

For our EnqueueRequest, we’d like the flexibility to place a goroutine into sleep when the slot isn’t accessible and wake it up as soon as the slot is out there.

  1. sync.Cond
Benchmark supply code:

In our new EnqueueRequest, there will likely be lock contentions on slot provided that the ring is all the time recycling.

The ultimate piece of a thread-safe consumer library in all probability is the issue of tips on how to shut it. Within the ultimate put up, I’ll share how rueidis handles it gracefully.

More Posts