io.grpc.Compressor Java Examples

The following examples show how to use io.grpc.Compressor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClientCallImpl.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static void prepareHeaders(
    Metadata headers,
    DecompressorRegistry decompressorRegistry,
    Compressor compressor,
    boolean fullStreamDecompression) {
  headers.discardAll(MESSAGE_ENCODING_KEY);
  if (compressor != Codec.Identity.NONE) {
    headers.put(MESSAGE_ENCODING_KEY, compressor.getMessageEncoding());
  }

  headers.discardAll(MESSAGE_ACCEPT_ENCODING_KEY);
  byte[] advertisedEncodings =
      InternalDecompressorRegistry.getRawAdvertisedMessageEncodings(decompressorRegistry);
  if (advertisedEncodings.length != 0) {
    headers.put(MESSAGE_ACCEPT_ENCODING_KEY, advertisedEncodings);
  }

  headers.discardAll(CONTENT_ENCODING_KEY);
  headers.discardAll(CONTENT_ACCEPT_ENCODING_KEY);
  if (fullStreamDecompression) {
    headers.put(CONTENT_ACCEPT_ENCODING_KEY, FULL_STREAM_DECOMPRESSION_ENCODINGS);
  }
}
 
Example #2
Source File: ArmeriaClientCall.java    From armeria with Apache License 2.0 6 votes vote down vote up
private void prepareHeaders(Compressor compressor, Metadata metadata) {
    final RequestHeadersBuilder newHeaders = req.headers().toBuilder();
    if (compressor != Identity.NONE) {
        newHeaders.set(GrpcHeaderNames.GRPC_ENCODING, compressor.getMessageEncoding());
    }

    if (!advertisedEncodingsHeader.isEmpty()) {
        newHeaders.add(GrpcHeaderNames.GRPC_ACCEPT_ENCODING, advertisedEncodingsHeader);
    }

    newHeaders.add(GrpcHeaderNames.GRPC_TIMEOUT,
                   TimeoutHeaderUtil.toHeaderValue(
                           TimeUnit.MILLISECONDS.toNanos(ctx.responseTimeoutMillis())));

    MetadataUtil.fillHeaders(metadata, newHeaders);

    final HttpRequest newReq = req.withHeaders(newHeaders);
    ctx.updateRequest(newReq);
}
 
Example #3
Source File: ClientCallImpl.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static void prepareHeaders(
    Metadata headers,
    DecompressorRegistry decompressorRegistry,
    Compressor compressor,
    boolean fullStreamDecompression) {
  headers.discardAll(MESSAGE_ENCODING_KEY);
  if (compressor != Codec.Identity.NONE) {
    headers.put(MESSAGE_ENCODING_KEY, compressor.getMessageEncoding());
  }

  headers.discardAll(MESSAGE_ACCEPT_ENCODING_KEY);
  byte[] advertisedEncodings =
      InternalDecompressorRegistry.getRawAdvertisedMessageEncodings(decompressorRegistry);
  if (advertisedEncodings.length != 0) {
    headers.put(MESSAGE_ACCEPT_ENCODING_KEY, advertisedEncodings);
  }

  headers.discardAll(CONTENT_ENCODING_KEY);
  headers.discardAll(CONTENT_ACCEPT_ENCODING_KEY);
  if (fullStreamDecompression) {
    headers.put(CONTENT_ACCEPT_ENCODING_KEY, FULL_STREAM_DECOMPRESSION_ENCODINGS);
  }
}
 
Example #4
Source File: RetriableStream.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public final void setCompressor(final Compressor compressor) {
  class CompressorEntry implements BufferEntry {
    @Override
    public void runWith(Substream substream) {
      substream.stream.setCompressor(compressor);
    }
  }

  delayOrExecute(new CompressorEntry());
}
 
Example #5
Source File: DelayedStream.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public void setCompressor(final Compressor compressor) {
  checkNotNull(compressor, "compressor");
  delayOrExecute(new Runnable() {
    @Override
    public void run() {
      realStream.setCompressor(compressor);
    }
  });
}
 
