Struct pravega_client::event::EventWriter
source · pub struct EventWriter { /* private fields */ }
Expand description
Write events exactly once to a given stream.
EventWriter spawns a Reactor
that runs in the background for processing incoming events.
The write
method sends the event to the Reactor
asynchronously and returns a tokio::oneshot::Receiver
which contains the result of the write to the caller. The maximum size of the serialized event
supported is 8MB, writing size larger than that will return an error.
§Backpressure
Write has a backpressure mechanism. Internally, it uses Channel
to send event to
Reactor for processing. Channel
has a limited capacity
, when its capacity
is reached, any further write will not be accepted until enough space has been freed in the Channel
.
§Retry
The EventWriter implementation provides retry
logic to handle connection failures and service host
failures. Internal retries will not violate the exactly once semantic so it is better to rely on them
than to wrap this with custom retry logic.
§Examples
use pravega_client_config::ClientConfigBuilder;
use pravega_client::client_factory::ClientFactory;
use pravega_client_shared::ScopedStream;
#[tokio::main]
async fn main() {
// assuming Pravega controller is listening at endpoint `localhost:9090`
let config = ClientConfigBuilder::default()
.controller_uri("localhost:9090")
.build()
.expect("creating config");
let client_factory = ClientFactory::new(config);
// assuming scope:myscope and stream:mystream has been created before.
let stream = ScopedStream::from("myscope/mystream");
let mut event_writer = client_factory.create_event_writer(stream);
let payload = "hello world".to_string().into_bytes();
let result = event_writer.write_event(payload).await;
assert!(result.await.is_ok())
}
Implementations§
source§impl EventWriter
impl EventWriter
sourcepub async fn write_event(
&mut self,
event: Vec<u8>
) -> Receiver<Result<(), Error>> ⓘ
pub async fn write_event( &mut self, event: Vec<u8> ) -> Receiver<Result<(), Error>> ⓘ
Write an event without routing key.
A random routing key will be generated in this case.
This method sends the payload to a background task called reactor to process, so the success of this method only means the payload has been sent to the reactor. Applications may want to call await on the returned tokio oneshot to check whether the payload is successfully persisted or not. If oneshot returns an error indicating something is going wrong on the server side, then subsequent calls are also likely to fail.
§Examples
let mut event_writer = client_factory.create_event_writer(stream);
// result is a tokio oneshot
let result = event_writer.write_event(payload).await;
result.await.expect("flush to server");
Trait Implementations§
Auto Trait Implementations§
impl Freeze for EventWriter
impl !RefUnwindSafe for EventWriter
impl Send for EventWriter
impl Sync for EventWriter
impl Unpin for EventWriter
impl !UnwindSafe for EventWriter
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<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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> 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::Request