From c4303287620ba39e807211fc429486c3c072ca79 Mon Sep 17 00:00:00 2001 From: Brandon Kase Date: Sat, 29 Aug 2020 00:56:47 +0300 Subject: [PATCH 1/2] Uses connection pooling for db queries Connections are stateful so we need a pool. This fixes the latest rosetta-cli error and allows the cli to acurately read all the blocks now. Additionally, test-agent still passes. --- src/app/rosetta/lib/rosetta.ml | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/app/rosetta/lib/rosetta.ml b/src/app/rosetta/lib/rosetta.ml index 7c6bd056326..0cb91efc512 100644 --- a/src/app/rosetta/lib/rosetta.ml +++ b/src/app/rosetta/lib/rosetta.ml @@ -17,16 +17,22 @@ let router ~graphql_uri ~db ~logger route body = | _ -> Deferred.return (Error `Page_not_found) -let server_handler ~db ~graphql_uri ~logger ~body _sock req = +let server_handler ~pool ~graphql_uri ~logger ~body _sock req = let uri = Cohttp_async.Request.uri req in let%bind body = Cohttp_async.Body.to_string body in let route = List.tl_exn (String.split ~on:'/' (Uri.path uri)) in let%bind result = + let with_db f = + Caqti_async.Pool.use (fun db -> f ~db) pool + |> Deferred.Result.map_error ~f:(fun _ -> + (* TODO: format this properly *) + `App (Errors.create `Invariant_violation) ) + in match Yojson.Safe.from_string body with | body -> - router route body ~db ~graphql_uri ~logger + with_db (router route body ~graphql_uri ~logger) | exception Yojson.Json_error "Blank input data" -> - router route `Null ~db ~graphql_uri ~logger + with_db (router route `Null ~graphql_uri ~logger) | exception Yojson.Json_error err -> Errors.create ~context:"JSON in request malformed" (`Json_parse (Some err)) @@ -70,13 +76,13 @@ let command = fun () -> let logger = Logger.create () in Cli.logger_setup log_json log_level ; - match%bind Caqti_async.connect archive_uri with + match Caqti_async.connect_pool ~max_size:128 archive_uri with | Error e -> [%log error] ~metadata:[("error", `String (Caqti_error.show e))] - "Failed to connect to postgresql database. Error: $error" ; + "Failed to create a caqti pool to postgres. Error: $error" ; Deferred.unit - | Ok db -> + | Ok pool -> let%bind server = Cohttp_async.Server.create_expert ~max_connections:128 ~on_handler_error: @@ -88,7 +94,7 @@ let command = [ ("error", `String (Exn.to_string_mach exn)) ; ("context", `String "rest_server") ] )) (Async.Tcp.Where_to_listen.bind_to All_addresses (On_port port)) - (server_handler ~db ~graphql_uri ~logger) + (server_handler ~pool ~graphql_uri ~logger) in [%log info] ~metadata:[("port", `Int port)] From 1030bcb545b154817b39e8002c3c800632fed3a2 Mon Sep 17 00:00:00 2001 From: Brandon Kase Date: Sun, 30 Aug 2020 16:04:15 +0300 Subject: [PATCH 2/2] Removes shadowing of existing errors --- src/app/rosetta/lib/rosetta.ml | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/app/rosetta/lib/rosetta.ml b/src/app/rosetta/lib/rosetta.ml index 0cb91efc512..57e02059913 100644 --- a/src/app/rosetta/lib/rosetta.ml +++ b/src/app/rosetta/lib/rosetta.ml @@ -24,9 +24,17 @@ let server_handler ~pool ~graphql_uri ~logger ~body _sock req = let%bind result = let with_db f = Caqti_async.Pool.use (fun db -> f ~db) pool - |> Deferred.Result.map_error ~f:(fun _ -> - (* TODO: format this properly *) - `App (Errors.create `Invariant_violation) ) + |> Deferred.Result.map_error ~f:(function + | `App e -> + `App e + | `Page_not_found -> + `Page_not_found + | `Connect_failed _e -> + `App (Errors.create (`Sql "Connect failed")) + | `Connect_rejected _e -> + `App (Errors.create (`Sql "Connect rejected")) + | `Post_connect _e -> + `App (Errors.create (`Sql "Post connect error")) ) in match Yojson.Safe.from_string body with | body ->