-
Notifications
You must be signed in to change notification settings - Fork 913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Close an HTTP/1.1 connection after delay #5616
Changes from all commits
5b6a9fd
f01ca8a
824bb23
1d764aa
5820fe9
4a7f0ef
90b5509
ae86f29
e74d182
4ec928d
abbfc56
f2c64e6
29308dd
a0c1787
f966235
38d644e
e0155d7
5e25804
151ff7a
12a536d
bbc35b7
cd2d1a4
32cda15
83d7ba2
4a9496f
3b45ee8
51986e3
596b223
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,136 @@ | ||||||||
/* | ||||||||
* Copyright 2024 LINE Corporation | ||||||||
* | ||||||||
* LINE Corporation licenses this file to you under the Apache License, | ||||||||
* version 2.0 (the "License"); you may not use this file except in compliance | ||||||||
* with the License. You may obtain a copy of the License at: | ||||||||
* | ||||||||
* https://www.apache.org/licenses/LICENSE-2.0 | ||||||||
* | ||||||||
* Unless required by applicable law or agreed to in writing, software | ||||||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||||||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||||||||
* License for the specific language governing permissions and limitations | ||||||||
* under the License. | ||||||||
*/ | ||||||||
|
||||||||
package com.linecorp.armeria.server; | ||||||||
|
||||||||
import static org.assertj.core.api.Assertions.assertThat; | ||||||||
import static org.assertj.core.api.Assertions.assertThatCode; | ||||||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||||||||
import static org.awaitility.Awaitility.await; | ||||||||
|
||||||||
import java.io.BufferedReader; | ||||||||
import java.io.IOException; | ||||||||
import java.io.InputStreamReader; | ||||||||
import java.io.PrintWriter; | ||||||||
import java.net.BindException; | ||||||||
import java.net.InetAddress; | ||||||||
import java.net.InetSocketAddress; | ||||||||
import java.net.Socket; | ||||||||
import java.time.Duration; | ||||||||
|
||||||||
import org.junit.jupiter.api.Test; | ||||||||
import org.junit.jupiter.api.extension.RegisterExtension; | ||||||||
|
||||||||
import com.linecorp.armeria.common.Flags; | ||||||||
import com.linecorp.armeria.common.HttpHeaderNames; | ||||||||
import com.linecorp.armeria.common.HttpResponse; | ||||||||
import com.linecorp.armeria.testing.junit5.server.ServerExtension; | ||||||||
|
||||||||
class Http1ServerDelayedCloseConnectionTest { | ||||||||
|
||||||||
@RegisterExtension | ||||||||
static ServerExtension server = new ServerExtension() { | ||||||||
@Override | ||||||||
protected void configure(ServerBuilder sb) { | ||||||||
sb.idleTimeoutMillis(0); | ||||||||
sb.http(0); | ||||||||
sb.https(0); | ||||||||
sb.tlsSelfSigned(); | ||||||||
sb.service("/close", (ctx, req) -> { | ||||||||
return HttpResponse.builder() | ||||||||
.ok() | ||||||||
.content("OK\n") | ||||||||
.header(HttpHeaderNames.CONNECTION, "close") | ||||||||
.build(); | ||||||||
}); | ||||||||
} | ||||||||
}; | ||||||||
|
||||||||
@Test | ||||||||
void shouldDelayDisconnectByServerSideIfClientDoesNotHandleConnectionClose() throws IOException { | ||||||||
try (Socket socket = new Socket("127.0.0.1", server.httpPort())) { | ||||||||
socket.setSoTimeout(100000); | ||||||||
final int socketPort = socket.getLocalPort(); | ||||||||
final PrintWriter writer = new PrintWriter(socket.getOutputStream()); | ||||||||
writer.print("GET /close" + " HTTP/1.1\r\n"); | ||||||||
writer.print("\r\n"); | ||||||||
writer.flush(); | ||||||||
|
||||||||
final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||||||||
assertThat(in.readLine()).isEqualTo("HTTP/1.1 200 OK"); | ||||||||
in.readLine(); // content-type | ||||||||
in.readLine(); // content-length | ||||||||
in.readLine(); // server | ||||||||
in.readLine(); // date | ||||||||
assertThat(in.readLine()).isEqualToIgnoringCase("connection: close"); | ||||||||
assertThat(in.readLine()).isEmpty(); | ||||||||
assertThat(in.readLine()).isEqualToIgnoringCase("OK"); | ||||||||
final long readStartTimestamp = System.nanoTime(); | ||||||||
final int readResult = in.read(); | ||||||||
final long readDurationMillis = Duration.ofNanos(System.nanoTime() - readStartTimestamp).toMillis(); | ||||||||
|
||||||||
assertThat(readResult).isEqualTo(-1); | ||||||||
|
||||||||
final long defaultHttp1ConnectionCloseDelayMillis = Flags.defaultHttp1ConnectionCloseDelayMillis(); | ||||||||
assertThat(readDurationMillis).isBetween( | ||||||||
defaultHttp1ConnectionCloseDelayMillis - 2000, | ||||||||
defaultHttp1ConnectionCloseDelayMillis + 2000 | ||||||||
); | ||||||||
|
||||||||
socket.close(); | ||||||||
try (Socket reuseSock = new Socket()) { | ||||||||
assertThatCode(() -> reuseSock.bind(new InetSocketAddress((InetAddress) null, socketPort))) | ||||||||
.doesNotThrowAnyException(); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
@Test | ||||||||
void shouldWaitForDisconnectByClientSideFirst() throws IOException { | ||||||||
try (Socket socket = new Socket("127.0.0.1", server.httpPort())) { | ||||||||
socket.setSoTimeout(100000); | ||||||||
final int socketPort = socket.getLocalPort(); | ||||||||
final PrintWriter writer = new PrintWriter(socket.getOutputStream()); | ||||||||
writer.print("GET /close" + " HTTP/1.1\r\n"); | ||||||||
writer.print("\r\n"); | ||||||||
writer.flush(); | ||||||||
|
||||||||
final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); | ||||||||
assertThat(in.readLine()).isEqualTo("HTTP/1.1 200 OK"); | ||||||||
in.readLine(); // content-type | ||||||||
in.readLine(); // content-length | ||||||||
in.readLine(); // server | ||||||||
in.readLine(); // date | ||||||||
assertThat(in.readLine()).isEqualToIgnoringCase("connection: close"); | ||||||||
assertThat(in.readLine()).isEmpty(); | ||||||||
assertThat(in.readLine()).isEqualToIgnoringCase("OK"); | ||||||||
|
||||||||
assertThat(server.server().numConnections()).isEqualTo(1); | ||||||||
|
||||||||
socket.close(); | ||||||||
assertThatThrownBy( | ||||||||
() -> { | ||||||||
final Socket reuseSock = new Socket("127.0.0.1", server.httpPort(), null, socketPort); | ||||||||
// close the socket in case initializing the socket doesn't throw an exception | ||||||||
reuseSock.close(); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So that we know why this logic exists
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for good suggestions 🙇 |
||||||||
}) | ||||||||
.isInstanceOf(BindException.class) | ||||||||
.hasMessageContaining("Address already in use"); | ||||||||
|
||||||||
await().untilAsserted(() -> assertThat(server.server().numConnections()).isZero()); | ||||||||
} | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.net.Socket; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.LinkedHashMap; | ||
|
@@ -27,8 +29,6 @@ | |
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import com.google.common.io.ByteStreams; | ||
|
||
import com.linecorp.armeria.common.HttpResponse; | ||
import com.linecorp.armeria.common.HttpStatus; | ||
import com.linecorp.armeria.server.logging.AccessLogWriter; | ||
|
@@ -121,7 +121,11 @@ private static void urlPathAssertion(HttpStatus expected, String path) throws Ex | |
try (Socket s = new Socket(NetUtil.LOCALHOST, server.httpPort())) { | ||
s.setSoTimeout(10000); | ||
s.getOutputStream().write(requestString.getBytes(StandardCharsets.US_ASCII)); | ||
assertThat(new String(ByteStreams.toByteArray(s.getInputStream()), StandardCharsets.US_ASCII)) | ||
final BufferedReader in = new BufferedReader( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question) I think the previous approach of collecting all bytes was correct (in the off-chance that the bytes are fragmented). What do you think of reverting this file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to the following implementation, the With this PR, I changed the server to wait for the client to close first instead of closing the connection immediately. However in this test, the client receives the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, I think I missed this point. I think it's fine to leave as-is then. Thanks for the explanation 👍
I assume this is because |
||
new InputStreamReader(s.getInputStream(), StandardCharsets.US_ASCII)); | ||
// only reads a first line because it only needs to check the expected status | ||
// and does not wait for the server to close the connection | ||
assertThat(in.readLine()) | ||
.as(path) | ||
.startsWith("HTTP/1.1 " + expected); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question) I didn't understand the intention of this test. If the functionality isn't very different from
shouldDelayDisconnectByServerSideIfClientDoesNotHandleConnectionClose
, what do you think of just removing it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
shouldWaitForDisconnectByClientSideFirst
test checks if the server sends theConnection: close
header and waits for the client to close the connection first, rather than closing the connection immediately. It additionally checks if the server closes the connection after the client closes it.On the other hand, the
shouldDelayDisconnectByServerSideIfClientDoesNotHandleConnectionClose
test verifies that if the client receives theConnection: close
header but ignores it without attempting to close the connection, and the close connection task scheduled by the server executes well waiting for the set time on the sever side before closing the connection.Therefore, I wrote two tests because I think the purpose of the two tests and what they verify are different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, I think I missed that point in the last review cycle. Thanks for the explanation 👍