From 2093eefd6ada1a1c90772f2f6d6eaccc1e19eb30 Mon Sep 17 00:00:00 2001 From: P2YH Date: Thu, 25 Jul 2024 19:35:06 +0800 Subject: [PATCH] fix OOM Calling electionHandler.Observe(e.ctx) within a for loop continuously creates goroutines, leading to an OOM (Out Of Memory) issue. --- elector.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/elector.go b/elector.go index c79d6c6..b0b62bd 100644 --- a/elector.go +++ b/elector.go @@ -189,9 +189,10 @@ func (e *Elector) Start(electionPath string) error { e.unsetLeader("") }() + ch := electionHandler.Observe(e.ctx) for e.ctx.Err() == nil { select { - case resp := <-electionHandler.Observe(e.ctx): + case resp := <-ch: if len(resp.Kvs) == 0 { continue }