|
| 1 | +// Package scraper implements provider search, stream extraction, and source diagnostics. |
| 2 | +package scraper |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + defaultSourceFailureThreshold = 3 |
| 12 | + defaultSourceCooldown = 10 * time.Minute |
| 13 | +) |
| 14 | + |
| 15 | +type sourceCircuitState struct { |
| 16 | + failures int |
| 17 | + openUntil time.Time |
| 18 | + lastDiagnostic *SourceDiagnostic |
| 19 | +} |
| 20 | + |
| 21 | +type sourceCircuitBreaker struct { |
| 22 | + mu sync.Mutex |
| 23 | + threshold int |
| 24 | + cooldown time.Duration |
| 25 | + now func() time.Time |
| 26 | + states map[ScraperType]*sourceCircuitState |
| 27 | +} |
| 28 | + |
| 29 | +func newSourceCircuitBreaker() *sourceCircuitBreaker { |
| 30 | + return &sourceCircuitBreaker{ |
| 31 | + threshold: defaultSourceFailureThreshold, |
| 32 | + cooldown: defaultSourceCooldown, |
| 33 | + now: time.Now, |
| 34 | + states: make(map[ScraperType]*sourceCircuitState), |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func (cb *sourceCircuitBreaker) isOpen(source ScraperType) (time.Time, *SourceDiagnostic, bool) { |
| 39 | + if cb == nil { |
| 40 | + return time.Time{}, nil, false |
| 41 | + } |
| 42 | + |
| 43 | + cb.mu.Lock() |
| 44 | + defer cb.mu.Unlock() |
| 45 | + |
| 46 | + state := cb.states[source] |
| 47 | + if state == nil || state.openUntil.IsZero() { |
| 48 | + return time.Time{}, nil, false |
| 49 | + } |
| 50 | + |
| 51 | + now := cb.now() |
| 52 | + if !now.Before(state.openUntil) { |
| 53 | + state.openUntil = time.Time{} |
| 54 | + state.failures = 0 |
| 55 | + state.lastDiagnostic = nil |
| 56 | + return time.Time{}, nil, false |
| 57 | + } |
| 58 | + |
| 59 | + return state.openUntil, state.lastDiagnostic, true |
| 60 | +} |
| 61 | + |
| 62 | +func (cb *sourceCircuitBreaker) recordSuccess(source ScraperType) { |
| 63 | + if cb == nil { |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + cb.mu.Lock() |
| 68 | + defer cb.mu.Unlock() |
| 69 | + delete(cb.states, source) |
| 70 | +} |
| 71 | + |
| 72 | +func (cb *sourceCircuitBreaker) recordFailure(source ScraperType, diagnostic *SourceDiagnostic) bool { |
| 73 | + if cb == nil || diagnostic == nil || !diagnostic.ShouldOpenCircuit() { |
| 74 | + return false |
| 75 | + } |
| 76 | + |
| 77 | + cb.mu.Lock() |
| 78 | + defer cb.mu.Unlock() |
| 79 | + |
| 80 | + state := cb.states[source] |
| 81 | + if state == nil { |
| 82 | + state = &sourceCircuitState{} |
| 83 | + cb.states[source] = state |
| 84 | + } |
| 85 | + |
| 86 | + state.failures++ |
| 87 | + state.lastDiagnostic = diagnostic |
| 88 | + if state.failures < cb.threshold { |
| 89 | + return false |
| 90 | + } |
| 91 | + |
| 92 | + state.openUntil = cb.now().Add(cb.cooldown) |
| 93 | + return true |
| 94 | +} |
| 95 | + |
| 96 | +func (sm *ScraperManager) ensureCircuitBreaker() *sourceCircuitBreaker { |
| 97 | + sm.breakerMu.Lock() |
| 98 | + defer sm.breakerMu.Unlock() |
| 99 | + |
| 100 | + if sm.breaker == nil { |
| 101 | + sm.breaker = newSourceCircuitBreaker() |
| 102 | + } |
| 103 | + return sm.breaker |
| 104 | +} |
| 105 | + |
| 106 | +func (sm *ScraperManager) circuitOpenDiagnostic(source ScraperType) (*SourceDiagnostic, time.Duration, bool) { |
| 107 | + breaker := sm.ensureCircuitBreaker() |
| 108 | + openUntil, lastDiagnostic, ok := breaker.isOpen(source) |
| 109 | + if !ok { |
| 110 | + return nil, 0, false |
| 111 | + } |
| 112 | + |
| 113 | + sourceName := sm.getScraperDisplayName(source) |
| 114 | + message := fmt.Sprintf("circuit breaker open until %s", openUntil.Format(time.RFC3339)) |
| 115 | + if lastDiagnostic != nil { |
| 116 | + message = fmt.Sprintf("%s; last failure: %s", message, lastDiagnostic.UserMessage()) |
| 117 | + } |
| 118 | + |
| 119 | + diagnostic := &SourceDiagnostic{ |
| 120 | + Source: sourceName, |
| 121 | + Layer: "circuit-breaker", |
| 122 | + Kind: DiagnosticSourceUnavailable, |
| 123 | + Message: message, |
| 124 | + Err: ErrSourceUnavailable, |
| 125 | + } |
| 126 | + |
| 127 | + return diagnostic, time.Until(openUntil), true |
| 128 | +} |
| 129 | + |
| 130 | +func (sm *ScraperManager) recordSourceSuccess(source ScraperType) { |
| 131 | + sm.ensureCircuitBreaker().recordSuccess(source) |
| 132 | +} |
| 133 | + |
| 134 | +func (sm *ScraperManager) recordSourceFailure(source ScraperType, diagnostic *SourceDiagnostic) bool { |
| 135 | + return sm.ensureCircuitBreaker().recordFailure(source, diagnostic) |
| 136 | +} |
0 commit comments