@SuppressWarnings("unchecked")
private <V, K extends Exception> V runWithGoodResource(FunctionCheckedException<Client, V, K> f)
throws K {
boolean shouldReuse = true;
Client resource = getGoodClient();
try {
return f.apply(resource);
} catch (Exception e) {
if (e instanceof TTransportException
|| e instanceof TProtocolException) {
log.warn("Not reusing resource {} due to {}", resource, e);
shouldReuse = false;
}
if (e instanceof TTransportException
&& ((TTransportException) e).getType() == TTransportException.END_OF_FILE) {
discardCurrentPool();
}
throw (K) e;
} finally {
if (shouldReuse) {
log.info("Returning {} to pool", resource);
returnResource(resource);
} else {
log.info("Discarding: {}", resource);
cleanupForDiscard(resource);
}
}
}