summaryrefslogtreecommitdiff
path: root/app/controllers/chaos_controller.rb
diff options
context:
space:
mode:
authorKamil Trzciński <ayufan@ayufan.eu>2019-07-10 11:43:51 +0200
committerKamil Trzciński <ayufan@ayufan.eu>2019-07-11 10:38:12 +0200
commit56eb9f6c0ee719d06a0cccce8f815922d35b3b05 (patch)
treed0d1533a6751a0e0e9f9bce00a2c277ba52a7a14 /app/controllers/chaos_controller.rb
parent92fac459533d5afeea3b302ca7634767e0ff110b (diff)
downloadgitlab-ce-56eb9f6c0ee719d06a0cccce8f815922d35b3b05.tar.gz
Add `db_spin` and refactor ChaosControllerimprove-chaos-controller
Diffstat (limited to 'app/controllers/chaos_controller.rb')
-rw-r--r--app/controllers/chaos_controller.rb87
1 files changed, 64 insertions, 23 deletions
diff --git a/app/controllers/chaos_controller.rb b/app/controllers/chaos_controller.rb
index 8d518c14b90..2985da35d83 100644
--- a/app/controllers/chaos_controller.rb
+++ b/app/controllers/chaos_controller.rb
@@ -1,35 +1,36 @@
# frozen_string_literal: true
class ChaosController < ActionController::Base
- before_action :validate_request
+ before_action :validate_chaos_secret, unless: :development?
+ before_action :request_start_time
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
+ Kernel.sleep(duration_left)
render plain: "OK"
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
+ def cpu_spin
+ rand while Time.now < expected_end_time
render plain: "OK"
end
+ def db_spin
+ while Time.now < expected_end_time
+ ActiveRecord::Base.connection.execute("SELECT 1")
+
+ end_interval_time = Time.now + [duration_s, interval_s].min
+ rand while Time.now < end_interval_time
+ end
+ end
+
def sleep
- duration_s = (params[:duration_s]&.to_i || 30).seconds
- Kernel.sleep duration_s
+ Kernel.sleep(duration_left)
render plain: "OK"
end
@@ -40,17 +41,57 @@ class ChaosController < ActionController::Base
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 plain: "chaos misconfigured: please configure GITLAB_CHAOS_SECRET when using GITLAB_ENABLE_CHAOS_ENDPOINTS outside of a development environment", status: :internal_server_error
- end
+ def request_start_time
+ @start_time ||= Time.now
+ end
+
+ def expected_end_time
+ request_start_time + duration_s
+ end
- return unless secret
+ def duration_left
+ # returns 0 if over time
+ [expected_end_time - Time.now, 0].max
+ end
+
+ def validate_chaos_secret
+ unless chaos_secret_configured
+ render plain: "chaos misconfigured: please configure GITLAB_CHAOS_SECRET",
+ status: :internal_server_error
+ return
+ end
- unless request.headers["HTTP_X_CHAOS_SECRET"] == secret
- render plain: "To experience chaos, please set X-Chaos-Secret header", status: :unauthorized
+ unless Devise.secure_compare(chaos_secret_configured, chaos_secret_request)
+ render plain: "To experience chaos, please set a valid `X-Chaos-Secret` header or `token` param",
+ status: :unauthorized
+ return
end
end
+
+ def chaos_secret_configured
+ ENV['GITLAB_CHAOS_SECRET']
+ end
+
+ def chaos_secret_request
+ request.headers["HTTP_X_CHAOS_SECRET"] || params[:token]
+ end
+
+ def interval_s
+ interval_s = params[:interval_s] || 1
+ interval_s.to_f.seconds
+ end
+
+ def duration_s
+ duration_s = params[:duration_s] || 30
+ duration_s.to_i.seconds
+ end
+
+ def memory_mb
+ memory_mb = params[:memory_mb] || 100
+ memory_mb.to_i
+ end
+
+ def development?
+ Rails.env.development?
+ end
end