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

builders-factories.mddocs/

Builders and Factories

gRPC Core provides internal builder implementations that serve as the foundation for creating managed channels and servers. These builders are extended by transport-specific modules to provide concrete implementations.

Capabilities

Managed Channel Builder

Internal implementation of the managed channel builder that provides the default channel creation logic.

/**
 * Internal implementation of ManagedChannelBuilder
 * Located: io.grpc.internal.ManagedChannelImplBuilder
 */
class ManagedChannelImplBuilder extends ManagedChannelBuilder<ManagedChannelImplBuilder> {
    /**
     * Sets the target string for the channel
     * @param target The target string (e.g., "dns:///example.com:443")
     * @return This builder for method chaining
     */
    public ManagedChannelImplBuilder target(String target);
    
    /**
     * Sets the client transport factory for creating connections
     * @param clientTransportFactory Factory for creating client transports
     * @return This builder for method chaining
     */
    public ManagedChannelImplBuilder transportFactory(ClientTransportFactory clientTransportFactory);
    
    /**
     * Sets the default load balancing policy
     * @param policy The load balancing policy name
     * @return This builder for method chaining
     */
    public ManagedChannelImplBuilder defaultLoadBalancingPolicy(String policy);
    
    /**
     * Enables full stream decompression
     * @return This builder for method chaining
     */
    public ManagedChannelImplBuilder enableFullStreamDecompression();
    
    /**
     * Sets the maximum message size for inbound messages
     * @param maxInboundMessageSize Maximum size in bytes
     * @return This builder for method chaining
     */
    public ManagedChannelImplBuilder maxInboundMessageSize(int maxInboundMessageSize);
    
    /**
     * Sets the maximum metadata size for inbound messages
     * @param maxInboundMetadataSize Maximum size in bytes
     * @return This builder for method chaining
     */
    public ManagedChannelImplBuilder maxInboundMetadataSize(int maxInboundMetadataSize);
    
    /**
     * Sets the keep alive time
     * @param keepAliveTime The time to wait before sending keep alive
     * @param timeUnit Time unit for the keep alive time
     * @return This builder for method chaining
     */
    public ManagedChannelImplBuilder keepAliveTime(long keepAliveTime, TimeUnit timeUnit);
    
    /**
     * Sets the keep alive timeout
     * @param keepAliveTimeout Timeout for keep alive responses
     * @param timeUnit Time unit for the timeout
     * @return This builder for method chaining
     */
    public ManagedChannelImplBuilder keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit);
    
    /**
     * Enables keep alive without calls
     * @param enable Whether to enable keep alive without active calls
     * @return This builder for method chaining
     */
    public ManagedChannelImplBuilder keepAliveWithoutCalls(boolean enable);
    
    /**
     * Sets the maximum connection idle time
     * @param maxConnectionIdle Time before closing idle connections
     * @param timeUnit Time unit for the idle time
     * @return This builder for method chaining
     */
    public ManagedChannelImplBuilder maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit);
    
    /**
     * Sets the maximum connection age
     * @param maxConnectionAge Maximum age of connections
     * @param timeUnit Time unit for the age
     * @return This builder for method chaining
     */
    public ManagedChannelImplBuilder maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit);
    
    /**
     * Sets the maximum connection age grace period
     * @param maxConnectionAgeGrace Grace period for connection age
     * @param timeUnit Time unit for the grace period
     * @return This builder for method chaining
     */
    public ManagedChannelImplBuilder maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit);
    
    /**
     * Builds the managed channel
     * @return ManagedChannelImpl instance
     */
    public ManagedChannelImpl build();
}

Usage Example:

import io.grpc.internal.ManagedChannelImplBuilder;
import io.grpc.internal.ClientTransportFactory;

// Used internally by transport modules
ManagedChannelImplBuilder builder = new ManagedChannelImplBuilder()
    .target("dns:///example.com:443")
    .transportFactory(transportFactory)
    .defaultLoadBalancingPolicy("pick_first")
    .maxInboundMessageSize(1024 * 1024)
    .keepAliveTime(30, TimeUnit.SECONDS);

ManagedChannel channel = builder.build();

Server Builder

Internal implementation of the server builder that provides the default server creation logic.

/**
 * Internal implementation of ServerBuilder
 * Located: io.grpc.internal.ServerImplBuilder
 */
class ServerImplBuilder extends ServerBuilder<ServerImplBuilder> {
    /**
     * Sets the transport server for accepting connections
     * @param transportServer Server transport implementation
     * @return This builder for method chaining
     */
    public ServerImplBuilder transportServer(io.grpc.internal.InternalServer transportServer);
    
    /**
     * Sets the executor for server operations
     * @param executor Executor for handling server tasks
     * @return This builder for method chaining
     */
    public ServerImplBuilder executor(Executor executor);
    
    /**
     * Sets the decompressor registry
     * @param decompressorRegistry Registry of available decompressors
     * @return This builder for method chaining
     */
    public ServerImplBuilder decompressorRegistry(DecompressorRegistry decompressorRegistry);
    
    /**
     * Sets the compressor registry
     * @param compressorRegistry Registry of available compressors
     * @return This builder for method chaining
     */
    public ServerImplBuilder compressorRegistry(CompressorRegistry compressorRegistry);
    
    /**
     * Sets the maximum message size for inbound messages
     * @param maxInboundMessageSize Maximum size in bytes
     * @return This builder for method chaining
     */
    public ServerImplBuilder maxInboundMessageSize(int maxInboundMessageSize);
    
