All Downloads are FREE. Search and download functionalities are using the official Maven repository.

netflix.ocelli.FailureDetectingInstanceFactory Maven / Gradle / Ivy

package netflix.ocelli;

import java.util.concurrent.TimeUnit;

import netflix.ocelli.functions.Connectors;
import netflix.ocelli.functions.Delays;
import netflix.ocelli.functions.Failures;
import rx.Observable;
import rx.functions.Func1;

/**
 * @author elandau
 *
 * @param 
 */
public class FailureDetectingInstanceFactory implements Func1> {

    public static class Builder {
        private Func1            quarantineDelayStrategy = Delays.fixed(10, TimeUnit.SECONDS);
        private Func1> failureDetector         = Failures.never();
        private Func1>      clientConnector         = Connectors.immediate();

        /**
         * Strategy used to determine the delay time in msec based on the quarantine 
         * count.  The count is incremented by one for each failure detections and reset
         * once the host is back to normal.
         */
        public Builder withQuarantineStrategy(Func1 quarantineDelayStrategy) {
            this.quarantineDelayStrategy = quarantineDelayStrategy;
            return this;
        }
        
        /**
         * The failure detector returns an Observable that will emit a Throwable for each 
         * failure of the client.  The load balancer will quarantine the client in response.
         * @param failureDetector
         */
        public Builder withFailureDetector(Func1> failureDetector) {
            this.failureDetector = failureDetector;
            return this;
        }
        
        /**
         * The connector can be used to prime a client prior to activating it in the connection
         * pool.  
         * @param clientConnector
         */
        public Builder withClientConnector(Func1> clientConnector) {
            this.clientConnector = clientConnector;
            return this;
        }
        
        public FailureDetectingInstanceFactory build() {
            return new FailureDetectingInstanceFactory(
                    clientConnector, 
                    failureDetector, 
                    quarantineDelayStrategy);
        }
    }
        
    public static  Builder builder() {
        return new Builder();
    }
    
    private final Func1> failureDetector;
    private final Func1>      clientConnector;
    private final Func1            quarantineDelayStrategy;

    public FailureDetectingInstanceFactory(
            Func1>      clientConnector, 
            Func1> failureDetector, 
            Func1            quarantineDelayStrategy) {
        this.quarantineDelayStrategy = quarantineDelayStrategy;
        this.failureDetector         = failureDetector;
        this.clientConnector         = clientConnector;
    }
    
    @Override
    public Instance call(final T client) {
        return new FailureDetectingInstance(
                client, 
                clientConnector, 
                failureDetector.call(client), 
                quarantineDelayStrategy);
    }

    public String toString() {
        return "FailureDetectingInstanceFactory[]";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy