feat: add lightweight /health endpoint (#13386)
The existing /api health check endpoint creates a new Redis connection
on every request and checks both Redis and Postgres availability. During
peak traffic, this creates unnecessary load and can cause cascading
failures when either service is slow - instances get marked unhealthy,
traffic shifts to remaining instances, which then also fail health
checks.
The new /health endpoint:
- Returns immediately with 200 {"status":"woot"}
- Skips all middleware and authentication
- No Redis or Postgres dependency
- Suitable for health checks that only need to verify the web server is
responding
This commit is contained in:
7
app/controllers/health_controller.rb
Normal file
7
app/controllers/health_controller.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
# Inherits from ActionController::Base to skip all middleware,
|
||||
# authentication, and callbacks. Used for health checks
|
||||
class HealthController < ActionController::Base # rubocop:disable Rails/ApplicationController
|
||||
def show
|
||||
render json: { status: 'woot' }
|
||||
end
|
||||
end
|
||||
@@ -47,6 +47,12 @@ class Rack::Attack
|
||||
|
||||
Rack::Attack.safelist('trusted IPs', &:allowed_ip?)
|
||||
|
||||
# Safelist health check endpoint so it never touches Redis for throttle tracking.
|
||||
# This keeps /health fully dependency-free for ALB liveness checks.
|
||||
Rack::Attack.safelist('health check') do |req|
|
||||
req.path == '/health'
|
||||
end
|
||||
|
||||
### Throttle Spammy Clients ###
|
||||
|
||||
# If any single client IP is making tons of requests, then they're
|
||||
|
||||
@@ -35,6 +35,7 @@ Rails.application.routes.draw do
|
||||
resource :slack_uploads, only: [:show]
|
||||
end
|
||||
|
||||
get '/health', to: 'health#show'
|
||||
get '/api', to: 'api#index'
|
||||
namespace :api, defaults: { format: 'json' } do
|
||||
namespace :v1 do
|
||||
|
||||
11
spec/controllers/health_controller_spec.rb
Normal file
11
spec/controllers/health_controller_spec.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Health Check', type: :request do
|
||||
describe 'GET /health' do
|
||||
it 'returns success status' do
|
||||
get '/health'
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(response.parsed_body['status']).to eq('woot')
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user