forked from zanfranceschi/rinha-de-backend-2024-q1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
uploading NotSoFastAPI (zanfranceschi#1618)
* upload NotSoFastAPI * correcao
- Loading branch information
1 parent
1847fcf
commit 62c2ee2
Showing
4 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# NotSoFastAPI | ||
|
||
- FastAPI | ||
- Postgres | ||
- Nginx | ||
|
||
[link do repo](https://github.com/srlucasromulo/not-so-fast-api) |
57 changes: 57 additions & 0 deletions
57
participantes/srlucasromulo-not_so_fast_api/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
version: '3.9' | ||
|
||
services: | ||
api01: &api | ||
image: srlucasromulo/not_so_fast_api | ||
hostname: api01 | ||
environment: | ||
- DB_HOST=db | ||
volumes: | ||
- ./api:/api01 | ||
ports: | ||
- "8001:8000" | ||
depends_on: | ||
- db | ||
deploy: | ||
resources: | ||
limits: | ||
cpus: "0.4" | ||
memory: "150MB" | ||
|
||
api02: | ||
<<: *api | ||
hostname: api02 | ||
ports: | ||
- "8002:8000" | ||
|
||
nginx: | ||
image: nginx:latest | ||
volumes: | ||
- ./nginx.conf:/etc/nginx/nginx.conf:ro | ||
depends_on: | ||
- api01 | ||
- api02 | ||
ports: | ||
- "9999:9999" | ||
deploy: | ||
resources: | ||
limits: | ||
cpus: "0.3" | ||
memory: "150MB" | ||
|
||
db: | ||
image: postgres:16 | ||
hostname: db | ||
environment: | ||
- POSTGRES_DB=rinha | ||
- POSTGRES_USER=admin | ||
- POSTGRES_PASSWORD=rinha | ||
volumes: | ||
- ./init.sql:/docker-entrypoint-initdb.d/init.sql | ||
ports: | ||
- "5432:5432" | ||
deploy: | ||
resources: | ||
limits: | ||
cpus: "0.4" | ||
memory: "100MB" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
\c rinha; | ||
|
||
|
||
CREATE TABLE cliente ( | ||
id SERIAL PRIMARY KEY, | ||
limite INTEGER NOT NULL, | ||
saldo INTEGER NOT NULL DEFAULT 0 | ||
); | ||
|
||
|
||
CREATE TABLE transacao ( | ||
id SERIAL PRIMARY KEY, | ||
cliente_id INTEGER REFERENCES cliente(id), | ||
valor INTEGER NOT NULL, | ||
tipo CHAR(1) NOT NULL, | ||
descricao VARCHAR(10) NOT NULL, | ||
realizada_em TIMESTAMP DEFAULT NOW() | ||
); | ||
|
||
|
||
CREATE OR REPLACE PROCEDURE init_db() AS $$ | ||
BEGIN | ||
INSERT INTO cliente(limite) | ||
VALUES | ||
(100000), | ||
(80000), | ||
(1000000), | ||
(10000000), | ||
(500000) | ||
; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CALL init_db(); | ||
|
||
|
||
CREATE OR REPLACE PROCEDURE reset_db() AS $$ | ||
BEGIN | ||
TRUNCATE TABLE cliente RESTART IDENTITY CASCADE; | ||
CALL init_db(); | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
|
||
CREATE OR REPLACE FUNCTION creditar(_cliente_id INT, _valor INT, _descricao VARCHAR(10)) RETURNS JSONB AS $$ | ||
DECLARE | ||
record RECORD; | ||
BEGIN | ||
PERFORM saldo FROM cliente WHERE id = _cliente_id LIMIT 1 FOR NO KEY UPDATE; | ||
|
||
IF NOT FOUND THEN | ||
RAISE SQLSTATE '22000'; -- cliente not found | ||
END IF; | ||
|
||
UPDATE cliente SET saldo = saldo + _valor WHERE id = _cliente_id | ||
RETURNING limite, saldo INTO record; | ||
INSERT INTO transacao (cliente_id, valor, tipo, descricao) VALUES (_cliente_id, _valor, 'c', _descricao); | ||
|
||
RETURN jsonb_build_object('limite', record.limite, 'saldo', record.saldo); | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
|
||
CREATE OR REPLACE FUNCTION debitar(_cliente_id INT, _valor INT, _descricao VARCHAR(10)) RETURNS JSONB AS $$ | ||
DECLARE | ||
record RECORD; | ||
saldo_atual int; | ||
limite_cliente int; | ||
BEGIN | ||
SELECT limite, saldo INTO limite_cliente, saldo_atual FROM cliente WHERE id = _cliente_id LIMIT 1 FOR NO KEY UPDATE; | ||
|
||
IF NOT FOUND THEN | ||
RAISE SQLSTATE '22000'; -- cliente not found | ||
END IF; | ||
|
||
IF saldo_atual - _valor >= limite_cliente * -1 THEN | ||
UPDATE cliente SET saldo = saldo - _valor WHERE id = _cliente_id | ||
RETURNING limite, saldo INTO record; | ||
INSERT INTO transacao (cliente_id, valor, tipo, descricao) VALUES (_cliente_id, _valor, 'd', _descricao); | ||
ELSE | ||
RAISE SQLSTATE '23000'; -- limite excedido | ||
END IF; | ||
|
||
RETURN jsonb_build_object('limite', record.limite, 'saldo', record.saldo); | ||
END; | ||
$$ LANGUAGE plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
worker_processes auto; | ||
|
||
events { | ||
worker_connections 256; | ||
} | ||
|
||
http { | ||
access_log off; | ||
|
||
upstream api { | ||
server api01:8000; | ||
server api02:8000; | ||
} | ||
|
||
server { | ||
listen 9999; | ||
|
||
location / { | ||
proxy_pass http://api; | ||
} | ||
} | ||
} |