Skip to content

Commit

Permalink
chore: add rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
jhandguy committed Oct 1, 2023
1 parent b07aee4 commit 6edae61
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 23 deletions.
17 changes: 12 additions & 5 deletions dynamo/k6/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import {check, sleep} from 'k6';
import {randomString, uuidv4} from 'https://jslib.k6.io/k6-utils/1.4.0/index.js';

export const options = {
stages: [
{target: 10, duration: '20s'},
{target: 10, duration: '20s'},
{target: 0, duration: '20s'},
],
scenarios: {
load: {
executor: 'ramping-arrival-rate',
startRate: 1,
timeUnit: '1s',
preAllocatedVUs: 10,
stages: [
{target: 10, duration: '40s'},
{target: 0, duration: '20s'},
],
},
},
thresholds: {
'checks': ['rate>0.9'],
'grpc_req_duration{method:CreateItem}': ['p(95)<10000'],
Expand Down
17 changes: 12 additions & 5 deletions gateway/k6/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import {check, sleep} from 'k6';
import {randomString, uuidv4} from 'https://jslib.k6.io/k6-utils/1.4.0/index.js';

export const options = {
stages: [
{target: 40, duration: '1m'},
{target: 40, duration: '1m'},
{target: 0, duration: '1m'},
],
scenarios: {
load: {
executor: 'ramping-arrival-rate',
startRate: 1,
timeUnit: '1s',
preAllocatedVUs: 20,
stages: [
{target: 20, duration: '40s'},
{target: 0, duration: '20s'},
],
},
},
thresholds: {
'checks': ['rate>0.9'],
'http_req_duration{method:POST}': ['p(95)<10000'],
Expand Down
17 changes: 12 additions & 5 deletions s3/k6/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import {check, sleep} from 'k6';
import {randomString, uuidv4} from 'https://jslib.k6.io/k6-utils/1.4.0/index.js';

export const options = {
stages: [
{target: 10, duration: '20s'},
{target: 10, duration: '20s'},
{target: 0, duration: '20s'},
],
scenarios: {
load: {
executor: 'ramping-arrival-rate',
startRate: 1,
timeUnit: '1s',
preAllocatedVUs: 10,
stages: [
{target: 10, duration: '40s'},
{target: 0, duration: '20s'},
],
},
},
thresholds: {
'checks': ['rate>0.9'],
'grpc_req_duration{method:CreateObject}': ['p(95)<10000'],
Expand Down
3 changes: 3 additions & 0 deletions sql/helm/templates/ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ metadata:
app: {{ $.Release.Name }}
annotations:
cert-manager.io/issuer: {{ $.Release.Name }}
{{- with .rateLimitRequests }}
haproxy.org/rate-limit-requests: "{{ . }}"
{{- end }}
spec:
rules:
- host: {{ .host }}
Expand Down
27 changes: 19 additions & 8 deletions sql/k6/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import {check, sleep} from 'k6';
import {randomItem, randomString, uuidv4} from 'https://jslib.k6.io/k6-utils/1.4.0/index.js';

export const options = {
setupTimeout: '2m',
stages: [
{target: 20, duration: '40s'},
{target: 20, duration: '40s'},
{target: 0, duration: '40s'},
],
setupTimeout: '3m',
scenarios: {
load: {
executor: 'ramping-arrival-rate',
startRate: 1,
timeUnit: '1s',
preAllocatedVUs: 10,
stages: [
{target: 10, duration: '40s'},
{target: 0, duration: '20s'},
],
},
},
thresholds: {
'checks': ['rate>0.9'],
'http_req_duration{method:POST}': ['p(95)<10000'],
Expand Down Expand Up @@ -37,6 +44,7 @@ export function setup() {
};

check(http.post(`${url}/user`, JSON.stringify(user), params), {
'post response status is not 403': (r) => r.status !== 403,
'post response status is 201': (r) => r.status === 201,
'post response body is valid': (r) => r.json().id === user.id && r.json().name === user.name,
});
Expand All @@ -49,11 +57,14 @@ export function setup() {
};

check(http.post(`${url}/message`, JSON.stringify(message), params), {
'post response status is not 403': (r) => r.status !== 403,
'post response status is 201': (r) => r.status === 201,
'post response body is valid': (r) => r.json().id === message.id && r.json().content === message.content && r.json().user_id === message.user_id,
});

messages.push(message)

sleep(0.03);
}

users.push({
Expand All @@ -71,16 +82,16 @@ export function setup() {
export default function (data) {
const user = randomItem(data.users);
check(http.get(`${url}/user/${user.id}/messages`, params), {
'get response status is not 403': (r) => r.status !== 403,
'get response status is 200': (r) => r.status === 200,
'get response body is valid': (r) => r.json().length === user.messages.length,
});

sleep(1);
}

export function teardown(data) {
for (let user of data.users) {
check(http.del(`${url}/user/${user.id}`, null, params), {
'delete response status is not 403': (r) => r.status !== 403,
'delete response status is 200': (r) => r.status === 200,
});
}
Expand Down
2 changes: 2 additions & 0 deletions terraform/environments/haproxy/modules.tf
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ module "sql_postgres" {
"sql_token" = random_password.sql_postgres_token.result
"tempo_url" = module.tempo.otlp_grpc_url
}
rate_limit_requests = 40
}

module "sql_mysql" {
Expand All @@ -89,6 +90,7 @@ module "sql_mysql" {
"sql_token" = random_password.sql_mysql_token.result
"tempo_url" = module.tempo.otlp_grpc_url
}
rate_limit_requests = 40
}

module "mimir" {
Expand Down
1 change: 1 addition & 0 deletions terraform/modules/sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ No modules.
| <a name="input_node_ip"></a> [node\_ip](#input\_node\_ip) | Node ip | `string` | n/a | yes |
| <a name="input_node_ports"></a> [node\_ports](#input\_node\_ports) | Node ports | `tuple([number, number])` | n/a | yes |
| <a name="input_prometheus_enabled"></a> [prometheus\_enabled](#input\_prometheus\_enabled) | Enable Prometheus | `bool` | `true` | no |
| <a name="input_rate_limit_requests"></a> [rate\_limit\_requests](#input\_rate\_limit\_requests) | Rate limit requests | `number` | `0` | no |
| <a name="input_replicas"></a> [replicas](#input\_replicas) | Replicas | `number` | `1` | no |
| <a name="input_secrets"></a> [secrets](#input\_secrets) | Secrets | `map(string)` | `{}` | no |

Expand Down
3 changes: 3 additions & 0 deletions terraform/modules/sql/helm_releases.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ resource "helm_release" "sql" {
metrics: ${var.node_ports.1}
ingress:
host: ${var.ingress_host}
%{if var.rate_limit_requests > 0}
rateLimitRequests: ${var.rate_limit_requests}
%{endif}
prometheus:
enabled: ${var.prometheus_enabled}
groupName: ${var.feature == "mysql" ? "MySQL" : title(var.feature)}
Expand Down
6 changes: 6 additions & 0 deletions terraform/modules/sql/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ variable "prometheus_enabled" {
default = true
description = "Enable Prometheus"
}

variable "rate_limit_requests" {
type = number
default = 0
description = "Rate limit requests"
}

0 comments on commit 6edae61

Please sign in to comment.