Skip to content

Commit

Permalink
add leonardo-meireles (zanfranceschi#1641)
Browse files Browse the repository at this point in the history
* add leonardo_meireles

* add readme
  • Loading branch information
LeonardoMeireles55 authored Mar 11, 2024
1 parent e35a209 commit 035f0a6
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
13 changes: 13 additions & 0 deletions participantes/leonardo-meireles/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Minha tentativa de participar da Rinha de Backend 2024.1

### Leonardo Meireles - Quero-1-Emprego Edition

Github: [@LeonardoMeireles55](https://github.com/LeonardoMeireles55)
Repositório: [https://github.com/LeonardoMeireles55/rinha-backend-leonardo](https://github.com/LeonardoMeireles55/rinha-backend-leonardo)

### Stack:
- Java
- Spring
- WebFlux
- Postgres
- Nginx
24 changes: 24 additions & 0 deletions participantes/leonardo-meireles/config/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
worker_processes auto;

events {
worker_connections 1000;
}

http {
access_log off;
error_log off;
sendfile on;

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

server {
listen 9999;

location / {
proxy_pass http://api;
}
}
}
68 changes: 68 additions & 0 deletions participantes/leonardo-meireles/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
version: "3.8"

services:
api01: &api
image: leonardomeireles55/rinha_backend:lastest
build:
context: ./
dockerfile: Dockerfile
working_dir: /usr/src/app
hostname: api01
environment:
- SPRING_R2DBC_URL=r2dbc:postgresql://db:5432/rinha
- SPRING_R2DBC_USERNAME=admin
- SPRING_R2DBC_PASSWORD=123
ports:
- "8081:8080"
depends_on:
- db
deploy:
resources:
limits:
cpus: "0.58"
memory: "278MB"
restart: always
api02:
<<: *api
hostname: api02
ports:
- "8082:8080"
deploy:
resources:
limits:
cpus: "0.57"
memory: "140MB"
nginx:
image: nginx:latest
volumes:
- ./config/nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- api01
- api02
ports:
- "9999:9999"
deploy:
resources:
limits:
cpus: "0.10"
memory: "32MB"
db:
image: postgres:latest
logging:
driver: none
hostname: db
environment:
- POSTGRES_DB=rinha
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=123
# command: "postgres -c synchronous_commit=0 -c full_page_writes=0"
command: postgres -c checkpoint_timeout=600 -c max_wal_size=2096 -c synchronous_commit=0 -c full_page_writes=0 -c fsync=off -c check_function_bodies=false
ports:
- "5432:5432"
volumes:
- ./sql/script.sql:/docker-entrypoint-initdb.d/script.sql
deploy:
resources:
limits:
cpus: "0.25"
memory: "100MB"
73 changes: 73 additions & 0 deletions participantes/leonardo-meireles/sql/script.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
CREATE UNLOGGED TABLE cliente
(
id INT PRIMARY KEY,
limites INT NOT NULL,
saldos INT NOT NULL
);

INSERT INTO cliente (id, limites, saldos)
VALUES (1, 100000, 0),
(2, 80000, 0),
(3, 1000000, 0),
(4, 10000000, 0),
(5, 500000, 0);

CREATE UNLOGGED TABLE TRANSACAO
(
ID SERIAL PRIMARY KEY,
ID_CLIENTE INT NOT NULL,
VALOR INT NOT NULL,
TIPO VARCHAR(1) NOT NULL,
DESCRICAO VARCHAR(10) NOT NULL,
REALIZADA_EM TIMESTAMP NOT NULL
);

CREATE INDEX idx_transacao_id_cliente ON transacao (id_cliente);
CREATE INDEX idx_transacao_id_cliente_realizada_em ON transacao (id_cliente, realizada_em DESC);


CREATE OR REPLACE FUNCTION efetuar_transacao(
clienteIdParam int,
tipoParam varchar(1),
valorParam int,
descricaoParam varchar(10)
)
RETURNS TABLE (saldo int, limite int) AS $$
DECLARE
cliente cliente%rowtype;
novoSaldo
int;
numeroLinhasAfetadas
int;
BEGIN
PERFORM
* FROM cliente where id = clienteIdParam FOR
UPDATE;
IF
tipoParam = 'd' THEN
novoSaldo := valorParam * -1;
ELSE
novoSaldo := valorParam;
END IF;

UPDATE cliente
SET saldos = saldos + novoSaldo
WHERE id = clienteIdParam
AND (novoSaldo > 0 OR limites * -1 <= saldos + novoSaldo) RETURNING *
INTO cliente;

GET DIAGNOSTICS numeroLinhasAfetadas = ROW_COUNT;

IF
numeroLinhasAfetadas = 0 THEN
RAISE EXCEPTION '';
END IF;

INSERT INTO transacao (id_cliente, valor, tipo, descricao, realizada_em)
VALUES (clienteIdParam, valorParam, tipoParam, descricaoParam, current_timestamp);


RETURN QUERY SELECT cliente.saldos AS saldo, cliente.limites AS limite;
END;
$$
LANGUAGE plpgsql;

0 comments on commit 035f0a6

Please sign in to comment.