Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor Hashtbl.find out of resources/table.ml
Signed-off-by: Andrii Sultanov <[email protected]>
  • Loading branch information
Andrii Sultanov committed Jul 4, 2024
commit 1869b443a0391f7af71296198eb2b9477d9423a2
2 changes: 1 addition & 1 deletion ocaml/libs/resources/table.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct
Hashtbl.remove t k
)

let find (t, m) k = with_lock m (fun () -> Hashtbl.find t k)
let find (t, m) k = with_lock m (fun () -> Hashtbl.find_opt t k)

let with_find_moved_exn (t, m) k =
let v =
Expand Down
57 changes: 37 additions & 20 deletions ocaml/libs/stunnel/stunnel_cache.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ let unlocked_gc () =
( if debug_enabled then
let now = Unix.gettimeofday () in
let string_of_id id =
let stunnel = Tbl.find !stunnels id in
Printf.sprintf "(id %s / idle %.2f age %.2f)" (id_of_stunnel stunnel)
(now -. Hashtbl.find !times id)
(now -. stunnel.Stunnel.connected_time)
match (Tbl.find !stunnels id, Hashtbl.find_opt !times id) with
| Some stunnel, Some stunnel_id ->
Printf.sprintf "(id %s / idle %.2f age %.2f)"
(id_of_stunnel stunnel) (now -. stunnel_id)
(now -. stunnel.Stunnel.connected_time)
| _ ->
Printf.sprintf "%s: found no entry for id=%d" __FUNCTION__ id
in
let string_of_endpoint ep = Printf.sprintf "%s:%d" ep.host ep.port in
let string_of_index ep xs =
Expand Down Expand Up @@ -134,20 +137,24 @@ let unlocked_gc () =
let oldest_ids = List.map fst oldest in
List.iter
(fun x ->
let stunnel = Tbl.find !stunnels x in
debug
"Expiring stunnel id %s since we have too many cached tunnels (limit \
is %d)"
(id_of_stunnel stunnel) max_stunnel
match Tbl.find !stunnels x with
| Some stunnel ->
debug
"Expiring stunnel id %s since we have too many cached tunnels \
(limit is %d)"
(id_of_stunnel stunnel) max_stunnel
| None ->
debug "%s: Couldn't find an expiring stunnel (id=%d) in the table"
__FUNCTION__ x
)
oldest_ids ;
to_gc := !to_gc @ oldest_ids
) ;
(* Disconnect all stunnels we wish to GC *)
List.iter
(fun id ->
let s = Tbl.find !stunnels id in
Stunnel.disconnect s
(* Only remove stunnel if we find it in the table *)
Option.iter (fun s -> Stunnel.disconnect s) (Tbl.find !stunnels id)
)
!to_gc ;
(* Remove all reference to them from our cache hashtables *)
Expand Down Expand Up @@ -201,23 +208,33 @@ let with_remove ~host ~port verified f =
let get_id () =
with_lock m (fun () ->
unlocked_gc () ;
let ids = Hashtbl.find !index ep in
let table = List.map (fun id -> (id, Hashtbl.find !times id)) ids in
let ( let* ) = Option.bind in
let* ids = Hashtbl.find_opt !index ep in
let table =
List.filter_map
(fun id ->
Option.map (fun time -> (id, time)) (Hashtbl.find_opt !times id)
)
ids
in
let sorted = List.sort (fun a b -> compare (snd a) (snd b)) table in
match sorted with
| (id, time) :: _ ->
let stunnel = Tbl.find !stunnels id in
debug "Removing stunnel id %s (idle %.2f) from the cache"
(id_of_stunnel stunnel)
(Unix.gettimeofday () -. time) ;
Option.iter
(fun stunnel ->
debug "Removing stunnel id %s (idle %.2f) from the cache"
(id_of_stunnel stunnel)
(Unix.gettimeofday () -. time)
)
(Tbl.find !stunnels id) ;
Hashtbl.remove !times id ;
Hashtbl.replace !index ep (List.filter (fun x -> x <> id) ids) ;
id
Some id
| _ ->
raise Not_found
None
)
in
let id_opt = try Some (get_id ()) with Not_found -> None in
let id_opt = get_id () in
id_opt
|> Option.map @@ fun id ->
(* cannot call while holding above mutex or we deadlock *)
Expand Down