CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-grpc--grpc-core

Core gRPC library containing transport implementation, channel abstraction, load balancing, name resolution, and other fundamental gRPC functionalities for Java

Pending
Overview
Eval results
Files

transport-layer.mddocs/

Transport Layer

gRPC Core defines the fundamental transport abstractions that enable different transport implementations (Netty, OkHttp, in-process) to work with the gRPC framework. These interfaces provide contracts for client and server communication.

Capabilities

Client Transport Interface

Defines the contract for client-side transport implementations.

/**
 * Interface for client-side transport implementations
 * Located: io.grpc.internal.ClientTransport
 */
interface ClientTransport extends InternalInstrumented<SocketStats> {
    /**
     * Creates a new stream for an RPC call
     * @param method The method descriptor for the RPC
     * @param headers Initial metadata/headers for the request
     * @param callOptions Options for this specific call
     * @param tracers Array of tracers for monitoring the stream
     * @return ClientStream instance for this RPC
     */
    ClientStream newStream(
        MethodDescriptor<?, ?> method,
        Metadata headers,
        CallOptions callOptions,
        ClientStreamTracer[] tracers
    );
    
    /**
     * Ping the remote endpoint to verify connectivity
     * @param callback Callback to invoke when ping completes
     * @param executor Executor to run the callback on
     */
    void ping(PingCallback callback, Executor executor);
}

Connection Client Transport Interface

Extended interface for connection-based client transports.

/**
 * Interface for connection-based client transports
 * Located: io.grpc.internal.ConnectionClientTransport
 */
interface ConnectionClientTransport extends ClientTransport {
    /**
     * Initiates shutdown of the transport
     * @param reason Status indicating reason for shutdown
     * @return ListenableFuture that completes when shutdown is initiated
     */
    ListenableFuture<Void> shutdown(Status reason);
    
    /**
     * Initiates graceful shutdown with specified timeout
     * @param reason Status indicating reason for shutdown  
     * @param timeout Timeout for graceful shutdown
     * @param unit Time unit for timeout
     * @return ListenableFuture that completes when shutdown is complete
     */
    ListenableFuture<Void> shutdownNow(Status reason, long timeout, TimeUnit unit);
    
    /**
     * Gets connectivity state of the transport
     * @return Current connectivity state
     */
    ConnectivityState getConnectivityState();
    
    /**
     * Registers a listener for connectivity state changes
     * @param state State to watch for changes from
     * @param listener Listener to notify of state changes
     * @param executor Executor to run listener on
     */
    void notifyWhenStateChanged(
        ConnectivityState state,
        Runnable listener,
        Executor executor
    );
}

Server Transport Interface

Defines the contract for server-side transport implementations.

/**
 * Interface for server-side transport implementations
 * Located: io.grpc.internal.ServerTransport
 */
interface ServerTransport {
    /**
     * Starts the server transport with the given listener
     * @param listener Listener for transport events
     */
    void start(ServerTransportListener listener);
    
    /**
     * Initiates graceful shutdown of the transport
     */
    void shutdown();
    
    /**
     * Initiates immediate shutdown of the transport
     * @param reason Status indicating reason for shutdown
     */
    void shutdownNow(Status reason);
    
    /**
     * Gets the log ID for this transport (for debugging/logging)
     * @return Unique log ID string
     */
    InternalLogId getLogId();
    
    /**
     * Gets the attributes associated with this transport
     * @return Transport attributes
     */
    Attributes getAttributes();
}

Transport Listeners

Listener interfaces for handling transport events.

/**
 * Listener for server transport events
 * Located: io.grpc.internal.ServerTransportListener
 */
interface ServerTransportListener {
    /**
     * Called when a new stream is created on the transport
     * @param stream The new server stream
     * @param method Method descriptor for the RPC
     * @param headers Initial metadata from the client
     */
    void streamCreated(ServerStream stream, String method, Metadata headers);
    
    /**
     * Called when the transport is ready to accept streams
     * @param attributes Updated transport attributes
     */
    void transportReady(Attributes attributes);
    
    /**
     * Called when the transport is shutting down
     * @param status Status indicating reason for shutdown
     */
    void transportTerminated(Status status);
}

/**
 * Listener for client transport events
 * Located: io.grpc.internal.ClientTransportListener
 */
interface ClientTransportListener {
    /**
     * Called when the transport is ready for use
     */
    void transportReady();
    
    /**
     * Called when the transport is shutting down
     * @param status Status indicating reason for shutdown
     */
    void transportShutdown(Status status);
    
    /**
     * Called when the transport is completely terminated
     * @param status Status indicating reason for termination
     */
    void transportTerminated(Status status);
    
