use of org.bboxdb.commons.Retryer in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class OrderManager method placeOrderAndWaitUntilActive.
/**
* Place an order and retry if Exception occur
* @param order - new BitfinexOrder to place
* @throws APIException
* @throws InterruptedException
*/
public void placeOrderAndWaitUntilActive(final BitfinexOrder order) throws APIException, InterruptedException {
final ConnectionCapabilities capabilities = bitfinexApiBroker.getCapabilities();
if (!capabilities.isHavingOrdersWriteCapability()) {
throw new APIException("Unable to wait for order " + order + " connection has not enough capabilities: " + capabilities);
}
order.setApikey(bitfinexApiBroker.getApiKey());
final Callable<Boolean> orderCallable = () -> placeOrderOrderOnAPI(order);
// Bitfinex does not implement a happens-before relationship. Sometimes
// canceling a stop-loss order and placing a new stop-loss order results
// in an 'ERROR, reason is Invalid order: not enough exchange balance'
// error for some seconds. The retryer tries to place the order up to
// three times
final Retryer<Boolean> retryer = new Retryer<>(ORDER_RETRIES, RETRY_DELAY_IN_MS, orderCallable);
retryer.execute();
if (retryer.getNeededExecutions() > 1) {
logger.info("Nedded {} executions for placing the order", retryer.getNeededExecutions());
}
if (!retryer.isSuccessfully()) {
final Exception lastException = retryer.getLastException();
if (lastException == null) {
throw new APIException("Unable to execute order");
} else {
throw new APIException(lastException);
}
}
}
use of org.bboxdb.commons.Retryer in project bboxdb by jnidzwetzki.
the class DistributionRegionSyncer method updateNode.
/**
* Update the given node
* @param nodePath
* @param region
* @throws InterruptedException
*/
private void updateNode(final String nodePath, final DistributionRegion region) throws InterruptedException {
logger.debug("updateNode called with node {}", nodePath);
final Watcher callbackWatcher = this;
final Retryer<Boolean> retryer = new Retryer<>(10, 100, () -> {
try {
final Collection<BBoxDBInstance> systemsForDistributionRegion = distributionRegionAdapter.getSystemsForDistributionRegion(region);
region.setSystems(systemsForDistributionRegion);
final int regionId = distributionGroupAdapter.getRegionIdForPath(nodePath);
if (region.getRegionId() != regionId) {
throw new RuntimeException("Replacing region id " + region.getRegionId() + " with " + regionId + " on " + nodePath);
}
final DistributionRegionState regionState = distributionRegionAdapter.getStateForDistributionRegion(nodePath, callbackWatcher);
region.setState(regionState);
updateChildrenForRegion(nodePath, region);
} catch (ZookeeperNotFoundException e) {
// Node is deleted, let the deletion callback remove the node
logger.debug("Skippping node update for path {}, node is deleted", nodePath);
}
return true;
});
retryer.execute();
if (!retryer.isSuccessfully()) {
logger.error("Got error while rereading tree", retryer.getLastException());
}
}
use of org.bboxdb.commons.Retryer in project bboxdb by jnidzwetzki.
the class DistributionRegionHelper method getDistributionRegionForNamePrefix.
/**
* Find the region for the given name prefix
* @param searchNameprefix
* @return
* @throws InterruptedException
*/
public static DistributionRegion getDistributionRegionForNamePrefix(final DistributionRegion region, final long searchNameprefix) throws InterruptedException {
if (region == null) {
return null;
}
final Callable<DistributionRegion> getDistributionRegion = new Callable<DistributionRegion>() {
@Override
public DistributionRegion call() throws Exception {
return region.getThisAndChildRegions().stream().filter(r -> r.getRegionId() == searchNameprefix).findAny().orElseThrow(() -> new Exception("Unable to get distribution region"));
}
};
// Retry the operation if needed
final Retryer<DistributionRegion> retyer = new Retryer<>(Const.OPERATION_RETRY, 250, getDistributionRegion);
retyer.execute();
return retyer.getResult();
}
use of org.bboxdb.commons.Retryer in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class OrderManager method cancelOrderAndWaitForCompletion.
/**
* Cancel a order
* @param id
* @throws APIException, InterruptedException
*/
public void cancelOrderAndWaitForCompletion(final long id) throws APIException, InterruptedException {
final ConnectionCapabilities capabilities = bitfinexApiBroker.getCapabilities();
if (!capabilities.isHavingOrdersWriteCapability()) {
throw new APIException("Unable to cancel order " + id + " connection has not enough capabilities: " + capabilities);
}
final Callable<Boolean> orderCallable = () -> cancelOrderOnAPI(id);
// See comment in placeOrder()
final Retryer<Boolean> retryer = new Retryer<>(ORDER_RETRIES, RETRY_DELAY_IN_MS, orderCallable);
retryer.execute();
if (retryer.getNeededExecutions() > 1) {
logger.info("Nedded {} executions for canceling the order", retryer.getNeededExecutions());
}
if (!retryer.isSuccessfully()) {
final Exception lastException = retryer.getLastException();
if (lastException == null) {
throw new APIException("Unable to cancel order");
} else {
throw new APIException(lastException);
}
}
}
Aggregations