Skip to content

Commit 8c37006

Browse files
committed
server: make server options available on app initialization
- code clean up
1 parent 2f39651 commit 8c37006

File tree

16 files changed

+95
-124
lines changed

16 files changed

+95
-124
lines changed

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.util.function.Function;
3434
import java.util.function.Predicate;
3535
import java.util.function.Supplier;
36-
import java.util.stream.Collectors;
3736

3837
import org.slf4j.Logger;
3938
import org.slf4j.LoggerFactory;
@@ -89,6 +88,7 @@ public class Jooby implements Router, Registry {
8988
private static Jooby owner;
9089
private static ExecutionMode BOOT_EXECUTION_MODE = ExecutionMode.DEFAULT;
9190
private static OutputFactory OUTPUT_FACTORY;
91+
private static ServerOptions SERVER_OPTIONS;
9292

9393
private RouterImpl router;
9494

@@ -136,6 +136,7 @@ public Jooby() {
136136
// NOTE: fallback to default, this is required for direct instance creation of class
137137
// app bootstrap always ensures server instance.
138138
router.setOutputFactory(Optional.ofNullable(OUTPUT_FACTORY).orElseGet(OutputFactory::create));
139+
router.setServerOptions(Optional.ofNullable(SERVER_OPTIONS).orElseGet(ServerOptions::new));
139140
} else {
140141
copyState(owner, this);
141142
}
@@ -444,7 +445,7 @@ public String getContextPath() {
444445
return router;
445446
}
446447

447-
@Nullable @Override
448+
@NonNull @Override
448449
public ServerOptions getServerOptions() {
449450
return router.getServerOptions();
450451
}
@@ -961,8 +962,7 @@ public Jooby setStartupSummary(List<StartupSummary> startupSummary) {
961962
if (config.hasPath(AvailableSettings.STARTUP_SUMMARY)) {
962963
Object value = config.getAnyRef(AvailableSettings.STARTUP_SUMMARY);
963964
List<String> values = value instanceof List ? (List) value : List.of(value.toString());
964-
startupSummary =
965-
values.stream().map(StartupSummary::create).collect(Collectors.toUnmodifiableList());
965+
startupSummary = values.stream().map(StartupSummary::create).toList();
966966
} else {
967967
startupSummary = List.of(StartupSummary.DEFAULT, StartupSummary.ROUTES);
968968
}
@@ -1301,11 +1301,13 @@ public static Jooby createApp(
13011301
Jooby app;
13021302
try {
13031303
Jooby.OUTPUT_FACTORY = server.getOutputFactory();
1304+
Jooby.SERVER_OPTIONS = server.getOptions();
13041305
Jooby.BOOT_EXECUTION_MODE = executionMode;
13051306
app = provider.get();
13061307
} finally {
13071308
Jooby.BOOT_EXECUTION_MODE = executionMode;
13081309
Jooby.OUTPUT_FACTORY = null;
1310+
Jooby.SERVER_OPTIONS = null;
13091311
}
13101312

13111313
return app;

jooby/src/main/java/io/jooby/Router.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,11 @@ default Object execute(@NonNull Context context) {
182182
@NonNull ServiceRegistry getServices();
183183

184184
/**
185-
* Server options. They might be null during application initialization. Once deployed, they are
186-
* never null (at runtime).
185+
* Server options.
187186
*
188-
* @return Server options or <code>null</code>.
187+
* @return Server options.
189188
*/
190-
@Nullable ServerOptions getServerOptions();
189+
@NonNull ServerOptions getServerOptions();
191190

192191
/**
193192
* Set application context path. Context path is the base path for all routes. Default is: <code>/

jooby/src/main/java/io/jooby/Server.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ public Server setOptions(@NonNull ServerOptions options) {
156156
return this;
157157
}
158158

159-
protected abstract ServerOptions defaultOptions();
159+
protected ServerOptions defaultOptions() {
160+
return new ServerOptions();
161+
}
160162
}
161163

162164
@NonNull OutputFactory getOutputFactory();
@@ -298,11 +300,6 @@ static Server loadServer(@Nullable ServerOptions options) {
298300
var log = LoggerFactory.getLogger(servers.get(0).getClass());
299301
log.warn("Multiple servers found {}. Using: {}", names, names.get(0));
300302
}
301-
var server = servers.get(0);
302-
if (options != null) {
303-
options.setServer(server.getName());
304-
server.setOptions(options);
305-
}
306-
return server;
303+
return servers.get(0);
307304
}
308305
}

jooby/src/main/java/io/jooby/ServerOptions.java

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,28 @@ public class ServerOptions {
7575
public static final int IO_THREADS =
7676
Integer.parseInt(
7777
System.getProperty(
78-
"jooby.server.ioThreads",
79-
Integer.toString(Runtime.getRuntime().availableProcessors())));
78+
"server.ioThreads", Integer.toString(Runtime.getRuntime().availableProcessors())));
79+
80+
private static final String SERVER_NAME = System.getProperty("server.name");
8081

8182
/**
8283
* Number of worker (a.k.a application) threads. It is the number of processors multiply by <code>
8384
* 8</code>.
8485
*/
85-
public static final int WORKER_THREADS = Runtime.getRuntime().availableProcessors() * 8;
86+
public static final int WORKER_THREADS =
87+
Integer.parseInt(
88+
System.getProperty(
89+
"server.workerThreads",
90+
Integer.toString(Runtime.getRuntime().availableProcessors() * 8)));
8691

8792
/** HTTP port. Default is <code>8080</code> or <code>0</code> for random port. */
8893
private int port = SERVER_PORT;
8994

9095
/** Number of IO threads used by the server. Used by Netty and Undertow. */
91-
private Integer ioThreads;
96+
private int ioThreads = IO_THREADS;
9297

9398
/** Number of worker threads (a.k.a application) to use. */
94-
private Integer workerThreads;
99+
private int workerThreads = WORKER_THREADS;
95100

96101
/**
97102
* Configure server to default headers: <code>Date</code>, <code>Content-Type</code> and <code>
@@ -100,9 +105,9 @@ public class ServerOptions {
100105
private boolean defaultHeaders = true;
101106

102107
/** Name of server: Jetty, Netty or Undertow. */
103-
private String server;
108+
private String server = SERVER_NAME;
104109

105-
private OutputOptions buffer = OutputOptions.defaults();
110+
private OutputOptions output = OutputOptions.defaults();
106111

107112
/**
108113
* Maximum request size in bytes. Request exceeding this value results in {@link
@@ -150,11 +155,11 @@ public class ServerOptions {
150155
if (conf.hasPath("server.name")) {
151156
options.setServer(conf.getString("server.name"));
152157
}
153-
if (conf.hasPath("server.buffer.size")) {
154-
options.buffer.setSize(conf.getInt("server.buffer.size"));
158+
if (conf.hasPath("server.output.size")) {
159+
options.output.setSize(conf.getInt("server.output.size"));
155160
}
156-
if (conf.hasPath("server.buffer.useDirectBuffers")) {
157-
options.buffer.setDirectBuffers(conf.getBoolean("server.buffer.useDirectBuffers"));
161+
if (conf.hasPath("server.output.useDirectBuffers")) {
162+
options.output.setDirectBuffers(conf.getBoolean("server.output.useDirectBuffers"));
158163
}
159164
if (conf.hasPath("server.defaultHeaders")) {
160165
options.setDefaultHeaders(conf.getBoolean("server.defaultHeaders"));
@@ -197,11 +202,9 @@ public String toString() {
197202
StringBuilder buff = new StringBuilder();
198203
buff.append(Optional.ofNullable(server).orElse("server")).append(" {");
199204
buff.append("port: ").append(port);
200-
if (!"jetty".equals(server)) {
201-
buff.append(", ioThreads: ").append(Optional.ofNullable(ioThreads).orElse(IO_THREADS));
202-
}
205+
buff.append(", ioThreads: ").append(getIoThreads());
203206
buff.append(", workerThreads: ").append(getWorkerThreads());
204-
buff.append(", buffer: ").append(getBuffer());
207+
buff.append(", output: ").append(getOutput());
205208
buff.append(", maxRequestSize: ").append(maxRequestSize);
206209
buff.append(", httpsOnly: ").append(httpsOnly);
207210
if (compressionLevel != null) {
@@ -311,17 +314,7 @@ public boolean isHttpsOnly() {
311314
* @return Number of IO threads used by the server. Required by Netty and Undertow.
312315
*/
313316
public int getIoThreads() {
314-
return getIoThreads(IO_THREADS);
315-
}
316-
317-
/**
318-
* Number of IO threads used by the server. Required by Netty and Undertow.
319-
*
320-
* @param defaultIoThreads Default number of threads if none was set.
321-
* @return Number of IO threads used by the server. Required by Netty and Undertow.
322-
*/
323-
public int getIoThreads(int defaultIoThreads) {
324-
return ioThreads == null ? defaultIoThreads : ioThreads;
317+
return ioThreads;
325318
}
326319

327320
/**
@@ -343,19 +336,7 @@ public int getIoThreads(int defaultIoThreads) {
343336
* allowed to block.
344337
*/
345338
public int getWorkerThreads() {
346-
return getWorkerThreads(WORKER_THREADS);
347-
}
348-
349-
/**
350-
* Number of worker threads (a.k.a application) to use. These are the threads which are allowed to
351-
* block.
352-
*
353-
* @param defaultWorkerThreads Default worker threads is none was set.
354-
* @return Number of worker threads (a.k.a application) to use. These are the threads which are
355-
* allowed to block.
356-
*/
357-
public int getWorkerThreads(int defaultWorkerThreads) {
358-
return workerThreads == null ? defaultWorkerThreads : workerThreads;
339+
return workerThreads;
359340
}
360341

361342
/**
@@ -415,12 +396,12 @@ public boolean getDefaultHeaders() {
415396
return this;
416397
}
417398

418-
public OutputOptions getBuffer() {
419-
return buffer;
399+
public OutputOptions getOutput() {
400+
return output;
420401
}
421402

422-
public ServerOptions setBuffer(@NonNull OutputOptions buffer) {
423-
this.buffer = buffer;
403+
public ServerOptions setOutput(@NonNull OutputOptions output) {
404+
this.output = output;
424405
return this;
425406
}
426407

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
import com.typesafe.config.Config;
3939
import edu.umd.cs.findbugs.annotations.NonNull;
40-
import edu.umd.cs.findbugs.annotations.Nullable;
4140
import io.jooby.*;
4241
import io.jooby.exception.RegistryException;
4342
import io.jooby.exception.StatusCodeException;
@@ -172,6 +171,8 @@ public Stack executor(Executor executor) {
172171

173172
private OutputFactory outputFactory;
174173

174+
private ServerOptions serverOptions;
175+
175176
public RouterImpl() {
176177
stack.addLast(new Stack(chi, null));
177178
}
@@ -783,9 +784,14 @@ public Router setFlashCookie(@NonNull Cookie flashCookie) {
783784
return this;
784785
}
785786

786-
@Nullable @Override
787+
@NonNull @Override
787788
public ServerOptions getServerOptions() {
788-
return services.getOrNull(ServerOptions.class);
789+
return serverOptions;
790+
}
791+
792+
public void setServerOptions(ServerOptions serverOptions) {
793+
this.serverOptions = serverOptions;
794+
services.put(ServerOptions.class, serverOptions);
789795
}
790796

791797
@NonNull @Override

jooby/src/test/java/io/jooby/Issue3653.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public OutputFactory getOutputFactory() {
2626
return null;
2727
}
2828

29-
@Override
3029
protected ServerOptions defaultOptions() {
3130
return defaultOptions;
3231
}

jooby/src/test/java/io/jooby/buffer/Issue3434.java renamed to jooby/src/test/java/io/jooby/output/Issue3434.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
44
* Copyright 2014 Edgar Espina
55
*/
6-
package io.jooby.buffer;
6+
package io.jooby.output;
77

88
import static org.junit.jupiter.api.Assertions.assertEquals;
99

@@ -15,8 +15,6 @@
1515
import org.junit.jupiter.api.Test;
1616

1717
import io.jooby.SneakyThrows;
18-
import io.jooby.output.OutputFactory;
19-
import io.jooby.output.OutputOptions;
2018

2119
public class Issue3434 {
2220
OutputFactory factory = OutputFactory.create(OutputOptions.small());

jooby/src/test/java/io/jooby/buffer/OutputTest.java renamed to jooby/src/test/java/io/jooby/output/OutputTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
44
* Copyright 2014 Edgar Espina
55
*/
6-
package io.jooby.buffer;
6+
package io.jooby.output;
77

88
import static org.junit.jupiter.api.Assertions.assertEquals;
99

@@ -17,8 +17,6 @@
1717
import io.jooby.SneakyThrows;
1818
import io.jooby.internal.output.ByteBufferWrappedOutput;
1919
import io.jooby.internal.output.CompsiteByteBufferOutput;
20-
import io.jooby.output.ByteBufferOutput;
21-
import io.jooby.output.Output;
2220

2321
public class OutputTest {
2422

modules/jooby-jetty/src/main/java/io/jooby/jetty/JettyServer.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@
4747
*/
4848
public class JettyServer extends io.jooby.Server.Base {
4949

50+
private static final String NAME = "jetty";
5051
private static final int THREADS = 200;
5152

5253
static {
5354
int cpus = ProcessorUtils.availableProcessors();
5455
var ioThreads = Math.max(1, Math.min(cpus / 2, THREADS / 16));
55-
System.setProperty("jooby.server.ioThreads", ioThreads + "");
56+
System.setProperty("server.ioThreads", ioThreads + "");
57+
System.setProperty("server.workerThreads", THREADS + "");
58+
System.setProperty("server.name", NAME);
5659
}
5760

5861
private OutputFactory outputFactory;
@@ -83,25 +86,14 @@ public JettyServer() {}
8386
@NonNull @Override
8487
public OutputFactory getOutputFactory() {
8588
if (outputFactory == null) {
86-
this.outputFactory = OutputFactory.create(getOptions().getBuffer());
89+
this.outputFactory = OutputFactory.create(getOptions().getOutput());
8790
}
8891
return outputFactory;
8992
}
9093

91-
@NonNull @Override
92-
public JettyServer setOptions(@NonNull ServerOptions options) {
93-
super.setOptions(options.setWorkerThreads(options.getWorkerThreads(THREADS)));
94-
return this;
95-
}
96-
97-
@Override
98-
protected ServerOptions defaultOptions() {
99-
return new ServerOptions().setServer(getName()).setWorkerThreads(THREADS);
100-
}
101-
10294
@NonNull @Override
10395
public String getName() {
104-
return "jetty";
96+
return NAME;
10597
}
10698

10799
/**
@@ -150,8 +142,8 @@ public io.jooby.Server start(@NonNull Jooby... application) {
150142

151143
var httpConf = new HttpConfiguration();
152144
httpConf.setUriCompliance(UriCompliance.LEGACY);
153-
httpConf.setOutputBufferSize(options.getBuffer().getSize());
154-
httpConf.setOutputAggregationSize(options.getBuffer().getSize());
145+
httpConf.setOutputBufferSize(options.getOutput().getSize());
146+
httpConf.setOutputAggregationSize(options.getOutput().getSize());
155147
httpConf.setSendXPoweredBy(false);
156148
httpConf.setSendDateHeader(options.getDefaultHeaders());
157149
httpConf.setSendServerVersion(false);
@@ -302,7 +294,7 @@ private List<Map.Entry<String, Handler>> createHandler(
302294
new JettyHandler(
303295
invocationType,
304296
applications.get(0),
305-
options.getBuffer().getSize(),
297+
options.getOutput().getSize(),
306298
options.getMaxRequestSize(),
307299
options.getDefaultHeaders());
308300
if (options.isExpectContinue() == Boolean.TRUE) {

0 commit comments

Comments
 (0)