DeribitEx.TimeSyncService (deribit_ex v0.1.0)

View Source

A 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

server()

@type server() :: GenServer.server()

Time synchronization service server identifier.

start_options()

@type start_options() :: [sync_interval: integer(), name: GenServer.name()]

Options for starting the TimeSyncService.

state()

@type state() :: %{
  client_pid: pid(),
  interval: integer(),
  delta: time_delta(),
  last_sync: time_ms() | nil
}

Internal state of the TimeSyncService.

sync_info()

@type sync_info() :: %{delta: time_delta(), last_sync: time_ms() | nil}

Synchronization information returned by sync_info/1.

time_delta()

@type time_delta() :: integer()

Delta between server and local time in milliseconds.

time_ms()

@type time_ms() :: integer()

Time in milliseconds since epoch.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_time_delta(server \\ __MODULE__)

@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

local_to_server(local_time_ms, server \\ __MODULE__)

@spec local_to_server(time_ms(), server()) :: time_ms()

Converts a local timestamp to the corresponding server timestamp.

Parameters

  • local_time_ms - Local time in milliseconds since epoch
  • server - The TimeSyncService server (default: MODULE)

Returns

  • The corresponding server time in milliseconds

server_time(server \\ __MODULE__)

@spec server_time(server()) :: time_ms()

Gets the current estimated server time in milliseconds since epoch.

Returns

  • The current server time in milliseconds

server_to_local(server_time_ms, server \\ __MODULE__)

@spec server_to_local(time_ms(), server()) :: time_ms()

Converts a server timestamp to the corresponding local timestamp.

Parameters

  • server_time_ms - Server time in milliseconds since epoch
  • server - The TimeSyncService server (default: MODULE)

Returns

  • The corresponding local time in milliseconds

start_link(client_pid, opts \\ [])

@spec start_link(pid(), start_options()) :: GenServer.on_start()

Starts the TimeSyncService linked to the caller.

Parameters

  • client_pid - The PID of the Client connection
  • opts - 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

sync_info(server \\ __MODULE__)

@spec sync_info(server()) :: sync_info()

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

sync_now(server \\ __MODULE__)

@spec sync_now(server()) :: :ok

Forces an immediate time synchronization.

Parameters

  • server - The TimeSyncService server (default: MODULE)

Returns

  • :ok - Synchronization request was submitted