@@ -25,31 +25,28 @@ defmodule HLClock.Timestamp do
2525 node's physical time), logical counter (initally zero), and the node id
2626 """
2727 def new ( time , counter , node_id \\ 0 ) do
28- cond do
29- byte_size ( :binary . encode_unsigned ( counter ) ) > 2 ->
30- { :error , :counter_too_large }
31-
32- byte_size ( :binary . encode_unsigned ( node_id ) ) > 8 ->
33- { :error , :node_id_too_large }
28+ assert_byte_size ( node_id , 8 )
29+ assert_byte_size ( counter , 2 )
30+ assert_byte_size ( time , 6 )
3431
35- byte_size ( :binary . encode_unsigned ( time ) ) > 6 ->
36- { :error , :time_too_large }
32+ % T { time: time , counter: counter , node_id: node_id }
33+ end
3734
38- true ->
39- { :ok , % T { time: time , counter: counter , node_id: node_id } }
40- end
35+ defp assert_byte_size ( value , size ) do
36+ byte_size ( :binary . encode_unsigned ( value ) ) <= size ||
37+ raise ArgumentError , " #{ value } exceeds max byte size of #{ size } "
4138 end
4239
4340 @ doc """
4441 Generate a single HLC Timestamp for sending to other nodes or
4542 local causality tracking
4643 """
47- def send ( % { time: old_time , counter: counter , node_id: node_id } , pt ) do
44+ def send ( % { time: old_time , counter: counter , node_id: node_id } , pt , max_drift ) do
4845 new_time = max ( old_time , pt )
4946 new_counter = advance_counter ( old_time , counter , new_time )
5047
51- with :ok <- handle_drift ( old_time , new_time ) do
52- new ( new_time , new_counter , node_id )
48+ with :ok <- handle_drift ( old_time , new_time , max_drift ) do
49+ { :ok , new ( new_time , new_counter , node_id ) }
5350 end
5451 end
5552
@@ -58,15 +55,20 @@ defmodule HLClock.Timestamp do
5855 perform the merge of both logical time and logical counters. Returns the new
5956 current timestamp for the local node
6057 """
61- def recv ( local , remote , physical_time ) do
58+ def recv ( local , remote , physical_time , max_drift ) do
6259 new_time = Enum . max ( [ physical_time , local . time , remote . time ] )
6360
6461 with { :ok , node_id } <- compare_node_ids ( local . node_id , remote . node_id ) ,
6562 :ok <-
66- handle_drift ( remote . time , physical_time , :remote_drift_violation ) ,
67- :ok <- handle_drift ( new_time , physical_time ) ,
63+ handle_drift (
64+ remote . time ,
65+ physical_time ,
66+ max_drift ,
67+ :remote_drift_violation
68+ ) ,
69+ :ok <- handle_drift ( new_time , physical_time , max_drift ) ,
6870 new_counter <- merge_logical ( new_time , local , remote ) do
69- new ( new_time , new_counter , node_id )
71+ { :ok , new ( new_time , new_counter , node_id ) }
7072 end
7173 end
7274
@@ -99,11 +101,8 @@ defmodule HLClock.Timestamp do
99101
100102 ## Example
101103
102- iex> {:ok, _t0} = HLClock.Timestamp.new(1410652800000, 0, 0)
103- {:ok, %HLClock.Timestamp{counter: 0, node_id: 0, time: 1410652800000}}
104-
105- ...> encoded = HLClock.Timestamp.encode(t0)
106- <<1, 72, 113, 117, 132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>
104+ iex> _t0 = HLClock.Timestamp.new(1410652800000, 0, 0)
105+ %HLClock.Timestamp{counter: 0, node_id: 0, time: 1410652800000}
107106
108107 ...> << time_and_counter :: size(64), _ :: size(64) >> = encoded
109108 <<1, 72, 113, 117, 132, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>
@@ -176,18 +175,18 @@ defmodule HLClock.Timestamp do
176175 end
177176 end
178177
179- defp handle_drift ( l , pt , err \\ :clock_drift_violation ) do
178+ defp handle_drift ( l , pt , max_drift , err \\ :clock_drift_violation ) do
180179 cond do
181- drift? ( l , pt ) ->
180+ drift? ( l , pt , max_drift ) ->
182181 { :error , err }
183182
184183 true ->
185184 :ok
186185 end
187186 end
188187
189- defp drift? ( l , pt ) do
190- abs ( l - pt ) > HLClock . max_drift ( )
188+ defp drift? ( l , pt , max_drift ) do
189+ abs ( l - pt ) > max_drift
191190 end
192191
193192 defp advance_counter ( old_time , counter , new_time ) do
0 commit comments