From dd599f0228f079ad8408f25a86510c6ed045be13 Mon Sep 17 00:00:00 2001
From: Garet Halliday <me@garet.holiday>
Date: Mon, 31 Jul 2023 16:08:06 -0600
Subject: [PATCH] race

---
 cmd/cgat/main.go                  | 2 +-
 lib/gat/pool.go                   | 8 ++++----
 lib/gat/pools/transaction/pool.go | 4 ++--
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/cmd/cgat/main.go b/cmd/cgat/main.go
index c063337b..d22bb288 100644
--- a/cmd/cgat/main.go
+++ b/cmd/cgat/main.go
@@ -26,7 +26,7 @@ func main() {
 
 	// create pool
 	rawPool := transaction.NewPool()
-	pool := gat.NewPool(rawPool)
+	pool := gat.NewPool(rawPool, 15*time.Second)
 	postgres.AddPool("uniswap", pool)
 	pool.AddRecipe("localhost", gat.TCPRecipe{
 		Database:       "uniswap",
diff --git a/lib/gat/pool.go b/lib/gat/pool.go
index 2b7d76c0..1c1c01c9 100644
--- a/lib/gat/pool.go
+++ b/lib/gat/pool.go
@@ -58,7 +58,7 @@ type Pool struct {
 	raw RawPool
 }
 
-func NewPool(raw RawPool) *Pool {
+func NewPool(raw RawPool, idleTimeout time.Duration) *Pool {
 	onWait := make(chan struct{})
 	pool := &Pool{
 		ctx: Context{
@@ -79,7 +79,7 @@ func NewPool(raw RawPool) *Pool {
 
 			now := time.Now()
 			idle := pool.IdleSince()
-			for now.Sub(idle) > 15*time.Second {
+			for now.Sub(idle) > idleTimeout {
 				if idle == (time.Time{}) {
 					break
 				}
@@ -88,9 +88,9 @@ func NewPool(raw RawPool) *Pool {
 			}
 
 			if idle == (time.Time{}) {
-				wait = 15 * time.Second
+				wait = idleTimeout
 			} else {
-				wait = now.Sub(idle.Add(15 * time.Second))
+				wait = now.Sub(idle.Add(idleTimeout))
 			}
 
 			time.Sleep(wait)
diff --git a/lib/gat/pools/transaction/pool.go b/lib/gat/pools/transaction/pool.go
index 05ef9125..c79aa76b 100644
--- a/lib/gat/pools/transaction/pool.go
+++ b/lib/gat/pools/transaction/pool.go
@@ -91,8 +91,8 @@ func (T *Pool) ScaleDown(amount int) (remaining int) {
 	remaining = amount
 
 	for i := 0; i < amount; i++ {
-		id, _ := T.s.GetIdleWorker()
-		if id == uuid.Nil {
+		id, idle := T.s.GetIdleWorker()
+		if id == uuid.Nil || idle == (time.Time{}) {
 			break
 		}
 		worker := T.s.RemoveWorker(id)
-- 
GitLab