From ccf8e35f90e7fa0206f61b8c255bca6acb7ec47a Mon Sep 17 00:00:00 2001 From: Garet Halliday <me@garet.holiday> Date: Tue, 18 Apr 2023 16:30:39 -0500 Subject: [PATCH] the good kind of race --- lib/gat2/sink/pools/request/pool.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/gat2/sink/pools/request/pool.go b/lib/gat2/sink/pools/request/pool.go index 5fcc4fc5..f8d2e153 100644 --- a/lib/gat2/sink/pools/request/pool.go +++ b/lib/gat2/sink/pools/request/pool.go @@ -3,7 +3,7 @@ package request import ( "gfx.cafe/gfx/pggat/lib/gat2/request" "gfx.cafe/gfx/pggat/lib/gat2/sink" - "math/rand" + "gfx.cafe/gfx/pggat/lib/util/race" ) type Pool struct { @@ -28,12 +28,20 @@ func (T *Pool) handle(req request.Request) { default: } } - // choose a random sink to wait for if len(T.sinks) == 0 { // TODO(garet) this should just error panic("no free pools") } - T.sinks[rand.Intn(len(T.sinks))].In() <- req + // choose a random sink to wait for + ok := race.Send(func(i int) (chan<- request.Request, bool) { + if i >= len(T.sinks) { + return nil, false + } + return T.sinks[i].In(), true + }, req) + if !ok { + panic("failed to send req to pool") + } } func (T *Pool) run() { -- GitLab