Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
3
Indexable
Never
class Server {
    // 00, 01 -> open
    // 10, 11 -> close
    public int currentStatus = 0;
    
    private boolean isOpen(){
        return currentStatus < 2;
    }
    
    private int addFailure() {
        return ++currentStatus == 5? currentStatus = 0: currentStatus;
    }
}

class DoubleBreakerResult {
    Server primaryServer = new Server();
    Server fallbackServer = new Server();
    
    call(isPrimary, isAccept) {
        Server server = isPrimary ? primaryServer : fallbackServer;
        Server anotherServer = isPrimary ? fallbackServer : primaryServer;
        // check server status
        if (server.isOpen()) {
            if (!isAccept) {
                anotherServer.addFailure();
                call(!isPrimary, false);
            }
        } else {
            return "server is down"
        }
    }
}

main(){
    double_breaker.call(isPrimary: true, isAccept:true); // 
    double_breaker.call(isPrimary: true, isAccept:false); // 
    double_breaker.call(isPrimary: true, isAccept:false); // 
    double_breaker.call(isPrimary: true, isAccept:false); //
}