Example #6
Source File: DelayedStream.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void setCompressor(final Compressor compressor) {
  checkNotNull(compressor, "compressor");
  delayOrExecute(new Runnable() {
    @Override
    public void run() {
      realStream.setCompressor(compressor);
    }
  });
}
 
Example #7
Source File: RetriableStream.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Override
public final void setCompressor(final Compressor compressor) {
  class CompressorEntry implements BufferEntry {
    @Override
    public void runWith(Substream substream) {
      substream.stream.setCompressor(compressor);
    }
  }

  delayOrExecute(new CompressorEntry());
}
 
Example #8
Source File: InProcessTransport.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public void setCompressor(Compressor compressor) {}
 
Example #9
Source File: ForwardingClientStreamTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void setCompressorTest() {
  Compressor compressor = mock(Compressor.class);
  forward.setCompressor(compressor);
  verify(mock).setCompressor(same(compressor));
}
 
Example #10
Source File: NoopClientStream.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public void setCompressor(Compressor compressor) {}
 
Example #11
Source File: AbstractStream.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public final void setCompressor(Compressor compressor) {
  framer().setCompressor(checkNotNull(compressor, "compressor"));
}
 
Example #12
Source File: MessageFramer.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public MessageFramer setCompressor(Compressor compressor) {
  this.compressor = checkNotNull(compressor, "Can't pass an empty compressor");
  return this;
}
 
Example #13
Source File: AbstractClientStream.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public Framer setCompressor(Compressor compressor) {
  return this;
}
 
Example #14
Source File: Framer.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/** Set the compressor used for compression. */
Framer setCompressor(Compressor compressor);
 
Example #15
Source File: ClientCallImpl.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
private void startInternal(final Listener<RespT> observer, Metadata headers) {
  checkState(stream == null, "Already started");
  checkState(!cancelCalled, "call was cancelled");
  checkNotNull(observer, "observer");
  checkNotNull(headers, "headers");

  if (context.isCancelled()) {
    // Context is already cancelled so no need to create a real stream, just notify the observer
    // of cancellation via callback on the executor
    stream = NoopClientStream.INSTANCE;
    executeCloseObserverInContext(observer, statusFromCancelled(context));
    return;
  }
  final String compressorName = callOptions.getCompressor();
  Compressor compressor;
  if (compressorName != null) {
    compressor = compressorRegistry.lookupCompressor(compressorName);
    if (compressor == null) {
      stream = NoopClientStream.INSTANCE;
      Status status = Status.INTERNAL.withDescription(
          String.format("Unable to find compressor by name %s", compressorName));
      executeCloseObserverInContext(observer, status);
      return;
    }
  } else {
    compressor = Codec.Identity.NONE;
  }
  prepareHeaders(headers, decompressorRegistry, compressor, fullStreamDecompression);

  Deadline effectiveDeadline = effectiveDeadline();
  boolean deadlineExceeded = effectiveDeadline != null && effectiveDeadline.isExpired();
  if (!deadlineExceeded) {
    logIfContextNarrowedTimeout(
        effectiveDeadline, context.getDeadline(), callOptions.getDeadline());
    if (retryEnabled) {
      stream = clientTransportProvider.newRetriableStream(method, callOptions, headers, context);
    } else {
      ClientTransport transport = clientTransportProvider.get(
          new PickSubchannelArgsImpl(method, headers, callOptions));
      Context origContext = context.attach();
      try {
        stream = transport.newStream(method, headers, callOptions);
      } finally {
        context.detach(origContext);
      }
    }
  } else {
    stream = new FailingClientStream(
        DEADLINE_EXCEEDED.withDescription(
            "ClientCall started after deadline exceeded: " + effectiveDeadline));
  }

  if (callExecutorIsDirect) {
    stream.optimizeForDirectExecutor();
  }
  if (callOptions.getAuthority() != null) {
    stream.setAuthority(callOptions.getAuthority());
  }
  if (callOptions.getMaxInboundMessageSize() != null) {
    stream.setMaxInboundMessageSize(callOptions.getMaxInboundMessageSize());
  }
  if (callOptions.getMaxOutboundMessageSize() != null) {
    stream.setMaxOutboundMessageSize(callOptions.getMaxOutboundMessageSize());
  }
  if (effectiveDeadline != null) {
    stream.setDeadline(effectiveDeadline);
  }
  stream.setCompressor(compressor);
  if (fullStreamDecompression) {
    stream.setFullStreamDecompression(fullStreamDecompression);
  }
  stream.setDecompressorRegistry(decompressorRegistry);
  channelCallsTracer.reportCallStarted();
  cancellationListener = new ContextCancellationListener(observer);
  stream.start(new ClientStreamListenerImpl(observer));

  // Delay any sources of cancellation after start(), because most of the transports are broken if
  // they receive cancel before start. Issue #1343 has more details

  // Propagate later Context cancellation to the remote side.
  context.addListener(cancellationListener, directExecutor());
  if (effectiveDeadline != null
      // If the context has the effective deadline, we don't need to schedule an extra task.
      && !effectiveDeadline.equals(context.getDeadline())
      // If the channel has been terminated, we don't need to schedule an extra task.
      && deadlineCancellationExecutor != null
      // if already expired deadline let failing stream handle
      && !(stream instanceof FailingClientStream)) {
    deadlineCancellationNotifyApplicationFuture =
        startDeadlineNotifyApplicationTimer(effectiveDeadline, observer);
  }
  if (cancelListenersShouldBeRemoved) {
    // Race detected! ClientStreamListener.closed may have been called before
    // deadlineCancellationFuture was set / context listener added, thereby preventing the future
    // and listener from being cancelled. Go ahead and cancel again, just to be sure it
    // was cancelled.
    removeContextListenerAndCancelDeadlineFuture();
  }
}
 
