diff options
author | Mateus Cruz <mateuscolvr@gmail.com> | 2024-02-06 00:59:54 -0300 |
---|---|---|
committer | Mateus Cruz <mateuscolvr@gmail.com> | 2024-02-06 01:21:36 -0300 |
commit | aae0f0595061ad32960541c532930be0e5af51f2 (patch) | |
tree | 4368ed78c393650ca7764bc32aba46eb8bf8dd41 | |
parent | 372834401b0b08498286c92a206a48654ec86a71 (diff) |
add lock
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | docker-compose.yml | 1 | ||||
-rw-r--r-- | lib/handler.ml | 4 | ||||
-rw-r--r-- | lib/query.ml | 9 | ||||
-rw-r--r-- | nginx.conf | 2 |
5 files changed, 16 insertions, 1 deletions
@@ -1,2 +1,3 @@ .direnv _build +gatling diff --git a/docker-compose.yml b/docker-compose.yml index eb90487..3304f71 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -34,6 +34,7 @@ services: memory: "110MB" db: + command: postgres -c 'max_connections=30' image: postgres:latest environment: - POSTGRES_PASSWORD=123 diff --git a/lib/handler.ml b/lib/handler.ml index 121a0c5..4babe6e 100644 --- a/lib/handler.ml +++ b/lib/handler.ml @@ -8,6 +8,10 @@ let valid_debit value limit balance = let create_transaction client_id (db_pool : Query.pool) (request : Request.t) = Caqti_eio.Pool.use (fun conn -> + let module C = (val conn : Rapper_helper.CONNECTION) in + C.with_transaction + @@ fun () -> + let _ = Query.lock client_id conn in let client_opt = Option.join @@ Result.to_option @@ Query.find_client client_id conn in diff --git a/lib/query.ml b/lib/query.ml index 1fdc2c3..2dab927 100644 --- a/lib/query.ml +++ b/lib/query.ml @@ -58,6 +58,14 @@ module Q = struct |sql} record_out] ;; + + let lock = + [%rapper + execute + {sql| + SELECT pg_advisory_xact_lock(%int{client_id}) + |sql}] + ;; end let ( let* ) = Result.bind @@ -80,3 +88,4 @@ let execute_transaction ~client_id ~(op : Operation.transaction_op) conn = let find_client id conn = Q.client ~id conn let balance client_id conn = Q.balance ~client_id conn let transactions client_id conn = Q.transactions ~client_id conn +let lock client_id conn = Q.lock ~client_id conn @@ -1,5 +1,5 @@ events { - worker_connections 2000; + worker_connections 256; } http { |