Hlc<T>.recv constructor

Hlc<T>.recv(
  1. Hlc<T> canonical,
  2. Hlc<T> remote, {
  3. int? millis,
})

Compares and validates a timestamp from a remote system with the local canonical timestamp to preserve monotonicity. Returns an updated canonical timestamp instance. Local wall time will be used if millis isn't supplied.

Implementation

factory Hlc.recv(Hlc<T> canonical, Hlc<T> remote, {int? millis}) {
  // Retrieve the local wall time if millis is null
  millis = millis ?? DateTime.now().millisecondsSinceEpoch;

  // No need to do any more work if the remote logical time is lower
  if (canonical.logicalTime >= remote.logicalTime) return canonical;

  // Assert the node id
  if (canonical.nodeId == remote.nodeId) {
    throw DuplicateNodeException(canonical.nodeId.toString());
  }
  // Assert the remote clock drift
  if (remote.millis - millis > _maxDrift) {
    throw ClockDriftException(remote.millis, millis);
  }

  return Hlc<T>.fromLogicalTime(remote.logicalTime, canonical.nodeId);
}