create ~server_name ~on_event ~retry_delay get_address
returns a persistent connection to a server whose host and port are obtained via get_address
every time we try to connect. For example, get_address
might look up a server's host and port in catalog at a particular path to which multiple redundant copies of a service are publishing their location. If one copy dies, we get the address of the another one when looking up the address afterwards.
All connection events (see the type above) are passed to the on_event
callback, if given. When this callback becomes determined, we move on to the next step in our connection attempt (e.g. we won't actually attempt to connect until on_event Attempting_to_connect
is finished). Note that on_event Disconnected
will only be called once on_event (Connected conn)
finishes even if the connection goes down during that callback.
`Failed_to_connect error
and `Obtained_address addr
events are only reported if they are distinct from the most recent event of the same type that has taken place since the most recent `Attempting_to_connect
event.
Connection is retried after Time.Span.randomize ~percent:(Percent.of_mult 0.3)
(retry_delay ())
. The default for retry_delay
is const (sec 10.)
. Note that what this retry delay actually throttles is the delay between two connection attempts, so when a long-lived connection dies, connection is usually immediately retried, and if that failed, wait for another retry delay and retry.
The random_state
and time_source
arguments are there to make persistent connection code more deterministically testable. They default to Random.State.default
and Time_source.wall_clock ()
, respectively.