Skip to content
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

Ember server graceful shutdown #4179

Merged

Conversation

RaasAhsan
Copy link
Member

Closes #3067. The goal of this PR is to give ember-server a graceful shutdown sequence, which @rossabaker captures succinctly in the original issue. I'd still like to add tests and follow up with some fs2 questions, but I think it's ready for review.

@RaasAhsan RaasAhsan marked this pull request as ready for review January 13, 2021 05:18
// one run. I think we can write a custom combinator that achieves both
sg.server[F](bindAddress, additionalSocketOptions = additionalSocketOptions)
.interruptWhen(shutdown.signal.attempt)
.prefetch
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separately, I'm looking into improving the performance of parJoin and getting rid of this prefetch altogether, but if we're not OK with a slight performance hit we can hold this PR


fixture.test("server shutdown waits for outstanding responses to complete") {
case (server, client) =>
IO.sleep(3.seconds) >> client
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason the server isn't immediately connect-able to after the resource is acquired. I wonder if there's some detail in the NIO API about this, but I have to wait 3 seconds or else I get a Connection refused, which seems to imply that the server socket channel hasn't even been opened yet?


import scala.concurrent.duration._

class EmberServerSuite extends Http4sSuite {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll notice that I didn't actually include any tests for the graceful shutdown mechanism... Unsurprisingly that behavior is rather hard to exercise since there's a lot of nondeterministic elements to it. In particular, we can't deterministically start a request after the server socket channel has been closed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could batter it with requests and assert that every request either completed successfully or was refused a connection. But our CI is suffering enough as it is.

@ChristopherDavenport
Copy link
Member

I got this feedback recently could this be integrated, @djspiewak believe this will also speed up so may cancel out the prefetch costs.

.parJoin(maxConcurrency)
          .interruptWhen(terminationSignal)
          .drain)

to

          .flatMap(s => Stream.resource(s.compile.resource.drain.background))
          .interruptWhen(terminationSignal)
          .drain)

@RaasAhsan
Copy link
Member Author

Does that have the semantics we want? It seems like it's going to start the handler for every socket in a separate fiber, lift that resource into stream, then immediately exit which cancels the fiber (since it introduced a scope). I think resourceWeak would work but then we are possibly leaking fibers, and we also can't control concurrency. Let me know if I'm wrong about that though

Also I realized that the prefetch isn't that bad. It becomes a problem when you have a lot of connection cycling, but it shouldn't affect max RPS all else being equal

@RaasAhsan
Copy link
Member Author

RaasAhsan commented Jan 14, 2021

Removed the comment I added about prefetch, because it really only limits the rate at which connections are accepted, but not max RPS. I'm going to write a special-purpose combinator that mixes the semantics of parJoin, prefetch and drain since they come with a lot of baggage in a follow up, so if you're OK with that, this is pretty much ready :)

Copy link
Member

@ChristopherDavenport ChristopherDavenport left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this seems good.

@rossabaker rossabaker merged commit 5b2ed57 into http4s:series/0.21 Jan 16, 2021
@RaasAhsan RaasAhsan mentioned this pull request Jan 23, 2021
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants