Skip to content

Commit c1d75b8

Browse files
Benjamin Erichsenjenkins
authored andcommitted
twitter-server: Add a readiness endpoint to query for accepting traffic.
Problem We need an endpoint where we can rely on the HTTP status code to tell us if the server is ready to accept traffic. Solution Add a new endpoint called /ready that returns a 503 when the server is warming up and a 200 when the server finishes warming up and is ready for traffic. Differential Revision: https://phabricator.twitter.biz/D1240266
1 parent f39efac commit c1d75b8

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

server/src/main/scala/com/twitter/server/Lifecycle.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,12 @@ object Lifecycle {
8484
private[Lifecycle] object Warmup {
8585

8686
/**
87-
* Initialize warmup code. Ensures that the /health endpoint will not return on "OK" response.
87+
* Initialize warmup code. Ensures that the /health endpoint will not return an "OK" response.
8888
*/
89-
def initializeWarmup(): Unit =
89+
def initializeWarmup(): Unit = {
9090
HttpMuxer.addHandler(Route("/health", new ReplyHandler("warming up\n")))
91+
HttpMuxer.addHandler(Route("/ready", new ReadinessHandler(isReady = false)))
92+
}
9193

9294
/**
9395
* Prebind warmup code. Used for warmup tasks that we want to run before we
@@ -98,8 +100,10 @@ object Lifecycle {
98100
/**
99101
* The service is bound to a port and warmed up, announce health.
100102
*/
101-
def warmupComplete(): Unit =
103+
def warmupComplete(): Unit = {
102104
HttpMuxer.addHandler(Route("/health", new ReplyHandler("OK\n")))
105+
HttpMuxer.addHandler(Route("/ready", new ReadinessHandler(isReady = true)))
106+
}
103107

104108
}
105109

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.twitter.server.handler
2+
3+
import com.twitter.finagle.Service
4+
import com.twitter.finagle.http.Request
5+
import com.twitter.finagle.http.Response
6+
import com.twitter.finagle.http.Status
7+
import com.twitter.io.Buf
8+
import com.twitter.server.util.HttpUtils.newOk
9+
import com.twitter.server.util.HttpUtils.newResponse
10+
import com.twitter.util.Future
11+
12+
class ReadinessHandler(isReady: Boolean) extends Service[Request, Response] {
13+
def apply(req: Request): Future[Response] =
14+
if (isReady) newOk("Ready")
15+
else
16+
newResponse(
17+
status = Status.ServiceUnavailable,
18+
contentType = "text/plain;charset=UTF-8",
19+
content = Buf.Utf8("Not ready. Warming up."))
20+
}

0 commit comments

Comments
 (0)