Trait Global

Source
pub trait Global:
    Send
    + Sync
    + 'static {
    type Config: ConfigParser + Send + 'static;

    // Required method
    fn init(
        config: Self::Config,
    ) -> impl Future<Output = Result<Arc<Self>>> + Send;

    // Provided methods
    fn pre_init() -> Result<()> { ... }
    fn tokio_runtime() -> Runtime { ... }
    fn on_services_start(
        self: &Arc<Self>,
    ) -> impl Future<Output = Result<()>> + Send { ... }
    fn on_service_exit(
        self: &Arc<Self>,
        name: &'static str,
        result: Result<()>,
    ) -> impl Future<Output = Result<()>> + Send { ... }
    fn on_exit(
        self: &Arc<Self>,
        result: Result<()>,
    ) -> impl Future<Output = Result<()>> + Send { ... }
}
Expand description

This trait is implemented for the global type of your application. It is intended to be used to store any global state of your application. When using the main! macro, one instance of this type will be made available to all services.

Using this trait requires a config type implementing ConfigParser. If your application does not have a config, consider using the GlobalWithoutConfig trait.

§See Also

Required Associated Types§

Source

type Config: ConfigParser + Send + 'static

The config type for the global.

This type is expected to implement ConfigParser.

Required Methods§

Source

fn init(config: Self::Config) -> impl Future<Output = Result<Arc<Self>>> + Send

Initialize the global.

Called to initialize the global. Returning an error from this function will cause the process to immediately exit without calling on_exit first.

Provided Methods§

Source

fn pre_init() -> Result<()>

Pre-initialization.

Called before initializing the tokio runtime and loading the config. Returning an error from this function will cause the process to immediately exit without calling on_exit first.

Source

fn tokio_runtime() -> Runtime

Builds the tokio runtime for the process.

If not overridden, a default runtime builder is used to build the runtime. It uses the following environment variables:

  • TOKIO_WORKER_THREADS: Number of worker threads to use. If 1, a current thread runtime is used.

    See [tokio::runtime::Builder::worker_threads] for details.

  • TOKIO_MAX_BLOCKING_THREADS: Maximum number of blocking threads.

    See [tokio::runtime::Builder::max_blocking_threads] for details.

  • TOKIO_DISABLE_TIME: If true disables time.

    See [tokio::runtime::Builder::enable_time] for details.

  • TOKIO_DISABLE_IO: If true disables IO.

    See [tokio::runtime::Builder::enable_io] for details.

  • TOKIO_THREAD_STACK_SIZE: Thread stack size.

    See [tokio::runtime::Builder::thread_stack_size] for details.

  • TOKIO_GLOBAL_QUEUE_INTERVAL: Global queue interval.

    See [tokio::runtime::Builder::global_queue_interval] for details.

  • TOKIO_EVENT_INTERVAL: Event interval.

    See [tokio::runtime::Builder::event_interval] for details.

  • TOKIO_MAX_IO_EVENTS_PER_TICK: Maximum IO events per tick.

    See [tokio::runtime::Builder::max_io_events_per_tick] for details.

Source

fn on_services_start( self: &Arc<Self>, ) -> impl Future<Output = Result<()>> + Send

Called right before all services start.

Returning an error from this function will prevent any service from starting and on_exit will be called with the result of this function.

Source

fn on_service_exit( self: &Arc<Self>, name: &'static str, result: Result<()>, ) -> impl Future<Output = Result<()>> + Send

Called after a service exits.

name is the name of the service that exited and result is the result the service exited with. Returning an error from this function will stop all currently running services and on_exit will be called with the result of this function.

Source

fn on_exit( self: &Arc<Self>, result: Result<()>, ) -> impl Future<Output = Result<()>> + Send

Called after the shutdown is complete, right before exiting the process.

result is Err when the process exits due to an error in one of the services or handler functions, otherwise Ok(()).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§