Code RoomCircuit breaker never resets
HardPrep Room Coding #1945

Circuit breaker never resets

Code reviewCode quality & reviewSenior–Staff~22 min

Review this Go circuit breaker wrapping a downstream call.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 22 min
Mark a line and say what kind of problem it is.0 findings
1type Breaker struct {
2 mu sync.Mutex
3 failures int
4 open bool
5}
6 
7func (b *Breaker) Call(fn func() error) error {
8 b.mu.Lock(); o := b.open; b.mu.Unlock()
9 if o {
10 return errors.New("circuit open")
11 }
12 err := fn()
13 b.mu.Lock(); defer b.mu.Unlock()
14 if err != nil {
15 b.failures++
16 if b.failures >= 5 {
17 b.open = true
18 }
19 }
20 return err
21}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.