Skip to content

Instantly share code, notes, and snippets.

@rueian
Last active February 20, 2022 13:42
Show Gist options
  • Save rueian/97364d806431f9cb6dc46f88a3f11c4e to your computer and use it in GitHub Desktop.
Save rueian/97364d806431f9cb6dc46f88a3f11c4e to your computer and use it in GitHub Desktop.
var waits = int32(0)
var sleep = int32(0)
var cond = sync.Cond{L: (*emptyLock)(nil)}
func makeRequest(req request, queue *ring) (resp response) {
waits := atomic.AddInt32(&waits, 1)
ch := queue.EnqueueRequest(queue)
if waits == 1 {
for atomic.LoadInt32(&sleep) != 0 {
cond.Broadcast()
runtime.Gosched()
}
}
resp = <-ch
atomic.AddInt32(&waits, -1)
}
func writing(outgoing *bufio.Writer, queue *ring) (err error) {
var req request
var ok bool
for err == nil {
if req, ok = queue.NextRequestToSend(); ok {
_, err = outgoing.Write(req.body)
} else {
err = outgoing.Flush()
atomic.AddInt32(&sleep, 1)
if atomic.LoadInt32(&waits) == 0 {
cond.Wait()
}
atomic.AddInt32(&sleep, -1)
}
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment