Ugrás a fő tartalomra
🔒 Előnézet mód. Az első tizenöt Foundations lecke ingyenes; ez Pro. Indíts 7 napos trial-t, hogy feloldd a szerkesztőt, az AI tippeket és a tananyag többi részét. Kártya szükséges, bármikor lemondhatod a Dashboard-ban.7 napos trial indítása →
← KurzusokSystem Design for Python Juniors1. modul · Rendszertervezés alapjaiTervezés kudarcrapredict15 / 105
+100 XP
Feladat
📝 **Feladat:** Megjósolja a megszakító által kibocsátott állapotsorok 7 soros nyomát, miközben 2 sikeren, 3 meghibásodáson, 1 rövidzárlaton és 1 helyreállítási szondán áthalad. 📋 Valósítsa meg a fenti funkciót. A tesztek automatikusan futnak. 💡 **Tipp:** Ha elakad, olvassa el újra az elméletet.
Találd ki a kimenetet

Olvasd el a kódot figyelmesen

class CircuitBreaker:
    def __init__(self, threshold=3):
        self.state = "CLOSED"
        self.failures = 0
        self.threshold = threshold

    def call(self, fn):
        # OPEN — short-circuit, never call the real fn.
        if self.state == "OPEN":
            return "fallback"
        try:
            result = fn()
        except Exception:
            self.failures += 1
            if self.failures >= self.threshold:
                self.state = "OPEN"
            return "fallback"
        # Success path — only reset failure count from HALF_OPEN.
        if self.state == "HALF_OPEN":
            self.state = "CLOSED"
            self.failures = 0
        return result

    def half_open(self):
        # An external timer in a real CB would do this. We trigger
        # it manually so the prediction stays deterministic.
        self.state = "HALF_OPEN"


def ok():    return "ok"
def boom():  raise RuntimeError("dep down")

cb = CircuitBreaker(threshold=3)

cb.call(ok);   print(cb.state)        # CLOSED, success — state unchanged
cb.call(ok);   print(cb.state)        # CLOSED
cb.call(boom); print(cb.state)        # 1 fail → CLOSED still (under threshold)
cb.call(boom); print(cb.state)        # 2 fails → CLOSED
cb.call(boom); print(cb.state)        # 3 fails → OPEN (just tripped)
cb.call(ok);   print(cb.state)        # OPEN — short-circuited, real fn skipped
cb.half_open()
cb.call(ok);   print(cb.state)        # HALF_OPEN probe success → CLOSED

# What does this print? Type your prediction.

Mit ír ki a program? Írd ide:

💬 Beszélgetés

Légy az első — tegyél fel kérdést vagy oszd meg egy tippet.
Jelentkezz be hogy csatlakozz a beszélgetéshez. Az olvasás ingyenes.
Beszélgetés betöltése…