Commit f793dad7 authored by Andrew Newdigate's avatar Andrew Newdigate

Updated with Sean's feedback

parent 04efa0b5
...@@ -4,24 +4,33 @@ class ChaosController < ActionController::Base ...@@ -4,24 +4,33 @@ class ChaosController < ActionController::Base
before_action :validate_request before_action :validate_request
def leakmem 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 = [] 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' render text: "OK", content_type: 'text/plain'
end end
def cpuspin 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 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' render text: "OK", content_type: 'text/plain'
end end
def sleep 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 Kernel.sleep duration_s
render text: "OK", content_type: 'text/plain' render text: "OK", content_type: 'text/plain'
end end
...@@ -33,6 +42,11 @@ class ChaosController < ActionController::Base ...@@ -33,6 +42,11 @@ class ChaosController < ActionController::Base
def validate_request def validate_request
secret = ENV['GITLAB_CHAOS_SECRET'] 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 return unless secret
unless request.headers["HTTP_X_CHAOS_SECRET"] == secret unless request.headers["HTTP_X_CHAOS_SECRET"] == secret
......
...@@ -41,12 +41,13 @@ To simulate a memory leak in your application, use the `/-/chaos/leakmem` endpoi ...@@ -41,12 +41,13 @@ To simulate a memory leak in your application, use the `/-/chaos/leakmem` endpoi
For example, if your GitLab instance is listening at `localhost:3000`, you could `curl` the endpoint as follows: For example, if your GitLab instance is listening at `localhost:3000`, you could `curl` the endpoint as follows:
```shell ```shell
curl http://localhost:3000/-/chaos/leakmem?memory_mb=1024 --header 'X-Chaos-Secret: secret' curl http://localhost:3000/-/chaos/leakmem?memory_mb=1024&duration_s=10 --header 'X-Chaos-Secret: secret'
``` ```
The `memory_mb` parameter tells the application how much memory it should leak. The `memory_mb` parameter tells the application how much memory it should leak. The `duration_s` parameter will ensure the request retains
the memory for this duration at a minimum (default 30s).
Note: the memory is not retained after the request, so once its completed, the Ruby garbage collector will attempt to recover the memory. Note: the memory is not retained after the request finishes. Once the request has completed, the Ruby garbage collector will attempt to recover the memory.
### CPU Spin ### CPU Spin
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment