mirror of
https://github.com/MedUnes/go-kata.git
synced 2026-03-12 21:55:53 +07:00
1.4 KiB
1.4 KiB
Kata 14: The Leak-Free Scheduler
Target Idioms: time.Timer/time.Ticker, Stop/Reset patterns, Jitter, Context Cancellation
Difficulty: 🟡 Intermediate
🧠 The "Why"
Scheduling in Go is deceptively easy until you ship:
- goroutines that never stop,
- overlapping executions,
- ticker drift and backlog,
- resource retention from careless timer usage.
This kata makes you build a scheduler that is predictable and stoppable.
🎯 The Scenario
You need to periodically refresh a local cache:
- every 5s, with ±10% jitter
- do not overlap refreshes
- stop immediately on shutdown
🛠 The Challenge
Implement:
type Scheduler struct { ... }func (s *Scheduler) Run(ctx context.Context, job func(context.Context) error) error
1. Functional Requirements
- Run
jobperiodically (interval + jitter). - Never run
jobconcurrently with itself. - Exit on
ctx.Done().
2. The "Idiomatic" Constraints (Pass/Fail Criteria)
- Must NOT use
time.Tick(no stop control). - Must use a
time.Timerortime.Tickerwith correct stop/reset. - Must propagate context into
job. - Log job duration and errors via
slog.
🧪 Self-Correction (Test Yourself)
- If
joboverlap occurs: you failed. - If cancel doesn’t stop quickly: you failed.
- If goroutines remain after exit: you failed.