diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2018-11-07 16:21:17 +0000 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2018-11-07 16:21:17 +0000 |
commit | cd6450923f72538a7a28b7c16cf9a2e38a5115ec (patch) | |
tree | 021dde9e03060dcc504ff0b70a16ac99106d12d7 /app | |
parent | 8a55e477adac8c41aaa281dc119f9007a0c20451 (diff) | |
parent | 673f06253d1e799a8024b18270c1a7279fabe9b8 (diff) | |
download | gitlab-ce-cd6450923f72538a7a28b7c16cf9a2e38a5115ec.tar.gz |
Merge branch '52767-more-chaos-for-gitlab' into 'master'
Add more chaos to GitLab
Closes #53362 and #52767
See merge request gitlab-org/gitlab-ce!22746
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/chaos_controller.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/app/controllers/chaos_controller.rb b/app/controllers/chaos_controller.rb new file mode 100644 index 00000000000..b4f46cddbe9 --- /dev/null +++ b/app/controllers/chaos_controller.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +class ChaosController < ActionController::Base + before_action :validate_request + + def leakmem + 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 + + render text: "OK", content_type: 'text/plain' + end + + def cpuspin + duration_s = (params[:duration_s]&.to_i || 30).seconds + end_time = Time.now + duration_s.seconds + + rand while Time.now < end_time + + render text: "OK", content_type: 'text/plain' + end + + def sleep + duration_s = (params[:duration_s]&.to_i || 30).seconds + Kernel.sleep duration_s + + render text: "OK", content_type: 'text/plain' + end + + def kill + Process.kill("KILL", Process.pid) + end + + private + + 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 + render text: "To experience chaos, please set X-Chaos-Secret header", content_type: 'text/plain', status: 401 + end + end +end |