com.github.phantomthief.failover.impl.SimpleWeightFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simple-failover Show documentation
Show all versions of simple-failover Show documentation
A simple failover library for Java
package com.github.phantomthief.failover.impl;
/**
* @author huangli
* Created on 2020-01-20
*/
public class SimpleWeightFunction implements WeightFunction {
private double failDecreaseRate = 0.05;
private double successIncreaseRate = 0.01;
public SimpleWeightFunction() {
}
public SimpleWeightFunction(double failDecreaseRate, double successIncreaseRate) {
this.failDecreaseRate = failDecreaseRate;
this.successIncreaseRate = successIncreaseRate;
}
@Override
public double success(double maxWeight, double minWeight, int priority, double currentOldWeight, T resource) {
return currentOldWeight + maxWeight * successIncreaseRate;
}
@Override
public double fail(double maxWeight, double minWeight, int priority, double currentOldWeight, T resource) {
return currentOldWeight - maxWeight * failDecreaseRate;
}
@Override
public boolean needCheck(double maxWeight, double minWeight, int priority, double currentWeight, T resource) {
return currentWeight <= minWeight;
}
}