Skip to content

Commit

Permalink
Check application health over HTTP instead of TCP
Browse files Browse the repository at this point in the history
This may also fix a memory leak:
encode/uvicorn#1226

Closes #35.
  • Loading branch information
rouge8 committed Nov 16, 2021
1 parent 334ba3b commit cc1a4fa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
6 changes: 4 additions & 2 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ processes = []
url_prefix = "/static"

[[services]]
http_checks = []
internal_port = 8080
processes = ["app"]
protocol = "tcp"
Expand All @@ -43,7 +42,10 @@ processes = []
handlers = ["tls", "http"]
port = 443

[[services.tcp_checks]]
[[services.http_checks]]
grace_period = "10s"
interval = "10s"
timeout = "2s"
method = "get"
path = "/__lbheartbeat__"
protocol = "http"
18 changes: 18 additions & 0 deletions src/wanikani_apprentice/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import operator
import os.path

import attr
import httpx
import sentry_sdk
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
Expand All @@ -18,6 +19,10 @@
from starlette.routing import Route
from starlette.staticfiles import StaticFiles
from starlette.templating import _TemplateResponse
from starlette.types import ASGIApp
from starlette.types import Receive
from starlette.types import Scope
from starlette.types import Send
import structlog

from . import config
Expand All @@ -36,6 +41,18 @@
log = structlog.get_logger()


@attr.frozen
class LBHeartbeatMiddleware:
app: ASGIApp

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] == "http" and scope["path"] == "/__lbheartbeat__":
response = Response("OK")
await response(scope, receive, send)
else:
await self.app(scope, receive, send)


async def index(request: Request) -> RedirectResponse:
if is_logged_in(request):
return RedirectResponse(request.url_for("assignments"))
Expand Down Expand Up @@ -143,6 +160,7 @@ def create_app() -> Starlette:
)
middleware = [
Middleware(SentryAsgiMiddleware),
Middleware(LBHeartbeatMiddleware),
]

api = WaniKaniAPIClient(str(config.WANIKANI_API_KEY), client=httpx_client)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,17 @@ def test_https_only(mocker):
resp = test_client.get("/", allow_redirects=False)
assert resp.status_code == 307
assert resp.headers["Location"].startswith("https://")


def test_lbheartbeat_bypass_https_only(mocker):
from wanikani_apprentice import config
from wanikani_apprentice.app import create_app

mocker.patch.object(config, "HTTPS_ONLY", True)

app = create_app()
test_client = TestClient(app)

resp = test_client.get("/__lbheartbeat__", allow_redirects=False)
assert resp.status_code == 200
assert resp.text == "OK"

0 comments on commit cc1a4fa

Please sign in to comment.