DeribitEx.TimeSyncService (deribit_ex v0.2.0)
View SourceA dedicated service for maintaining and tracking the time delta between the local system and the Deribit server. This service periodically synchronizes with the Deribit server time to ensure accurate timing for operations that depend on server time.
Features
- Periodically polls the server time to calculate time drift
- Provides functions to convert between local and server time
- Automatically adjusts for network latency in time calculations
- Uses a GenServer with configurable synchronization interval
Usage
# Start the service
{:ok, pid} = TimeSyncService.start_link(client_pid)
# Get the current server time (in milliseconds)
server_time_ms = TimeSyncService.server_time()
# Convert local time to server time
server_time = TimeSyncService.local_to_server(System.system_time(:millisecond))
# Convert server time to local time
local_time = TimeSyncService.server_to_local(server_time_ms)
Summary
Types
Time synchronization service server identifier.
Options for starting the TimeSyncService.
Internal state of the TimeSyncService.
Synchronization information returned by sync_info/1.
Delta between server and local time in milliseconds.
Time in milliseconds since epoch.
Functions
Returns a specification to start this module under a supervisor.
Gets the current time delta between local and server time in milliseconds.
Converts a local timestamp to the corresponding server timestamp.
Gets the current estimated server time in milliseconds since epoch.
Converts a server timestamp to the corresponding local timestamp.
Starts the TimeSyncService linked to the caller.
Returns information about the last synchronization.
Forces an immediate time synchronization.
Types
@type server() :: GenServer.server()
Time synchronization service server identifier.
@type start_options() :: [sync_interval: integer(), name: GenServer.name()]
Options for starting the TimeSyncService.
@type state() :: %{ client_pid: pid(), interval: integer(), delta: time_delta(), last_sync: time_ms() | nil }
Internal state of the TimeSyncService.
@type sync_info() :: %{delta: time_delta(), last_sync: time_ms() | nil}
Synchronization information returned by sync_info/1.
@type time_delta() :: integer()
Delta between server and local time in milliseconds.
@type time_ms() :: integer()
Time in milliseconds since epoch.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
@spec get_time_delta(server()) :: time_delta()
Gets the current time delta between local and server time in milliseconds.
Parameters
server
- The TimeSyncService server (default: MODULE)
Returns
- The time delta in milliseconds (server_time - local_time)
- Returns 0 if the service has not yet successfully synchronized
Converts a local timestamp to the corresponding server timestamp.
Parameters
local_time_ms
- Local time in milliseconds since epochserver
- The TimeSyncService server (default: MODULE)
Returns
- The corresponding server time in milliseconds
Gets the current estimated server time in milliseconds since epoch.
Returns
- The current server time in milliseconds
Converts a server timestamp to the corresponding local timestamp.
Parameters
server_time_ms
- Server time in milliseconds since epochserver
- The TimeSyncService server (default: MODULE)
Returns
- The corresponding local time in milliseconds
@spec start_link(pid(), start_options()) :: GenServer.on_start()
Starts the TimeSyncService linked to the caller.
Parameters
client_pid
- The PID of the Client connectionopts
- Options for the time sync service::sync_interval
- Interval between time syncs in milliseconds (default: 300_000 ms / 5 minutes):name
- Optional registration name for the server
Returns
{:ok, pid}
- The PID of the started service{:error, reason}
- If the service could not be started
Returns information about the last synchronization.
Parameters
server
- The TimeSyncService server (default: MODULE)
Returns
- A map containing
:delta
and:last_sync
(timestamp of last successful sync) - Returns nil for
:last_sync
if no successful synchronization has occurred yet
@spec sync_now(server()) :: :ok
Forces an immediate time synchronization.
Parameters
server
- The TimeSyncService server (default: MODULE)
Returns
:ok
- Synchronization request was submitted