com.navercorp.pinpoint.grpc.channelz.ChannelzUtils Maven / Gradle / Ivy
package com.navercorp.pinpoint.grpc.channelz;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import io.grpc.InternalInstrumented;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public final class ChannelzUtils {
private static final Logger logger = LoggerFactory.getLogger(ChannelzUtils.class);
private static final long timeout = 3000L;
private ChannelzUtils() {
}
public static T getResult(String name, InternalInstrumented instrumented) {
final ListenableFuture future = instrumented.getStats();
try {
return future.get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
logger.info("ExecutionError {} {}", name, e.getMessage());
} catch (TimeoutException e) {
logger.info("Timeout {} {}", name, e.getMessage());
}
return null;
}
public static List getResults(String name, List> instrumentedList) {
Objects.requireNonNull(instrumentedList, "instrumentedList");
final List> listenableFutures = new ArrayList<>(instrumentedList.size());
for (InternalInstrumented each : instrumentedList) {
listenableFutures.add(each.getStats());
}
ListenableFuture> listListenableFuture = Futures.allAsList(listenableFutures);
try {
return listListenableFuture.get(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
logger.info("ExecutionError {} {}", name, e.getMessage());
} catch (TimeoutException e) {
logger.info("Timeout {} {}", name, e.getMessage());
}
return null;
}
}