hitbox_backend/lib.rs
1#![warn(missing_docs)]
2//! Traits and struct messages for hitbox backend interaction.
3//!
4//! If you want implement your own backend, you in the right place.
5use actix::dev::ToEnvelope;
6use actix::prelude::*;
7use thiserror::Error;
8
9/// Define the behavior needed of an cache layer to work with cache backend.
10///
11/// Ultimately the implementing type must be an Actix `Actor` and it must implement handlers for a
12/// specific set of message types:
13///
14/// * [Get]
15/// * [Set]
16/// * [Lock]
17/// * [Delete]
18///
19/// [Get]: crate::Get
20/// [Set]: crate::Set
21/// [Delete]: crate::Delete
22/// [Lock]: crate::Lock
23pub trait Backend
24where
25 Self: Actor + Handler<Set> + Handler<Get> + Handler<Lock> + Handler<Delete>,
26{
27 /// Type of backend actor bound.
28 type Actor: Actor<Context = <Self as Backend>::Context>
29 + Handler<Set>
30 + Handler<Get>
31 + Handler<Lock>
32 + Handler<Delete>;
33 /// Type for backend Actor context.
34 type Context: ActorContext
35 + ToEnvelope<Self::Actor, Get>
36 + ToEnvelope<Self::Actor, Set>
37 + ToEnvelope<Self::Actor, Lock>
38 + ToEnvelope<Self::Actor, Delete>;
39}
40
41/// Proxy Error describes general groups of errors in backend interaction process.
42#[derive(Debug, Error)]
43pub enum BackendError {
44 /// Internal backend error, state or computation error.
45 ///
46 /// Any error not bounded with network interaction.
47 #[error(transparent)]
48 InternalError(Box<dyn std::error::Error + Send>),
49 /// Network interaction error.
50 #[error(transparent)]
51 ConnectionError(Box<dyn std::error::Error + Send>),
52}
53
54/// Actix message requests cache backend value by key.
55#[derive(Message, Debug, Clone, PartialEq)]
56#[rtype(result = "Result<Option<Vec<u8>>, BackendError>")]
57pub struct Get {
58 /// Key of cache backend record.
59 pub key: String,
60}
61
62/// Actix message writes cache backend value by key.
63#[derive(Message, Debug, Clone, PartialEq)]
64#[rtype(result = "Result<String, BackendError>")]
65pub struct Set {
66 /// Key of cache backend record.
67 pub key: String,
68 /// Data for sorage by cache key.
69 pub value: Vec<u8>,
70 /// Optional value of time-to-live for cache record.
71 pub ttl: Option<u32>,
72}
73
74/// Status of deleting result.
75#[derive(Debug, PartialEq)]
76pub enum DeleteStatus {
77 /// Record successfully deleted.
78 Deleted(u32),
79 /// Record already missing.
80 Missing,
81}
82
83/// Actix message delete record in backend by key.
84#[derive(Message, Debug, Clone, PartialEq)]
85#[rtype(result = "Result<DeleteStatus, BackendError>")]
86pub struct Delete {
87 /// Key of cache backend record for deleting
88 pub key: String,
89}
90
91/// Actix message creates lock in cache backend.
92#[derive(Message, Debug, Clone, PartialEq)]
93#[rtype(result = "Result<LockStatus, BackendError>")]
94pub struct Lock {
95 /// Key of cache backend record for lock.
96 pub key: String,
97 /// Time-to-live for cache key lock record.
98 pub ttl: u32,
99}
100
101/// Enum for representing status of Lock object in backend.
102#[derive(Debug, PartialEq)]
103pub enum LockStatus {
104 /// Lock successfully created and acquired.
105 Acquired,
106 /// Lock object already acquired (locked).
107 Locked,
108}