Skip to content

Commit

Permalink
submissão: samueljansem
Browse files Browse the repository at this point in the history
  • Loading branch information
samueljansem committed Feb 8, 2024
1 parent e1729f3 commit 50ac277
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 0 deletions.
13 changes: 13 additions & 0 deletions participantes/samueljansem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Submissão para Rinha de Backend, Segunda Edição: 2024/Q1 - Controle de Concorrência

<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/7d/Microsoft_.NET_logo.svg/300px-Microsoft_.NET_logo.svg.png" alt="Logo .NET" width="200" height="auto">
<img src="https://upload.wikimedia.org/wikipedia/commons/2/29/Postgresql_elephant.svg" alt="Logo PostgreSQL" width="200" height="auto">
<img src="https://upload.wikimedia.org/wikipedia/commons/c/c5/Nginx_logo.svg" alt="logo nginx" width="400" height="auto">

## Samuel Jansem / [@samueljazen](https://twitter.com/samueljazen)

Submissão feita com:

- `.NET (Native AOT)`
- `PostgreSQL`
- [Repositório API](https://github.com/samueljansem/rinha-backend-2024-q1-dotnet)
58 changes: 58 additions & 0 deletions participantes/samueljansem/config/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
CREATE TABLE
"clientes" (
"id" INT PRIMARY KEY,
"saldo" INTEGER NOT NULL,
"limite" INTEGER NOT NULL
);

CREATE UNLOGGED TABLE
"transacoes" (
"id" SERIAL PRIMARY KEY,
"valor" INTEGER NOT NULL,
"id_cliente" INTEGER NOT NULL,
"tipo" VARCHAR(1) NOT NULL,
"descricao" VARCHAR(10) NOT NULL,
"realizada_em" TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT "fk_transacoes_id_cliente" FOREIGN KEY ("id_cliente") REFERENCES "clientes" ("id")
);

ALTER TABLE "transacoes" SET (autovacuum_enabled = false);

INSERT INTO
clientes (id, saldo, limite)
VALUES
(1, 0, 100000),
(2, 0, 80000),
(3, 0, 1000000),
(4, 0, 10000000),
(5, 0, 500000);

CREATE OR REPLACE PROCEDURE criar_transacao_e_atualizar_saldo(
id_cliente INTEGER,
valor INTEGER,
tipo VARCHAR(1),
descricao VARCHAR(10),
realizada_em TIMESTAMP WITH TIME ZONE,
INOUT saldo_atual INTEGER DEFAULT NULL,
INOUT limite_atual INTEGER DEFAULT NULL
)
LANGUAGE plpgsql
AS $$
DECLARE
valor_absoluto INTEGER;
BEGIN
valor_absoluto := valor;

IF tipo = 'd' THEN
valor := -valor;
END IF;

UPDATE clientes
SET saldo = saldo + valor
WHERE id = id_cliente AND (saldo + valor) >= -limite
RETURNING saldo, limite INTO saldo_atual, limite_atual;

INSERT INTO transacoes (valor, id_cliente, tipo, descricao, realizada_em)
VALUES (valor_absoluto, id_cliente, tipo, descricao, realizada_em);
END;
$$;
22 changes: 22 additions & 0 deletions participantes/samueljansem/config/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
events {
worker_connections 1000;
}

http {
access_log off;
gzip on;
http2 on;

upstream api {
server api01:8080;
server api02:8080;
}

server {
listen 9999;

location / {
proxy_pass http://api;
}
}
}
20 changes: 20 additions & 0 deletions participantes/samueljansem/config/postgres.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
listen_addresses = '*'
max_connections = 500
superuser_reserved_connections = 3
unix_socket_directories = '/var/run/postgresql'
shared_buffers = 512MB
work_mem = 4MB
maintenance_work_mem = 256MB
effective_cache_size = 1GB
wal_buffers = 64MB
checkpoint_completion_target = 0.9
random_page_cost = 4.0
effective_io_concurrency = 10
autovacuum = on
log_statement = 'none'
log_duration = off
log_lock_waits = off
log_error_verbosity = terse
log_min_messages = panic
log_min_error_statement = panic
synchronous_commit=off
60 changes: 60 additions & 0 deletions participantes/samueljansem/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
version: '3.8'

services:
api01: &api
image: samueljansem/rinha-backend-2024-q1-dotnet:latest
hostname: api01
ports:
- '8081:8080'
depends_on:
- db
deploy:
resources:
limits:
cpus: '0.25'
memory: '20MB'

api02:
<<: *api
hostname: api02
ports:
- '8082:8080'

db:
image: postgres:latest
hostname: db
environment:
- POSTGRES_DB=rinha
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=123
ports:
- '5432:5432'
volumes:
- ./config/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
- ./config/postgres.conf:/docker-entrypoint-initdb.d/postgres.conf
command: postgres -c config_file=/docker-entrypoint-initdb.d/postgres.conf
deploy:
resources:
limits:
cpus: '0.8'
memory: '490MB'

nginx:
image: nginx:latest
ports:
- '9999:9999'
volumes:
- ./config/nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- api01
- api02
deploy:
resources:
limits:
cpus: '0.2'
memory: '20MB'

networks:
default:
driver: bridge
name: rinha-nginx-2024-q1-dotnet

0 comments on commit 50ac277

Please sign in to comment.