|
73 | 73 | public class GrpcModule implements Extension { |
74 | 74 | private final List<BindableService> services = new ArrayList<>(); |
75 | 75 | private final List<Class<? extends BindableService>> serviceClasses = new ArrayList<>(); |
| 76 | + private SneakyThrows.Consumer<InProcessServerBuilder> serverCustomizer; |
| 77 | + private SneakyThrows.Consumer<InProcessChannelBuilder> channelCustomizer; |
76 | 78 |
|
77 | 79 | static { |
78 | 80 | // Optionally remove existing handlers attached to the j.u.l root logger |
@@ -111,6 +113,30 @@ public final GrpcModule bind(Class<? extends BindableService>... serviceClasses) |
111 | 113 | return this; |
112 | 114 | } |
113 | 115 |
|
| 116 | + /** |
| 117 | + * Customizes the in-process gRPC server using the provided server customizer. This method accepts |
| 118 | + * a consumer that applies custom settings to an {@code InProcessServerBuilder} instance. |
| 119 | + * |
| 120 | + * @param serverCustomizer a consumer to customize the {@code InProcessServerBuilder}. |
| 121 | + * @return this {@code GrpcModule} instance for method chaining. |
| 122 | + */ |
| 123 | + public GrpcModule withServer(SneakyThrows.Consumer<InProcessServerBuilder> serverCustomizer) { |
| 124 | + this.serverCustomizer = serverCustomizer; |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Configures the gRPC channel using a consumer that applies custom settings to an {@code |
| 130 | + * InProcessChannelBuilder} instance. |
| 131 | + * |
| 132 | + * @param channelConsumer a consumer to customize the {@code InProcessChannelBuilder}. |
| 133 | + * @return this {@code GrpcModule} instance for method chaining. |
| 134 | + */ |
| 135 | + public GrpcModule withChannel(SneakyThrows.Consumer<InProcessChannelBuilder> channelConsumer) { |
| 136 | + this.channelCustomizer = channelConsumer; |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
114 | 140 | /** |
115 | 141 | * Installs the gRPC extension into the Jooby application. * |
116 | 142 | * |
@@ -142,12 +168,22 @@ public void install(Jooby app) throws Exception { |
142 | 168 | var service = app.require(serviceClass); |
143 | 169 | bindService(app, builder, registry, service); |
144 | 170 | } |
| 171 | + // Sync both |
| 172 | + builder.maxInboundMessageSize(app.getServerOptions().getMaxRequestSize()); |
| 173 | + builder.maxInboundMetadataSize(app.getServerOptions().getMaxRequestSize()); |
| 174 | + if (serverCustomizer != null) { |
| 175 | + serverCustomizer.accept(builder); |
| 176 | + } |
145 | 177 | var grpcServer = builder.build().start(); |
146 | 178 |
|
147 | 179 | // KEEP .directExecutor() here! |
148 | 180 | // This ensures that when the background gRPC worker finishes, it instantly pushes |
149 | 181 | // the response back to Undertow/Netty without wasting time on another thread hop. |
150 | | - var channel = InProcessChannelBuilder.forName(serverName).directExecutor().build(); |
| 182 | + var channelBuilder = InProcessChannelBuilder.forName(serverName).directExecutor(); |
| 183 | + if (channelCustomizer != null) { |
| 184 | + channelCustomizer.accept(channelBuilder); |
| 185 | + } |
| 186 | + var channel = channelBuilder.build(); |
151 | 187 | processor.setChannel(channel); |
152 | 188 |
|
153 | 189 | app.onStop(channel::shutdownNow); |
|
0 commit comments