blob: cd82a3b2ff61902b0e5338ce705dcdbc40f01dff (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty;
import org.eclipse.jetty.http2.HTTP2Connection;
import org.eclipse.jetty.http2.ISession;
import org.eclipse.jetty.http2.server.HTTP2ServerConnection;
import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.annotation.Name;
import org.eclipse.jetty.util.component.Dumpable;
import org.eclipse.jetty.util.component.Graceful;
import org.eclipse.jetty.util.component.LifeCycle;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;
/**
* Workaround for <a href="https://github.com/eclipse/jetty.project/issues/8811">eclipse/jetty.project#8811</a>.
*
* @author bjorncs
*/
class FixedHTTP2ServerConnectionFactory extends HTTP2ServerConnectionFactory {
private static final Logger log = Logger.getLogger(FixedHTTP2ServerConnectionFactory.class.getName());
private final HTTP2SessionContainer originalSessionContainer;
private final FixedHTTP2SessionContainer fixedSessionContainer;
FixedHTTP2ServerConnectionFactory(@Name("config") HttpConfiguration config) {
super(config);
fixedSessionContainer = new FixedHTTP2SessionContainer();
originalSessionContainer = getBean(HTTP2SessionContainer.class);
removeBean(originalSessionContainer);
addBean(fixedSessionContainer);
}
@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
var conn = (HTTP2ServerConnection) super.newConnection(connector, endPoint);
conn.removeEventListener(originalSessionContainer);
conn.addEventListener(fixedSessionContainer);
return conn;
}
@ManagedObject("The container of HTTP/2 sessions")
private static class FixedHTTP2SessionContainer implements Connection.Listener, Graceful, Dumpable {
private final Object monitor = new Object();
private final Set<ISession> sessions = new HashSet<>();
private CompletableFuture<Void> shutdown;
@Override
public void onOpened(Connection conn) {
var session = session(conn);
boolean shuttingDown;
synchronized (monitor) {
sessions.add(session);
shuttingDown = shutdown != null;
}
log.fine(() -> "Added session %s".formatted(session));
LifeCycle.start(session);
if (shuttingDown) session.shutdown();
}
@Override
public void onClosed(Connection conn) {
var session = session(conn);
boolean removed;
CompletableFuture<Void> shutdown;
synchronized (monitor) {
removed = sessions.remove(session);
shutdown = this.shutdown != null && sessions.size() == 0 && !this.shutdown.isDone()
? this.shutdown : null;
}
log.fine(() -> "Removed session %s".formatted(session));
if (removed) LifeCycle.stop(session);
if (shutdown != null) {
log.fine("Shutdown completed after last session removed");
shutdown.complete(null);
}
}
@Override
public void dump(Appendable out, String indent) throws IOException {
synchronized (monitor) { Dumpable.dumpObjects(out, indent, this, sessions); }
}
@Override public CompletableFuture<Void> shutdown() {
CompletableFuture<Void> shutdown = null;
ISession[] sessionsToClose = null;
synchronized (monitor) {
if (this.shutdown == null) {
shutdown = (this.shutdown = new CompletableFuture<>());
sessionsToClose = sessions.toArray(ISession[]::new);
}
}
if (sessionsToClose != null) {
log.fine("Shutdown initiated");
if (sessionsToClose.length > 0) {
for (ISession session : sessionsToClose) {
session.shutdown();
}
} else {
log.fine("Shutdown completed since no sessions");
shutdown.complete(null);
}
}
return shutdown;
}
@Override public boolean isShutdown() { synchronized (monitor) { return shutdown != null; } }
private static ISession session(Connection conn) { return ((HTTP2Connection)conn).getSession(); }
}
}
|