diff options
author | Andrew Newdigate <andrew@gitlab.com> | 2018-11-05 18:34:38 +0300 |
---|---|---|
committer | Andrew Newdigate <andrew@gitlab.com> | 2018-11-05 18:34:38 +0300 |
commit | f793dad78be67a155d7ce942e7947db39caebaaf (patch) | |
tree | da5e3162cf59452a1dab93f0ffb26beb6244b1ae /app/controllers/chaos_controller.rb | |
parent | 04efa0b512953d90e125850146759d09fac2d9bc (diff) |
Updated with Sean's feedback
Diffstat (limited to 'app/controllers/chaos_controller.rb')
-rw-r--r-- | app/controllers/chaos_controller.rb | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/app/controllers/chaos_controller.rb b/app/controllers/chaos_controller.rb index 392814b4275..b4f46cddbe9 100644 --- a/app/controllers/chaos_controller.rb +++ b/app/controllers/chaos_controller.rb @@ -4,24 +4,33 @@ class ChaosController < ActionController::Base before_action :validate_request def leakmem - memory_mb = params[:memory_mb] ? params[:memory_mb].to_i : 100 + memory_mb = (params[:memory_mb]&.to_i || 100) + duration_s = (params[:duration_s]&.to_i || 30).seconds + + start = Time.now retainer = [] + # Add `n` 1mb chunks of memory to the retainer array + memory_mb.times { retainer << "x" * 1.megabyte } + + duration_taken = (Time.now - start).seconds + Kernel.sleep duration_s - duration_taken if duration_s > duration_taken - memory_mb.times { retainer << "x" * (1024 * 1024) } render text: "OK", content_type: 'text/plain' end def cpuspin - duration_s = params[:duration_s] ? params[:duration_s].to_i : 30 + duration_s = (params[:duration_s]&.to_i || 30).seconds end_time = Time.now + duration_s.seconds - 10_000.times { } while Time.now < end_time + + rand while Time.now < end_time render text: "OK", content_type: 'text/plain' end def sleep - duration_s = params[:duration_s] ? params[:duration_s].to_i : 30 + duration_s = (params[:duration_s]&.to_i || 30).seconds Kernel.sleep duration_s + render text: "OK", content_type: 'text/plain' end @@ -33,6 +42,11 @@ class ChaosController < ActionController::Base def validate_request secret = ENV['GITLAB_CHAOS_SECRET'] + # GITLAB_CHAOS_SECRET is required unless you're running in Development mode + if !secret && !Rails.env.development? + render text: "chaos misconfigured: please configure GITLAB_CHAOS_SECRET when using GITLAB_ENABLE_CHAOS_ENDPOINTS outside of a development environment", content_type: 'text/plain', status: 500 + end + return unless secret unless request.headers["HTTP_X_CHAOS_SECRET"] == secret |