Skip to content

Commit d4fb41d

Browse files
committed
performance: netty: simplify date header service
1 parent bea5fbd commit d4fb41d

File tree

2 files changed

+14
-31
lines changed

2 files changed

+14
-31
lines changed

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyDateService.java

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,27 @@
88
import java.time.ZoneOffset;
99
import java.time.ZonedDateTime;
1010
import java.time.format.DateTimeFormatter;
11-
import java.util.concurrent.RejectedExecutionException;
1211
import java.util.concurrent.ScheduledExecutorService;
1312
import java.util.concurrent.TimeUnit;
14-
import java.util.concurrent.atomic.AtomicReference;
1513

16-
import io.jooby.SneakyThrows;
14+
import io.netty.util.AsciiString;
1715

18-
public class NettyDateService {
19-
private static final AtomicReference<String> cachedDateString = new AtomicReference<>();
20-
private static final Runnable INVALIDATE_TASK = () -> cachedDateString.set(null);
16+
public class NettyDateService implements Runnable {
2117
private static final int DATE_INTERVAL = 1000;
22-
private final ScheduledExecutorService scheduler;
18+
private AsciiString date;
2319

2420
public NettyDateService(ScheduledExecutorService scheduler) {
25-
this.scheduler = scheduler;
21+
scheduler.scheduleAtFixedRate(this, 0, DATE_INTERVAL, TimeUnit.MILLISECONDS);
2622
}
2723

28-
public String date() {
29-
var dateString = cachedDateString.get();
30-
if (dateString == null) {
31-
// set the time and register a timer to invalidate it
32-
// note that this is racey, it does not matter if multiple threads do this
33-
// the perf cost of synchronizing would be more than the perf cost of multiple threads running
34-
// it
35-
long realTime = System.currentTimeMillis();
36-
long mod = realTime % DATE_INTERVAL;
37-
long toGo = DATE_INTERVAL - mod;
38-
dateString = DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneOffset.UTC));
39-
if (cachedDateString.compareAndSet(null, dateString)) {
40-
try {
41-
scheduler.schedule(INVALIDATE_TASK, toGo, TimeUnit.MILLISECONDS);
42-
} catch (RejectedExecutionException rejected) {
43-
if (!scheduler.isShutdown()) {
44-
throw SneakyThrows.propagate(rejected);
45-
}
46-
}
47-
}
48-
}
49-
return dateString;
24+
public AsciiString date() {
25+
return this.date;
26+
}
27+
28+
@Override
29+
public void run() {
30+
this.date =
31+
new AsciiString(
32+
DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneOffset.UTC)));
5033
}
5134
}

modules/jooby-netty/src/main/java/io/jooby/netty/NettyServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public Server start(@NonNull Jooby application) {
139139
var transport = NettyTransport.transport(application.getClassLoader());
140140

141141
/* Acceptor event-loop */
142-
this.acceptorloop = transport.createEventLoop(1, "acceptor", _100);
142+
this.acceptorloop = transport.createEventLoop(1, "acceptor", _50);
143143

144144
/* Event loop: processing connections, parsing messages and doing engine's internal work */
145145
this.eventloop = transport.createEventLoop(options.getIoThreads(), "eventloop", _100);

0 commit comments

Comments
 (0)