Example #16
Source File: ForwardingClientStream.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public void setCompressor(Compressor compressor) {
  delegate().setCompressor(compressor);
}
 
Example #17
Source File: InProcessTransport.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public void setCompressor(Compressor compressor) {}
 
Example #18
Source File: InProcessTransport.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public void setCompressor(Compressor compressor) {}
 
Example #19
Source File: ForwardingClientStreamTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Test
public void setCompressorTest() {
  Compressor compressor = mock(Compressor.class);
  forward.setCompressor(compressor);
  verify(mock).setCompressor(same(compressor));
}
 
Example #20
Source File: NoopClientStream.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public void setCompressor(Compressor compressor) {}
 
Example #21
Source File: AbstractStream.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public final void setCompressor(Compressor compressor) {
  framer().setCompressor(checkNotNull(compressor, "compressor"));
}
 
Example #22
Source File: MessageFramer.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public MessageFramer setCompressor(Compressor compressor) {
  this.compressor = checkNotNull(compressor, "Can't pass an empty compressor");
  return this;
}
 
Example #23
Source File: AbstractClientStream.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public Framer setCompressor(Compressor compressor) {
  return this;
}
 
Example #24
Source File: Framer.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/** Set the compressor used for compression. */
Framer setCompressor(Compressor compressor);
 
Example #25
Source File: ForwardingClientStream.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public void setCompressor(Compressor compressor) {
  delegate().setCompressor(compressor);
}
 
Example #26
Source File: InProcessTransport.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public void setCompressor(Compressor compressor) {}
 
Example #27
Source File: Stream.java    From grpc-java with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the compressor on the framer.
 *
 * @param compressor the compressor to use
 */
void setCompressor(Compressor compressor);
 
Example #28
Source File: Stream.java    From grpc-nebula-java with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the compressor on the framer.
 *
 * @param compressor the compressor to use
 */
void setCompressor(Compressor compressor);