Trait plumber_rs::servlet::AsyncServlet [−][src]
pub trait AsyncServlet { type AsyncTaskData: Sized; fn init(
&mut self,
args: &[&str],
type_model: TypeModelObject
) -> ServletFuncResult; fn async_init(
&mut self,
handle: &AsyncTaskHandle
) -> Option<Box<Self::AsyncTaskData>>; fn async_exec(
handle: &AsyncTaskHandle,
task_data: &mut Self::AsyncTaskData
) -> ServletFuncResult; fn async_cleanup(
&mut self,
handle: &AsyncTaskHandle,
task_data: &mut Self::AsyncTaskData
) -> ServletFuncResult; fn cleanup(&mut self) -> ServletFuncResult; }
The trait for an asynchronous servlet.
An async servlet is a servlet that uses an async thread to run. It's also possible the async servlet uses some event driven model such as ASIO. This model is useful when the servlet isn't CPU bound and this will makes the task yield the worker thread to other event.
Associated Types
type AsyncTaskData: Sized
The private data buffer used by the async buffer.
A private data buffer for this async task is the data object that contains all the
information async_exec
would use and doesn't share to anyone else.
So this per task isolation eliminates the race condition of an async task.
See the async servlet programming model documentation for details
Required Methods
fn init(
&mut self,
args: &[&str],
type_model: TypeModelObject
) -> ServletFuncResult
&mut self,
args: &[&str],
type_model: TypeModelObject
) -> ServletFuncResult
The initialization function.
This should be called by the Plumber framework before the application gets started. All the pipe declaration should be done in this function.
args
: The servlet init argument listtype_model
: The type model object
Return the result of the servlet
fn async_init(
&mut self,
handle: &AsyncTaskHandle
) -> Option<Box<Self::AsyncTaskData>>
&mut self,
handle: &AsyncTaskHandle
) -> Option<Box<Self::AsyncTaskData>>
Initialize the async task.
This function will be called by the Plumber framework from any worker thread. The servlet should allocate the private data object which would be used for this task only.
handle
The async handle for this task
Return The newly created async task private data, None indicates failure
fn async_exec(
handle: &AsyncTaskHandle,
task_data: &mut Self::AsyncTaskData
) -> ServletFuncResult
handle: &AsyncTaskHandle,
task_data: &mut Self::AsyncTaskData
) -> ServletFuncResult
Run the execution task.
This function will be called by Plumber framework from any async processing thread. In this function, all the Plumber API beside the async handle related ones are disabled and will return an error when it gets called.
This is desgined for the slow operation such as network IO, database query, etc.
The task result should be put into task private data, so that the async_cleanup function can consume it.
handle
: The async task handletask_data
: The private task data
Returns the servlet function invocation result
fn async_cleanup(
&mut self,
handle: &AsyncTaskHandle,
task_data: &mut Self::AsyncTaskData
) -> ServletFuncResult
&mut self,
handle: &AsyncTaskHandle,
task_data: &mut Self::AsyncTaskData
) -> ServletFuncResult
The finalization step of an async task.
In this step, the async task execution result should have been propageated by the
async_exec
thread already. And this function is responsible to write the slow operation
result to the pipe as well as some task cleanup.
This function will be called by Plumber framework from the same worker thread as
async_init
. So all the Plumber API can be used in the execution stage can be used at this
point.
handle
: The async task handletask_data
: The async task private data
Return the servlet function invocation result
fn cleanup(&mut self) -> ServletFuncResult
The cleanup function
This should be called by Plumber framework when the Plumber application gets either killed or upgraded (and new version of the binary is loaded).
Return the servlet function result.
Implementors
impl AsyncServlet for Unimplemented type AsyncTaskData = ();