pub struct ManagedBacktestSession { /* private fields */ }Expand description
A session that reconnects on its own.
Self::startit.- Call
subscribe_*for the notifications you need. - Drive it: read
Self::next_event, and answer eachManagedEvent::ReadyForContinuewith oneSelf::send_continueorSelf::send_continue_to. Never answerManagedEvent::Paused: aReadyForContinuefollows it, and answering both sends two continues.Self::run_to_completionruns this loop for you. Self::shutdownit. Dropping it instead cancels the session without waiting for its tasks to stop.
See the module docs for what happens when a connection drops.
Implementations§
Source§impl ManagedBacktestSession
impl ManagedBacktestSession
Sourcepub async fn start(
url: String,
api_key: String,
create: CreateBacktestSessionRequest,
) -> Result<Self, ManagedSessionError>
pub async fn start( url: String, api_key: String, create: CreateBacktestSessionRequest, ) -> Result<Self, ManagedSessionError>
Create a session and return once this client is attached to it on the session’s own endpoint.
url may be a bare host (see StartSessionParams::url). Build create with
crate::CreateSession and into_request. Use Self::start_with_params for extra
headers or your own cancellation token.
§Errors
ManagedSessionError::ApiKeyRejectedif the server refuses the API key.ManagedSessionError::Createif the server rejects the create request, the connection fails past the retry budget, or the session’s endpoint is not reachable (CreateError::EndpointUnreachable).ManagedSessionError::SessionTokenRejectedif the session’s endpoint refuses the token.
Sourcepub async fn start_with_params(
params: StartSessionParams,
reconnect_coordinator: Option<Arc<ReconnectCoordinator>>,
) -> Result<Self, ManagedSessionError>
pub async fn start_with_params( params: StartSessionParams, reconnect_coordinator: Option<Arc<ReconnectCoordinator>>, ) -> Result<Self, ManagedSessionError>
Like Self::start, from explicit StartSessionParams. Pass None for
reconnect_coordinator; it is internal plumbing for parallel runs.
A create that leaves disconnect_timeout_secs unset gets
DEFAULT_DISCONNECT_TIMEOUT_SECS, so the server keeps the session while the client
reconnects.
§Errors
As Self::start, plus ManagedSessionError::Cancelled if
StartSessionParams::parent_cancel fires first.
Sourcepub fn session_info(&self) -> &SessionInfo
pub fn session_info(&self) -> &SessionInfo
Metadata reported by the server when the session was created.
Sourcepub fn rpc(&self) -> &RpcClient
pub fn rpc(&self) -> &RpcClient
RPC client (confirmed commitment) for the session’s chain state at its current slot;
serves from the first ReadyForContinue until Completed, and 502s after.
Sourcepub fn set_completion_drain_timeout(&mut self, idle_timeout: Duration)
pub fn set_completion_drain_timeout(&mut self, idle_timeout: Duration)
Override the completion-drain idle timeout (default 60 s). The drain gives up only after this long with no trailing notification; it does not cap total drain time.
Sourcepub fn subscribe_transactions(&mut self, program_ids: Vec<Address>)
pub fn subscribe_transactions(&mut self, program_ids: Vec<Address>)
Subscribe to every transaction that mentions any of program_ids. Each arrives as
ManagedEvent::Transaction, with program logs in meta.log_messages.
Every subscribe_* call opens one WebSocket on the session’s RPC endpoint; the next
send_continue waits until it is connected.
Sourcepub fn subscribe_account_diffs(&mut self, program_ids: Vec<Address>)
pub fn subscribe_account_diffs(&mut self, program_ids: Vec<Address>)
Subscribe to changes of every account owned by any of program_ids. Each change arrives
as ManagedEvent::AccountDiff. To follow single accounts across reconnects, subscribe
to their owner program and keep the diffs for their addresses.
crate::subscribe_account_diffs follows single accounts but does not reconnect.
Sourcepub fn subscribe_actions(&mut self)
pub fn subscribe_actions(&mut self)
Subscribe to the results of the pre-scheduled actions in the create request. Each arrives
as ManagedEvent::ActionResult.
Sourcepub fn subscribe_replacements(&mut self)
pub fn subscribe_replacements(&mut self)
Subscribe to the reroute results for each original swap: re-quotes, venue probes and the
original swaps replayed beside them. One stream carries all three, as
ManagedEvent::Replacement.
Sourcepub fn subscribe_diagnostics(&mut self)
pub fn subscribe_diagnostics(&mut self)
Subscribe to the samples of the diagnostic probes in the create request. Each arrives as
ManagedEvent::Diagnostic.
Sourcepub async fn next_event(&mut self) -> Result<ManagedEvent, ManagedSessionError>
pub async fn next_event(&mut self) -> Result<ManagedEvent, ManagedSessionError>
Wait for the next event. See ManagedEvent for the order events arrive in.
§Errors
ManagedSessionError::ControlClosedonce the session is over.ManagedSessionError::ControlFailedonce the session control channel gave up.ManagedSessionError::SubscriptionFailedin place ofCompletedwhen a subscription’s stream ended early.ManagedSessionError::Cancelledif the session was cancelled.
Sourcepub async fn send_continue(
&mut self,
params: ContinueParams,
) -> Result<(), ManagedSessionError>
pub async fn send_continue( &mut self, params: ContinueParams, ) -> Result<(), ManagedSessionError>
Wait until the session control channel and every subscription are connected, then send
a continue. Build params with crate::Continue.
Call this once per ManagedEvent::ReadyForContinue.
§Errors
ManagedSessionError::ControlFailedorManagedSessionError::SubscriptionFailedif a connection gave up.ManagedSessionError::ContinueSendif the session has ended.ManagedSessionError::Cancelledif the session was cancelled.
Sourcepub async fn send_continue_to(
&mut self,
params: ContinueToParams,
) -> Result<(), ManagedSessionError>
pub async fn send_continue_to( &mut self, params: ContinueToParams, ) -> Result<(), ManagedSessionError>
Wait until the session control channel and every subscription are connected, then send
a continueTo: execute through params.slot, or with batch_index, up to just before
that batch of it. Call this once per ManagedEvent::ReadyForContinue.
The session then sends ManagedEvent::Paused, where you can read state, and
ManagedEvent::ReadyForContinue, which you answer. Rules for params:
slotmust be after the last executed slot. Otherwisenext_eventreturns anInvalidRequestManagedEvent::Errorand the session stays where it is.slotmust have a block. A skipped slot has none, and the session does not stop at it: it runs on to the end of the range.- With a
ManagedEvent::DiscoveryBatch, pass itsslotandbatch_indexto pause just before the matching batch.
To run your transactions just before slot n, pause at n - 1, then send them with the
next continue. They run and commit before slot n’s block:
use simulator_client::{Continue, ContinueToParams, ManagedBacktestSession, ManagedEvent};
use solana_transaction::versioned::VersionedTransaction;
async fn run_before(
session: &mut ManagedBacktestSession,
n: u64,
tx: &VersionedTransaction,
) -> Result<(), Box<dyn std::error::Error>> {
let pause = ContinueToParams { slot: n - 1, batch_index: None };
session.send_continue_to(pause).await?;
loop {
match session.next_event().await? {
ManagedEvent::ReadyForContinue => break,
ManagedEvent::Error(err) => return Err(err.into()),
_ => {}
}
}
let step = Continue::builder().build().push_transaction(tx)?.into_params();
session.send_continue(step).await?;
Ok(())
}If slot n - 1 may be skipped, or to skip the round trip, schedule the transaction
instead: a ScheduledAction with a BeforeSlot anchor
and ActionKind::Send.
§Errors
Sourcepub async fn wait_until_ready(&mut self) -> Result<(), ManagedSessionError>
pub async fn wait_until_ready(&mut self) -> Result<(), ManagedSessionError>
Wait for the first ReadyForContinue, from which the session’s RPC endpoint serves the
state before the start slot executes (the end of slot start_slot - 1). Earlier events
(only Status in practice) are discarded.
§Errors
ManagedSessionError::CompletedBeforeReady, ManagedSessionError::Remote on an
Error event, or any error of Self::next_event. The session is not shut down; call
Self::shutdown.
Sourcepub async fn run_to_completion(
self,
step: ContinueParams,
on_event: impl FnMut(ManagedEvent),
) -> Result<Option<SessionSummary>, ManagedSessionError>
pub async fn run_to_completion( self, step: ContinueParams, on_event: impl FnMut(ManagedEvent), ) -> Result<Option<SessionSummary>, ManagedSessionError>
On every ReadyForContinue, send step.clone() until Completed; every other event,
Paused included, goes to on_event. Any Error event, terminal or not, stops the loop
and returns Remote. Always shuts the session down before returning. A caller that already
consumed a ready (e.g. via Self::wait_until_ready) sends its first
Self::send_continue itself.
Returns the summary carried by Completed.
§Errors
ManagedSessionError::Remote on an Error event, or any error of Self::next_event
and Self::send_continue.
Trait Implementations§
Source§impl Drop for ManagedBacktestSession
impl Drop for ManagedBacktestSession
Auto Trait Implementations§
impl !RefUnwindSafe for ManagedBacktestSession
impl !UnwindSafe for ManagedBacktestSession
impl Forget for ManagedBacktestSession
impl Freeze for ManagedBacktestSession
impl Move for ManagedBacktestSession
impl Send for ManagedBacktestSession
impl Sync for ManagedBacktestSession
impl Unpin for ManagedBacktestSession
impl UnsafeUnpin for ManagedBacktestSession
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.