    /**
     * Called when transport is in use (has active streams)
     * @param inUse true if transport has active streams
     */
    void transportInUse(boolean inUse);
}

Ping Callback Interface

Callback interface for ping operations.

/**
 * Callback for ping operations
 * Located: io.grpc.internal.ClientTransport.PingCallback
 */
interface PingCallback {
    /**
     * Called when ping operation succeeds
     * @param roundTripTimeNanos Round trip time in nanoseconds
     */
    void onSuccess(long roundTripTimeNanos);
    
    /**
     * Called when ping operation fails
     * @param cause Status indicating the failure reason
     */
    void onFailure(Status cause);
}

Transport Implementations

Delayed Client Transport

Special transport implementation that queues operations until the actual transport is ready.

/**
 * Transport that delays operations until ready
 * Located: io.grpc.internal.DelayedClientTransport
 */
class DelayedClientTransport implements ClientTransport {
    /**
     * Creates a new delayed client transport
     * @param executor Executor for callback operations
     * @param syncContext Synchronization context
     */
    public DelayedClientTransport(Executor executor, SynchronizationContext syncContext);
    
    /**
     * Sets the real transport once it becomes available
     * @param transport The actual transport to delegate to
     */
    public final void setTransport(ClientTransport transport);
    
    /**
     * Shuts down the transport with the given status
     * @param status Status indicating shutdown reason
     * @param errorDescription Optional error description
     */
    public final void shutdownNow(Status status, String errorDescription);
    
    @Override
    public ClientStream newStream(
        MethodDescriptor<?, ?> method,
        Metadata headers,
        CallOptions callOptions,
        ClientStreamTracer[] tracers
    );
    
    @Override
    public void ping(PingCallback callback, Executor executor);
}

Forwarding Client Transport

Base class for transport implementations that delegate to another transport.

/**
 * Base class for forwarding client transports
 * Located: io.grpc.internal.ForwardingClientTransport
 */
abstract class ForwardingClientTransport implements ClientTransport {
    /**
     * Gets the delegate transport
     * @return The transport to forward calls to
     */
    protected abstract ClientTransport delegate();
    
    @Override
    public ClientStream newStream(
        MethodDescriptor<?, ?> method,
        Metadata headers,
        CallOptions callOptions,
        ClientStreamTracer[] tracers
    ) {
        return delegate().newStream(method, headers, callOptions, tracers);
    }
    
    @Override
    public void ping(PingCallback callback, Executor executor) {
        delegate().ping(callback, executor);
    }
}

Transport Usage Patterns

Client Transport Lifecycle

  1. Creation: Transport factory creates transport for target address
  2. Connection: Transport establishes connection to server
  3. Stream Creation: Client creates streams for RPC calls
  4. Communication: Data flows through streams over transport
  5. Shutdown: Transport gracefully closes connections

Example:

// Transport factory creates transport
ClientTransport transport = factory.newClientTransport(serverAddress, options);

// Create stream for RPC
ClientStream stream = transport.newStream(method, headers, callOptions, tracers);

// Use stream for communication
stream.writeMessage(requestMessage);
stream.halfClose();

// Shutdown transport when done
if (transport instanceof ConnectionClientTransport) {
    ((ConnectionClientTransport) transport).shutdown(Status.OK);
}

Server Transport Lifecycle

  1. Binding: Server transport binds to listen address
  2. Starting: Transport starts listening for connections
  3. Stream Handling: Transport creates streams for incoming RPCs
  4. Communication: Data flows through streams over transport
  5. Shutdown: Transport stops accepting new connections and closes existing ones

Example:

// Server transport starts with listener
ServerTransportListener listener = new ServerTransportListener() {
    @Override
    public void streamCreated(ServerStream stream, String method, Metadata headers) {
        // Handle new stream
        ServerStreamListener streamListener = serviceRegistry.lookupMethod(method);
        stream.setListener(streamListener);
    }
    
    @Override
    public void transportReady(Attributes attributes) {
        // Transport ready for connections
    }
};

serverTransport.start(listener);

Error Handling

Transport layer error handling:

  • Connection Failures: Reported via transport listeners with appropriate Status
  • Stream Errors: Individual stream failures don't affect transport
  • Protocol Violations: Result in transport shutdown with PROTOCOL_ERROR status
  • Resource Exhaustion: May trigger flow control or transport shutdown
  • Graceful Shutdown: Allows in-flight RPCs to complete before closing
  • Immediate Shutdown: Cancels all active streams and closes transport

Install with Tessl CLI

npx tessl i tessl/maven-io-grpc--grpc-core

docs

buffer-management.md

builders-factories.md

core-utilities.md

index.md

load-balancing-name-resolution.md

service-providers.md

transport-layer.md

tile.json