@@ -74,10 +74,13 @@ let unlocked_gc () =
7474 ( if debug_enabled then
7575 let now = Unix. gettimeofday () in
7676 let string_of_id id =
77- let stunnel = Tbl. find ! stunnels id in
78- Printf. sprintf " (id %s / idle %.2f age %.2f)" (id_of_stunnel stunnel)
79- (now -. Hashtbl. find ! times id)
80- (now -. stunnel.Stunnel. connected_time)
77+ match (Tbl. find ! stunnels id, Hashtbl. find_opt ! times id) with
78+ | Some stunnel , Some stunnel_id ->
79+ Printf. sprintf " (id %s / idle %.2f age %.2f)"
80+ (id_of_stunnel stunnel) (now -. stunnel_id)
81+ (now -. stunnel.Stunnel. connected_time)
82+ | _ ->
83+ Printf. sprintf " %s: found no entry for id=%d" __FUNCTION__ id
8184 in
8285 let string_of_endpoint ep = Printf. sprintf " %s:%d" ep.host ep.port in
8386 let string_of_index ep xs =
@@ -134,20 +137,24 @@ let unlocked_gc () =
134137 let oldest_ids = List. map fst oldest in
135138 List. iter
136139 (fun x ->
137- let stunnel = Tbl. find ! stunnels x in
138- debug
139- " Expiring stunnel id %s since we have too many cached tunnels (limit \
140- is %d)"
141- (id_of_stunnel stunnel) max_stunnel
140+ match Tbl. find ! stunnels x with
141+ | Some stunnel ->
142+ debug
143+ " Expiring stunnel id %s since we have too many cached tunnels \
144+ (limit is %d)"
145+ (id_of_stunnel stunnel) max_stunnel
146+ | None ->
147+ debug " %s: Couldn't find an expiring stunnel (id=%d) in the table"
148+ __FUNCTION__ x
142149 )
143150 oldest_ids ;
144151 to_gc := ! to_gc @ oldest_ids
145152 ) ;
146153 (* Disconnect all stunnels we wish to GC *)
147154 List. iter
148155 (fun id ->
149- let s = Tbl. find ! stunnels id in
150- Stunnel. disconnect s
156+ (* Only remove stunnel if we find it in the table *)
157+ Option. iter ( fun s -> Stunnel. disconnect s) ( Tbl. find ! stunnels id)
151158 )
152159 ! to_gc ;
153160 (* Remove all reference to them from our cache hashtables *)
@@ -187,12 +194,7 @@ let add (x : Stunnel.t) =
187194 ; verified= x.Stunnel. verified
188195 }
189196 in
190- let existing =
191- if Hashtbl. mem ! index ep then
192- Hashtbl. find ! index ep
193- else
194- []
195- in
197+ let existing = Option. value (Hashtbl. find_opt ! index ep) ~default: [] in
196198 Hashtbl. replace ! index ep (idx :: existing) ;
197199 debug " Adding stunnel id %s (idle %.2f) to the cache" (id_of_stunnel x) 0. ;
198200 unlocked_gc ()
@@ -206,23 +208,33 @@ let with_remove ~host ~port verified f =
206208 let get_id () =
207209 with_lock m (fun () ->
208210 unlocked_gc () ;
209- let ids = Hashtbl. find ! index ep in
210- let table = List. map (fun id -> (id, Hashtbl. find ! times id)) ids in
211+ let ( let * ) = Option. bind in
212+ let * ids = Hashtbl. find_opt ! index ep in
213+ let table =
214+ List. filter_map
215+ (fun id ->
216+ Option. map (fun time -> (id, time)) (Hashtbl. find_opt ! times id)
217+ )
218+ ids
219+ in
211220 let sorted = List. sort (fun a b -> compare (snd a) (snd b)) table in
212221 match sorted with
213222 | (id , time ) :: _ ->
214- let stunnel = Tbl. find ! stunnels id in
215- debug " Removing stunnel id %s (idle %.2f) from the cache"
216- (id_of_stunnel stunnel)
217- (Unix. gettimeofday () -. time) ;
223+ Option. iter
224+ (fun stunnel ->
225+ debug " Removing stunnel id %s (idle %.2f) from the cache"
226+ (id_of_stunnel stunnel)
227+ (Unix. gettimeofday () -. time)
228+ )
229+ (Tbl. find ! stunnels id) ;
218230 Hashtbl. remove ! times id ;
219231 Hashtbl. replace ! index ep (List. filter (fun x -> x <> id) ids) ;
220- id
232+ Some id
221233 | _ ->
222- raise Not_found
234+ None
223235 )
224236 in
225- let id_opt = try Some ( get_id () ) with Not_found -> None in
237+ let id_opt = get_id () in
226238 id_opt
227239 |> Option. map @@ fun id ->
228240 (* cannot call while holding above mutex or we deadlock *)
0 commit comments