From f937ab7d290e7be5a01d32ddaa54a0c3ed5e7cda Mon Sep 17 00:00:00 2001 From: Stephanie Gredell Date: Thu, 19 Jun 2025 13:53:59 -0700 Subject: [PATCH] Modify microservicenode.go --- internal/simulation/microservicenode.go | 54 ++++++++----------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/internal/simulation/microservicenode.go b/internal/simulation/microservicenode.go index fc525fa..20dc5f6 100644 --- a/internal/simulation/microservicenode.go +++ b/internal/simulation/microservicenode.go @@ -38,58 +38,36 @@ func (n *MicroserviceNode) GetTargets() []string { } func (n *MicroserviceNode) Tick(tick int, currentTimeMs int) { - if !n.Alive { - return - } - - // Simulate circuit breaker state transitions - switch n.CircuitState { - case "open": - // Skip processing - return - case "half-open": - // Allow limited testing traffic - if len(n.Queue) == 0 { - return - } - req := n.Queue[0] - n.Queue = n.Queue[1:] - req.LatencyMS += 15 - req.Path = append(req.Path, n.ID+"(half-open)") - success := simulateSuccess() - if success { - n.CircuitState = "closed" - n.ErrorCount = 0 - n.Output = append(n.Output, req) - } else { - n.ErrorCount++ - n.CircuitState = "open" - } - return - } + n.CurrentLoad = 0 + n.ErrorCount = 0 + n.Output = nil - // "closed" circuit - normal processing toProcess := min(len(n.Queue), n.RateLimit) for i := 0; i < toProcess; i++ { req := n.Queue[i] - req.LatencyMS += 10 - req.Path = append(req.Path, n.ID) - if simulateFailure() { + if rand.Float64() < 0.02 { n.ErrorCount++ - if n.CircuitBreaker && n.ErrorCount > 5 { + if n.CircuitBreaker { n.CircuitState = "open" - break + n.Alive = false } + continue } + req.LatencyMS += 10 + req.Path = append(req.Path, n.ID) n.Output = append(n.Output, req) + n.CurrentLoad++ } - n.Queue = n.Queue[toProcess:] - // Health check: simple example - n.Alive = n.CircuitState != "open" + if n.CircuitState == "open" && tick%10 == 0 { + n.CircuitState = "closed" + n.Alive = true + } + + n.Queue = n.Queue[toProcess:] } func simulateFailure() bool {