CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-grpc--grpc-inprocess

High-performance in-process transport implementation for gRPC Java that enables direct communication between client and server within the same JVM process

Pending
Overview
Eval results
Files

socket-addresses.mddocs/

Socket Addresses

The gRPC In-Process Transport provides two types of socket addresses for identifying servers: named addresses using InProcessSocketAddress and anonymous addresses using AnonymousInProcessSocketAddress. These addresses enable different patterns for server identification and client connection.

Capabilities

InProcessSocketAddress

A socket address that identifies an in-process server by name. Multiple clients can connect to the same named server.

/**
 * Custom SocketAddress class for InProcessTransport.
 */
public final class InProcessSocketAddress extends SocketAddress {
    /**
     * Construct an address for a server identified by name.
     * @param name The name of the inprocess server
     */
    public InProcessSocketAddress(String name);
    
    /**
     * Gets the name of the inprocess server.
     * @return the server name
     */
    public String getName();
    
    /**
     * Returns the server name.
     * @return the server name
     */
    public String toString();
    
    /**
     * Hash code based on server name.
     * @return hash code
     */
    public int hashCode();
    
    /**
     * Returns true if the object is of the same type and server names match.
     * @param obj the object to compare
     * @return true if equal
     */
    public boolean equals(Object obj);
}

Usage Examples:

// Create named address
InProcessSocketAddress address = new InProcessSocketAddress("user-service");

// Get server name
String serverName = address.getName(); // Returns "user-service"

// Use with builders
InProcessServerBuilder serverBuilder = 
    InProcessServerBuilder.forAddress(address);
InProcessChannelBuilder channelBuilder = 
    InProcessChannelBuilder.forAddress(address);

// Addresses with same name are equal
InProcessSocketAddress addr1 = new InProcessSocketAddress("service");
InProcessSocketAddress addr2 = new InProcessSocketAddress("service");
boolean isEqual = addr1.equals(addr2); // Returns true

AnonymousInProcessSocketAddress

A socket address for anonymous in-process servers that can only be referenced via the specific address instance. This provides better isolation than named servers.

/**
 * Custom SocketAddress class for InProcessTransport, for a server which 
 * can only be referenced via this address instance.
 */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/8626")
public final class AnonymousInProcessSocketAddress extends SocketAddress {
    /**
     * Creates a new AnonymousInProcessSocketAddress.
     */
    public AnonymousInProcessSocketAddress();
}

Usage Examples:

// Create anonymous address
AnonymousInProcessSocketAddress address = new AnonymousInProcessSocketAddress();

// Create server with anonymous address
Server server = InProcessServerBuilder.forAddress(address)
    .addService(new MyServiceImpl())
    .build()
    .start();

// Create channel using the same address instance
Channel channel = InProcessChannelBuilder.forAddress(address)
    .build();

// Only this specific address instance can connect to the server
// Creating a new AnonymousInProcessSocketAddress() will NOT connect to the same server

Address Types Comparison

FeatureInProcessSocketAddressAnonymousInProcessSocketAddress
IdentificationNamed (string-based)Instance-based
Multiple Clients✅ Yes✅ Yes
Server DiscoveryBy name lookupBy address instance reference
IsolationShared namespaceComplete isolation
Use CaseGeneral purpose, testing with known namesMaximum isolation, advanced testing scenarios
StatusStableExperimental

Usage Patterns

Named Server Pattern

Use InProcessSocketAddress when you need multiple clients to connect to the same server or when you want to use human-readable server names.

String serviceName = "order-service";
InProcessSocketAddress address = new InProcessSocketAddress(serviceName);

// Server setup
Server server = InProcessServerBuilder.forAddress(address)
    .addService(new OrderServiceImpl())
    .build()
    .start();

// Multiple clients can connect using the same name
Channel channel1 = InProcessChannelBuilder.forAddress(address).build();
Channel channel2 = InProcessChannelBuilder.forName(serviceName).build(); // Equivalent

Anonymous Server Pattern

Use AnonymousInProcessSocketAddress when you need complete isolation and want to ensure only specific code can access the server.

// Create anonymous address
AnonymousInProcessSocketAddress address = new AnonymousInProcessSocketAddress();

// Server setup - only accessible via this address instance
Server server = InProcessServerBuilder.forAddress(address)
    .addService(new SecureServiceImpl())
    .build()
    .start();

// Only this specific address can connect
Channel channel = InProcessChannelBuilder.forAddress(address).build();

// This would create a DIFFERENT anonymous server (not connected)
// AnonymousInProcessSocketAddress differentAddress = new AnonymousInProcessSocketAddress();

Testing Isolation

Anonymous addresses provide better test isolation:

@Test
void testServiceA() {
    AnonymousInProcessSocketAddress address = new AnonymousInProcessSocketAddress();
    // This test's server is completely isolated
    Server server = InProcessServerBuilder.forAddress(address)
        .addService(new ServiceAImpl())
        .build()
        .start();
    
    Channel channel = InProcessChannelBuilder.forAddress(address).build();
    // Test implementation...
}

@Test
void testServiceB() {
    AnonymousInProcessSocketAddress address = new AnonymousInProcessSocketAddress();
    // This test's server is also completely isolated
    Server server = InProcessServerBuilder.forAddress(address)
        .addService(new ServiceBImpl())
        .build()
        .start();
    
    Channel channel = InProcessChannelBuilder.forAddress(address).build();
    // Test implementation...
}

Error Handling

InProcessSocketAddress

  • NullPointerException - Thrown if name parameter is null in constructor
  • Name conflicts are handled at the server level during registration

AnonymousInProcessSocketAddress

  • IOException - Can be thrown during server registration if address is already in use
  • Thread-safe for concurrent access to the same address instance

Thread Safety

Both address types are thread-safe:

  • InProcessSocketAddress - All methods are thread-safe. Multiple threads can safely access the same address instance concurrently.
  • AnonymousInProcessSocketAddress - Internally synchronized for thread-safe server registration and access. Safe for concurrent use across multiple threads.

Performance Considerations

  • InProcessSocketAddress uses string-based lookups in a concurrent map
  • AnonymousInProcessSocketAddress uses direct object references for faster access
  • Both address types have minimal memory overhead
  • Address equality checks are optimized for performance

Install with Tessl CLI

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

docs

channel-builder.md

index.md

server-builder.md

socket-addresses.md

tile.json