    /**
     * Sets the maximum metadata size for inbound messages
     * @param maxInboundMetadataSize Maximum size in bytes
     * @return This builder for method chaining
     */
    public ServerImplBuilder maxInboundMetadataSize(int maxInboundMetadataSize);
    
    /**
     * Sets the maximum connection idle time
     * @param maxConnectionIdle Time before closing idle connections
     * @param timeUnit Time unit for the idle time
     * @return This builder for method chaining
     */
    public ServerImplBuilder maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit);
    
    /**
     * Sets the maximum connection age
     * @param maxConnectionAge Maximum age of connections
     * @param timeUnit Time unit for the age
     * @return This builder for method chaining
     */
    public ServerImplBuilder maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit);
    
    /**
     * Sets the maximum connection age grace period
     * @param maxConnectionAgeGrace Grace period for connection age
     * @param timeUnit Time unit for the grace period
     * @return This builder for method chaining
     */
    public ServerImplBuilder maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit);
    
    /**
     * Sets the permit keep alive time
     * @param keepAliveTime Minimum time between keep alive pings
     * @param timeUnit Time unit for the keep alive time
     * @return This builder for method chaining
     */
    public ServerImplBuilder permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit);
    
    /**
     * Sets whether to permit keep alive without calls
     * @param permit Whether to permit keep alive without active calls
     * @return This builder for method chaining
     */
    public ServerImplBuilder permitKeepAliveWithoutCalls(boolean permit);
    
    /**
     * Builds the server
     * @return ServerImpl instance
     */
    public ServerImpl build();
}

Usage Example:

import io.grpc.internal.ServerImplBuilder;
import io.grpc.internal.InternalServer;

// Used internally by transport modules
ServerImplBuilder builder = new ServerImplBuilder()
    .transportServer(transportServer)
    .executor(serverExecutor)
    .maxInboundMessageSize(2 * 1024 * 1024)
    .maxConnectionIdle(5, TimeUnit.MINUTES)
    .permitKeepAliveTime(5, TimeUnit.SECONDS);

Server server = builder.build();

Client Transport Factory

Abstract factory interface for creating client transports.

/**
 * Factory interface for creating client transports
 * Located: io.grpc.internal.ClientTransportFactory
 */
interface ClientTransportFactory {
    /**
     * Creates a new client transport for the given server address
     * @param serverAddress The server address to connect to
     * @param options Transport-specific options
     * @return New ClientTransport instance
     */
    ClientTransport newClientTransport(
        SocketAddress serverAddress,
        ClientTransportOptions options
    );
    
    /**
     * Gets the scheduled executor service for this factory
     * @return ScheduledExecutorService used by transports
     */
    ScheduledExecutorService getScheduledExecutorService();
    
    /**
     * Swaps the channel credentials for this factory
     * @param channelCreds New channel credentials
     * @return New factory instance with updated credentials
     */
    ClientTransportFactory swapChannelCredentials(ChannelCredentials channelCreds);
    
    /**
     * Swaps the call credentials for this factory
     * @param callCreds New call credentials
     * @return New factory instance with updated credentials
     */
    ClientTransportFactory swapCallCredentials(CallCredentials callCreds);
    
    /**
     * Closes the factory and releases resources
     */
    void close();
}

Client Transport Options

Configuration options for client transports.

/**
 * Options for configuring client transports
 * Located: io.grpc.internal.ClientTransportOptions
 */
class ClientTransportOptions {
    /**
     * Gets the authority to use for the connection
     * @return Authority string
     */
    public String getAuthority();
    
    /**
     * Gets the user agent string
     * @return User agent for the connection
     */
    public String getUserAgent();
    
    /**
     * Gets the attributes for the transport
     * @return Transport attributes
     */
    public Attributes getAttributes();
    
    /**
     * Gets the eager transport creation flag
     * @return true if transport should be created eagerly
     */
    public boolean getEagerTransportCreation();
    
    /**
     * Gets the HTTP connect timeout
     * @return Connect timeout duration
     */
    public long getHttpConnectTimeoutNanos();
    
    /**
     * Creates a new builder for transport options
     * @return ClientTransportOptions.Builder instance
     */
    public static Builder newBuilder();
    
    /**
     * Builder for ClientTransportOptions
     */
    public static class Builder {
        public Builder setAuthority(String authority);
        public Builder setUserAgent(String userAgent);
        public Builder setAttributes(Attributes attributes);
        public Builder setEagerTransportCreation(boolean eagerTransportCreation);
        public Builder setHttpConnectTimeoutNanos(long httpConnectTimeoutNanos);
        public ClientTransportOptions build();
    }
}

Factory Usage Patterns

Extending Transport Factories

Transport modules typically extend or implement these interfaces:

// Netty transport example
public class NettyChannelBuilder extends AbstractManagedChannelImplBuilder<NettyChannelBuilder> {
    @Override
    protected ClientTransportFactory buildTransportFactory() {
        return new NettyTransportFactory(
            transportCreationParamsFilter,
            channelz,
            keepAliveManager,
            flowControlWindow,
            maxHeaderListSize
        );
    }
}

Factory Lifecycle

  1. Creation: Transport-specific builders create factory instances
  2. Configuration: Factories are configured with credentials and options
  3. Transport Creation: Factories create transport instances per connection
  4. Resource Management: Factories manage shared resources like thread pools
  5. Cleanup: Factories are closed when channels/servers shut down

Error Handling

Builders and factories handle errors through:

  • Validation: Invalid configurations throw IllegalArgumentException
  • State Checking: Operations on closed resources throw IllegalStateException
  • Transport Errors: Connection failures are propagated as gRPC Status errors
  • Resource Cleanup: Automatic cleanup of resources on shutdown

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