From 8b094d64b1f5743116388856877302201a5e0339 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Fri, 14 Mar 2025 19:14:56 -0500 Subject: [PATCH] add `?flakey=pct` param to make the app return 500 errors intermittently --- app.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app.rb b/app.rb index 6b8f205..de43f76 100644 --- a/app.rb +++ b/app.rb @@ -23,6 +23,7 @@ require "config" CHUNK_SIZE = 1024**2 SESSION_SECRET_HEX_LENGTH = 64 JWT_SECRET_HEX_LENGTH = 64 +DEFAULT_FLAKEY = 50 ENV_PREFIX = "KUBERNAUT" @@ -207,6 +208,13 @@ Health.instance.up Ready.instance.up Sleep.instance.wake +def flaky(pct = DEFAULT_FLAKEY) + r = Random.rand(0..100) + unless r < (100 - pct) + halt 500, "so unreliable" + end +end + enable :sessions configure do @@ -218,6 +226,17 @@ before do sleep(1) while Sleep.instance.asleep? && request.path_info != "/livez/sleep" content_type :text if request.path_info.start_with? "/_cat" request.session_options[:skip] = !request.path_info.start_with?("/session") + + if params.has_key? :flaky + begin + pct = Integer(params[:flaky]) + pct = pct.clamp(0, 100) + rescue => e + logger.warn "#{e.message}: falling back to default flaky percentage of #{DEFAULT_FLAKEY}" + pct = DEFAULT_FLAKEY + end + flaky(pct) + end end helpers do