Skip to content

Commit

Permalink
Only create watcher once
Browse files Browse the repository at this point in the history
Use a ref cell to track whether a watcher has been created, and add
mutex to protect its access.

Signed-off-by: Vincent Liu <[email protected]>
  • Loading branch information
Vincent-lau committed Jun 19, 2024
1 parent bb2fe47 commit cb4329c
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions ocaml/xapi/xapi_clustering.ml
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,12 @@ module Watcher = struct
performing update"
__FUNCTION__ (Printexc.to_string exn)

let cluster_change_watcher : Thread.t option ref = ref None

let mu = Mutex.create ()

open Xapi_stdext_threads.Threadext

let watch_cluster_change ~__context ~host =
while !Daemon.enabled do
let m =
Expand All @@ -545,7 +551,8 @@ module Watcher = struct
warn "%s: Got exception %s while query cluster host updates, retrying"
__FUNCTION__ (Printexc.to_string exn) ;
Thread.delay 3.
done
done ;
Mutex.execute mu (fun () -> cluster_change_watcher := None)

(** [create_as_necessary] will create cluster watchers on the coordinator if they are not
already created.
Expand All @@ -557,7 +564,16 @@ module Watcher = struct
debug "%s: create watcher for corosync-notifyd on coordinator"
__FUNCTION__ ;

ignore
@@ Thread.create (fun () -> watch_cluster_change ~__context ~host) ()
Mutex.execute mu (fun () ->
match !cluster_change_watcher with
| None ->
cluster_change_watcher :=
Thread.create
(fun () -> watch_cluster_change ~__context ~host)
()
|> Option.some
| Some _ ->
()
)
)
end

0 comments on commit cb4329c

Please sign in to comment.