use of io.grpc.Metadata in project grpc-java by grpc.
the class GoogleAuthLibraryCallCredentials method applyRequestMetadata.
@Override
public void applyRequestMetadata(MethodDescriptor<?, ?> method, Attributes attrs, Executor appExecutor, final MetadataApplier applier) {
String authority = checkNotNull(attrs.get(ATTR_AUTHORITY), "authority");
final URI uri;
try {
uri = serviceUri(authority, method);
} catch (StatusException e) {
applier.fail(e.getStatus());
return;
}
appExecutor.execute(new Runnable() {
@Override
public void run() {
try {
// Credentials is expected to manage caching internally if the metadata is fetched over
// the network.
//
// TODO(zhangkun83): we don't know whether there is valid cache data. If there is, we
// would waste a context switch by always scheduling in executor. However, we have to
// do so because we can't risk blocking the network thread. This can be resolved after
// https://github.com/google/google-auth-library-java/issues/3 is resolved.
//
// Some implementations may return null here.
Map<String, List<String>> metadata = creds.getRequestMetadata(uri);
// Re-use the headers if getRequestMetadata() returns the same map. It may return a
// different map based on the provided URI, i.e., for JWT. However, today it does not
// cache JWT and so we won't bother tring to save its return value based on the URI.
Metadata headers;
synchronized (GoogleAuthLibraryCallCredentials.this) {
if (lastMetadata == null || lastMetadata != metadata) {
lastMetadata = metadata;
lastHeaders = toHeaders(metadata);
}
headers = lastHeaders;
}
applier.apply(headers);
} catch (Throwable e) {
applier.fail(Status.UNAUTHENTICATED.withCause(e));
}
}
});
}
use of io.grpc.Metadata in project grpc-java by grpc.
the class StatsTraceContext method newServerContext.
/**
* Creates a {@code StatsTraceContext} for an incoming RPC, using the StatsContext deserialized
* from the headers.
*
* <p>The current time is used as the start time of the RPC.
*/
public static StatsTraceContext newServerContext(String methodName, StatsContextFactory statsFactory, Metadata headers, Supplier<Stopwatch> stopwatchSupplier) {
Metadata.Key<StatsContext> statsHeader = createStatsHeader(statsFactory);
StatsContext parentCtx = headers.get(statsHeader);
if (parentCtx == null) {
parentCtx = statsFactory.getDefault();
}
return new StatsTraceContext(Side.SERVER, methodName, parentCtx, stopwatchSupplier, statsHeader);
}
use of io.grpc.Metadata in project grpc-java by grpc.
the class AbstractClientStream method inboundDataReceived.
/**
* Processes the contents of a received data frame from the server.
*
* @param frame the received data frame. Its ownership is transferred to this method.
*/
protected void inboundDataReceived(ReadableBuffer frame) {
Preconditions.checkNotNull(frame, "frame");
boolean needToCloseFrame = true;
try {
if (inboundPhase() == Phase.STATUS) {
return;
}
if (inboundPhase() == Phase.HEADERS) {
// Have not received headers yet so error
inboundTransportError(Status.INTERNAL.withDescription("headers not received before payload"), new Metadata());
return;
}
inboundPhase(Phase.MESSAGE);
needToCloseFrame = false;
deframe(frame, false);
} finally {
if (needToCloseFrame) {
frame.close();
}
}
}
use of io.grpc.Metadata in project grpc-java by grpc.
the class DelayedStream method start.
@Override
public void start(ClientStreamListener listener) {
checkState(this.listener == null, "already started");
Status savedError;
boolean savedPassThrough;
synchronized (this) {
this.listener = checkNotNull(listener, "listener");
// If error != null, then cancel() has been called and was unable to close the listener
savedError = error;
savedPassThrough = passThrough;
if (!savedPassThrough) {
listener = delayedListener = new DelayedStreamListener(listener);
}
}
if (savedError != null) {
listener.closed(savedError, new Metadata());
return;
}
if (savedPassThrough) {
realStream.start(listener);
} else {
final ClientStreamListener finalListener = listener;
delayOrExecute(new Runnable() {
@Override
public void run() {
realStream.start(finalListener);
}
});
}
}
use of io.grpc.Metadata in project grpc-java by grpc.
the class DelayedStream method cancel.
// When this method returns, passThrough is guaranteed to be true
@Override
public void cancel(final Status reason) {
checkNotNull(reason, "reason");
boolean delegateToRealStream = true;
ClientStreamListener listenerToClose = null;
synchronized (this) {
// If realStream != null, then either setStream() or cancel() has been called
if (realStream == null) {
realStream = NoopClientStream.INSTANCE;
delegateToRealStream = false;
// If listener == null, then start() will later call listener with 'error'
listenerToClose = listener;
error = reason;
}
}
if (delegateToRealStream) {
delayOrExecute(new Runnable() {
@Override
public void run() {
realStream.cancel(reason);
}
});
} else {
if (listenerToClose != null) {
listenerToClose.closed(reason, new Metadata());
}
drainPendingCalls();
}
}
Aggregations