@@ -18,6 +18,10 @@ defmodule Plausible.Session.Transfer do
1818 @ cmd_dump_cache :get
1919 @ cmd_takeover_done :done
2020
21+ # NOTE: Timeout must be higher than respective WriteBuffer's
22+ # flush interval + some margin for flush alone.
23+ @ lock_acquire_timeout :timer . seconds ( 8000 )
24+
2125 def telemetry_event , do: [ :plausible , :sessions , :takeover ]
2226
2327 @ doc """
@@ -109,7 +113,15 @@ defmodule Plausible.Session.Transfer do
109113 case request do
110114 { @ cmd_list_cache_names , session_version } ->
111115 if session_version == session_version ( ) and attempted? ( parent ) do
112- # NOTE: block ingest here
116+ # Blocking ingest before transfer
117+ Task . await_many (
118+ [
119+ Task . async ( fn -> Plausible.Event.WriteBuffer . lock ( @ lock_acquire_timeout ) end ) ,
120+ Task . async ( fn -> Plausible.Session.WriteBuffer . lock ( @ lock_acquire_timeout ) end )
121+ ] ,
122+ @ lock_acquire_timeout + :timer . seconds ( 1 )
123+ )
124+
113125 Cache.Adapter . get_names ( :sessions )
114126 else
115127 [ ]
@@ -119,9 +131,11 @@ defmodule Plausible.Session.Transfer do
119131 Cache.Adapter . cache2list ( cache )
120132
121133 @ cmd_takeover_done ->
122- # NOTE: unblock ingest here
123134 # Wipe the cache after transfer is complete to avoid making the transferred cache stale
124135 Cache.Adapter . wipe ( :sessions )
136+ # Unblock ingest after trasnsfer is done
137+ Plausible.Event.WriteBuffer . unlock ( )
138+ Plausible.Session.WriteBuffer . unlock ( )
125139 :counters . add ( given_counter , 1 , 1 )
126140 end
127141 end
0 commit comments