@@ -10,6 +10,8 @@ defmodule HLClock.Timestamp do
1010 languages/representations.
1111 """
1212
13+ import Kernel , except: [ send: 2 ]
14+
1315 defstruct [ :time , :counter , :node_id ]
1416
1517 alias __MODULE__ , as: T
@@ -41,13 +43,24 @@ defmodule HLClock.Timestamp do
4143 Generate a single HLC Timestamp for sending to other nodes or
4244 local causality tracking
4345 """
44- def send ( % { time: old_time , counter: counter , node_id: node_id } , pt , max_drift ) do
46+ def send ( % { time: old_time , counter: counter , node_id: node_id } , pt ) do
4547 new_time = max ( old_time , pt )
4648 new_counter = advance_counter ( old_time , counter , new_time )
4749
48- with :ok <- handle_drift ( old_time , new_time , max_drift ) do
49- { :ok , new ( new_time , new_counter , node_id ) }
50- end
50+ # We were checking drift exception here, but functionally that
51+ # only catches errors caused by a system adminstrator setting the
52+ # system clock to jump or, more commonly, causing an error
53+ # whenever the computer running the vm sleeps.
54+ { :ok , new ( new_time , new_counter , node_id ) }
55+ end
56+
57+ # Compatibility for older users of Timestamp that may be providing the max_drift.
58+ def send (
59+ % { time: old_time , counter: counter , node_id: node_id } ,
60+ pt ,
61+ _max_drift
62+ ) do
63+ send ( % { time: old_time , counter: counter , node_id: node_id } , pt )
5164 end
5265
5366 @ doc """
@@ -185,8 +198,15 @@ defmodule HLClock.Timestamp do
185198 end
186199 end
187200
201+ # This was enforcing drift against abs(l - pt) which causes
202+ # timestamps in the past to raise a drift exception. The algorithm
203+ # only requires protecting ourselves against drift into the future
204+ # compared to our physical clock. Relaxing this constraint makes it
205+ # easier to always recv timestamps, simplifying client
206+ # implementations. Clients need to recv every timestamp that could
207+ # be in the future to preserve the causal order.
188208 defp drift? ( l , pt , max_drift ) do
189- abs ( l - pt ) > max_drift
209+ l - pt > max_drift
190210 end
191211
192212 defp advance_counter ( old_time , counter , new_time ) do
0 commit comments