From 50ac277df7cb779b6c4462deaf511dddd39ae73a Mon Sep 17 00:00:00 2001 From: Samuel Jansem Date: Thu, 8 Feb 2024 17:09:55 -0300 Subject: [PATCH] =?UTF-8?q?submiss=C3=A3o:=20samueljansem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- participantes/samueljansem/README.md | 13 ++++ participantes/samueljansem/config/init.sql | 58 ++++++++++++++++++ participantes/samueljansem/config/nginx.conf | 22 +++++++ .../samueljansem/config/postgres.conf | 20 +++++++ participantes/samueljansem/docker-compose.yml | 60 +++++++++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 participantes/samueljansem/README.md create mode 100644 participantes/samueljansem/config/init.sql create mode 100644 participantes/samueljansem/config/nginx.conf create mode 100644 participantes/samueljansem/config/postgres.conf create mode 100644 participantes/samueljansem/docker-compose.yml diff --git a/participantes/samueljansem/README.md b/participantes/samueljansem/README.md new file mode 100644 index 000000000..71ed96709 --- /dev/null +++ b/participantes/samueljansem/README.md @@ -0,0 +1,13 @@ +# Submissão para Rinha de Backend, Segunda Edição: 2024/Q1 - Controle de Concorrência + +Logo .NET +Logo PostgreSQL +logo nginx + +## 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) diff --git a/participantes/samueljansem/config/init.sql b/participantes/samueljansem/config/init.sql new file mode 100644 index 000000000..04f160299 --- /dev/null +++ b/participantes/samueljansem/config/init.sql @@ -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; +$$; \ No newline at end of file diff --git a/participantes/samueljansem/config/nginx.conf b/participantes/samueljansem/config/nginx.conf new file mode 100644 index 000000000..917b6c9be --- /dev/null +++ b/participantes/samueljansem/config/nginx.conf @@ -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; + } + } +} diff --git a/participantes/samueljansem/config/postgres.conf b/participantes/samueljansem/config/postgres.conf new file mode 100644 index 000000000..f32b11e1b --- /dev/null +++ b/participantes/samueljansem/config/postgres.conf @@ -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 \ No newline at end of file diff --git a/participantes/samueljansem/docker-compose.yml b/participantes/samueljansem/docker-compose.yml new file mode 100644 index 000000000..fd50e1fe9 --- /dev/null +++ b/participantes/samueljansem/docker-compose.yml @@ -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