Untitled

 avatar
unknown
plain_text
2 years ago
1.1 kB
7
Indexable
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); //
}
Editor is loading...