Created
July 30, 2019 20:08
-
-
Save s4nchez/7f308483ac2402d9d429ff2b319b5b24 to your computer and use it in GitHub Desktop.
Configuring undertow with connection timeout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.http4k.server | |
import com.natpryce.hamkrest.assertion.assertThat | |
import com.natpryce.hamkrest.equalTo | |
import io.undertow.UndertowOptions | |
import io.undertow.server.handlers.BlockingHandler | |
import org.http4k.client.ApacheClient | |
import org.http4k.core.BodyMode | |
import org.http4k.core.HttpHandler | |
import org.http4k.core.Method | |
import org.http4k.core.Request | |
import org.http4k.core.Response | |
import org.http4k.core.Status.Companion.OK | |
import org.junit.jupiter.api.Test | |
import org.junit.jupiter.api.fail | |
import java.io.InputStream | |
import java.io.PipedInputStream | |
import java.io.PipedOutputStream | |
import java.net.InetSocketAddress | |
import java.net.SocketException | |
import kotlin.concurrent.thread | |
class UndertowTest { | |
@Test | |
fun `server with aggressive connection timeout`() { | |
{ _: Request -> Response(OK).body("done") }.asServer(CustomUndertow(8123)).start() | |
val client = ApacheClient(requestBodyMode = BodyMode.Stream) | |
try { | |
client(Request(Method.POST, "http://localhost:8123").body(beeper())) | |
fail("should have blown up") | |
} catch (e: SocketException) { | |
assertThat(e.message, equalTo("Connection reset")) | |
} | |
} | |
private fun beeper(): InputStream { | |
val input = PipedInputStream() | |
val output = PipedOutputStream(input) | |
val line = "beep\n" | |
thread { | |
repeat(10) { | |
output.write(line.toByteArray()) | |
output.flush() | |
Thread.sleep(100) | |
} | |
output.close() | |
} | |
return input | |
} | |
} | |
data class CustomUndertow(val port: Int = 8000, val enableHttp2: Boolean) : ServerConfig { | |
constructor(port: Int = 8000) : this(port, false) | |
override fun toServer(httpHandler: HttpHandler): Http4kServer = | |
object : Http4kServer { | |
val server = io.undertow.Undertow.builder() | |
.addHttpListener(port, "0.0.0.0") | |
.setServerOption(UndertowOptions.NO_REQUEST_TIMEOUT, 200) | |
.setHandler(BlockingHandler(HttpUndertowHandler(httpHandler))).build() | |
override fun start() = apply { server.start() } | |
override fun stop() = apply { server.stop() } | |
override fun port(): Int = if (port > 0) port else (server.listenerInfo[0].address as InetSocketAddress).port | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment