add frontend folder

This commit is contained in:
appflowy
2021-11-20 09:32:46 +08:00
parent f93f012bc8
commit 8f1d62f115
1697 changed files with 754 additions and 104 deletions

View File

@ -0,0 +1,23 @@
[package]
name = "lib-infra"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
uuid = { version = "0.8", features = ["serde", "v4"] }
diesel = {version = "1.4.8", features = ["sqlite"]}
diesel_derives = {version = "1.4.1", features = ["sqlite"]}
diesel_migrations = {version = "1.4.0", features = ["sqlite"]}
flowy-derive = { path = "../flowy-derive"}
lib-sqlite = { path = "../lib-sqlite" }
lazy_static = "1.4.0"
protobuf = {version = "2.18.0"}
log = "0.4.14"
chrono = "0.4.19"
bytes = { version = "1.0" }
pin-project = "1.0"
futures-core = { version = "0.3", default-features = false }
tokio = { version = "1.0", features = ["time", "rt"] }
rand = "0.8.3"

View File

@ -0,0 +1,2 @@
proto_crates = ["src/kv"]
event_files = []

View File

@ -0,0 +1,68 @@
use futures_core::ready;
use pin_project::pin_project;
use std::{
fmt::Debug,
future::Future,
pin::Pin,
task::{Context, Poll},
};
pub fn wrap_future<T, O>(f: T) -> FnFuture<O>
where
T: Future<Output = O> + Send + Sync + 'static,
{
FnFuture { fut: Box::pin(f) }
}
#[pin_project]
pub struct FnFuture<T> {
#[pin]
pub fut: Pin<Box<dyn Future<Output = T> + Sync + Send>>,
}
impl<T> Future for FnFuture<T>
where
T: Send + Sync,
{
type Output = T;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.as_mut().project();
loop {
return Poll::Ready(ready!(this.fut.poll(cx)));
}
}
}
#[pin_project]
pub struct ResultFuture<T, E> {
#[pin]
pub fut: Pin<Box<dyn Future<Output = Result<T, E>> + Sync + Send>>,
}
impl<T, E> ResultFuture<T, E> {
pub fn new<F>(f: F) -> Self
where
F: Future<Output = Result<T, E>> + Send + Sync + 'static,
{
Self {
fut: Box::pin(async { f.await }),
}
}
}
impl<T, E> Future for ResultFuture<T, E>
where
T: Send + Sync,
E: Debug,
{
type Output = Result<T, E>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.as_mut().project();
loop {
let result = ready!(this.fut.poll(cx));
return Poll::Ready(result);
}
}
}

View File

@ -0,0 +1,233 @@
use crate::kv::schema::{kv_table, kv_table::dsl, KV_SQL};
use ::diesel::{query_dsl::*, ExpressionMethods};
use diesel::{Connection, SqliteConnection};
use flowy_derive::ProtoBuf;
use lazy_static::lazy_static;
use lib_sqlite::{DBConnection, Database, PoolConfig};
use std::{collections::HashMap, path::Path, sync::RwLock};
const DB_NAME: &str = "kv.db";
lazy_static! {
static ref KV_HOLDER: RwLock<KV> = RwLock::new(KV::new());
}
pub struct KV {
database: Option<Database>,
cache: HashMap<String, KeyValue>,
}
impl KV {
fn new() -> Self {
KV {
database: None,
cache: HashMap::new(),
}
}
fn set(value: KeyValue) -> Result<(), String> {
log::debug!("set value: {:?}", value);
update_cache(value.clone());
let _ = diesel::replace_into(kv_table::table)
.values(&value)
.execute(&*(get_connection()?))
.map_err(|e| format!("KV set error: {:?}", e))?;
Ok(())
}
fn get(key: &str) -> Result<KeyValue, String> {
if let Some(value) = read_cache(key) {
return Ok(value);
}
let conn = get_connection()?;
let value = dsl::kv_table
.filter(kv_table::key.eq(key))
.first::<KeyValue>(&*conn)
.map_err(|e| format!("KV get error: {:?}", e))?;
update_cache(value.clone());
Ok(value)
}
#[allow(dead_code)]
pub fn remove(key: &str) -> Result<(), String> {
log::debug!("remove key: {}", key);
match KV_HOLDER.write() {
Ok(mut guard) => {
guard.cache.remove(key);
},
Err(e) => log::error!("Require write lock failed: {:?}", e),
};
let conn = get_connection()?;
let sql = dsl::kv_table.filter(kv_table::key.eq(key));
let _ = diesel::delete(sql)
.execute(&*conn)
.map_err(|e| format!("KV remove error: {:?}", e))?;
Ok(())
}
pub fn init(root: &str) -> Result<(), String> {
if !Path::new(root).exists() {
return Err(format!("Init KVStore failed. {} not exists", root));
}
let pool_config = PoolConfig::default();
let database = Database::new(root, DB_NAME, pool_config).unwrap();
let conn = database.get_connection().unwrap();
SqliteConnection::execute(&*conn, KV_SQL).unwrap();
let mut store = KV_HOLDER
.write()
.map_err(|e| format!("KVStore write failed: {:?}", e))?;
store.database = Some(database);
Ok(())
}
}
fn read_cache(key: &str) -> Option<KeyValue> {
match KV_HOLDER.read() {
Ok(guard) => guard.cache.get(key).cloned(),
Err(e) => {
log::error!("Require read lock failed: {:?}", e);
None
},
}
}
fn update_cache(value: KeyValue) {
match KV_HOLDER.write() {
Ok(mut guard) => {
guard.cache.insert(value.key.clone(), value);
},
Err(e) => log::error!("Require write lock failed: {:?}", e),
};
}
macro_rules! impl_get_func {
(
$func_name:ident,
$get_method:ident=>$target:ident
) => {
impl KV {
#[allow(dead_code)]
pub fn $func_name(k: &str) -> Option<$target> {
match KV::get(k) {
Ok(item) => item.$get_method,
Err(_) => None,
}
}
}
};
}
macro_rules! impl_set_func {
($func_name:ident,$set_method:ident,$key_type:ident) => {
impl KV {
#[allow(dead_code)]
pub fn $func_name(key: &str, value: $key_type) {
let mut item = KeyValue::new(key);
item.$set_method = Some(value);
match KV::set(item) {
Ok(_) => {},
Err(e) => {
log::error!("{:?}", e)
},
};
}
}
};
}
impl_set_func!(set_str, str_value, String);
impl_set_func!(set_bool, bool_value, bool);
impl_set_func!(set_int, int_value, i64);
impl_set_func!(set_float, float_value, f64);
impl_get_func!(get_str,str_value=>String);
impl_get_func!(get_int,int_value=>i64);
impl_get_func!(get_float,float_value=>f64);
impl_get_func!(get_bool,bool_value=>bool);
fn get_connection() -> Result<DBConnection, String> {
match KV_HOLDER.read() {
Ok(store) => {
let conn = store
.database
.as_ref()
.expect("KVStore is not init")
.get_connection()
.map_err(|e| format!("KVStore error: {:?}", e))?;
Ok(conn)
},
Err(e) => {
let msg = format!("KVStore get connection failed: {:?}", e);
log::error!("{:?}", msg);
Err(msg)
},
}
}
#[derive(Clone, Debug, ProtoBuf, Default, Queryable, Identifiable, Insertable, AsChangeset)]
#[table_name = "kv_table"]
#[primary_key(key)]
pub struct KeyValue {
#[pb(index = 1)]
pub key: String,
#[pb(index = 2, one_of)]
pub str_value: Option<String>,
#[pb(index = 3, one_of)]
pub int_value: Option<i64>,
#[pb(index = 4, one_of)]
pub float_value: Option<f64>,
#[pb(index = 5, one_of)]
pub bool_value: Option<bool>,
}
impl KeyValue {
pub fn new(key: &str) -> Self {
KeyValue {
key: key.to_string(),
..Default::default()
}
}
}
#[cfg(test)]
mod tests {
use crate::kv::KV;
#[test]
fn kv_store_test() {
let dir = "./temp/";
if !std::path::Path::new(dir).exists() {
std::fs::create_dir_all(dir).unwrap();
}
KV::init(dir).unwrap();
KV::set_str("1", "hello".to_string());
assert_eq!(KV::get_str("1").unwrap(), "hello");
assert_eq!(KV::get_str("2"), None);
KV::set_bool("1", true);
assert_eq!(KV::get_bool("1").unwrap(), true);
assert_eq!(KV::get_bool("2"), None);
}
}

View File

@ -0,0 +1,4 @@
mod kv;
mod schema;
pub use kv::*;

View File

@ -0,0 +1,20 @@
#[allow(dead_code)]
pub const KV_SQL: &str = r#"
CREATE TABLE IF NOT EXISTS kv_table (
key TEXT NOT NULL PRIMARY KEY,
str_value TEXT,
int_value BIGINT,
float_value DOUBLE,
bool_value BOOLEAN
);
"#;
table! {
kv_table (key) {
key -> Text,
str_value -> Nullable<Text>,
int_value -> Nullable<BigInt>,
float_value -> Nullable<Double>,
bool_value -> Nullable<Bool>,
}
}

View File

@ -0,0 +1,16 @@
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_derives;
pub mod future;
pub mod kv;
mod protobuf;
pub mod retry;
#[allow(dead_code)]
pub fn uuid() -> String { uuid::Uuid::new_v4().to_string() }
#[allow(dead_code)]
pub fn timestamp() -> i64 { chrono::Utc::now().timestamp() }

View File

@ -0,0 +1,4 @@
mod model;
pub use model::*;

View File

@ -0,0 +1,478 @@
// This file is generated by rust-protobuf 2.22.1. Do not edit
// @generated
// https://github.com/rust-lang/rust-clippy/issues/702
#![allow(unknown_lints)]
#![allow(clippy::all)]
#![allow(unused_attributes)]
#![cfg_attr(rustfmt, rustfmt::skip)]
#![allow(box_pointers)]
#![allow(dead_code)]
#![allow(missing_docs)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(trivial_casts)]
#![allow(unused_imports)]
#![allow(unused_results)]
//! Generated file from `kv.proto`
/// Generated files are compatible only with the same version
/// of protobuf runtime.
// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_22_1;
#[derive(PartialEq,Clone,Default)]
pub struct KeyValue {
// message fields
pub key: ::std::string::String,
// message oneof groups
pub one_of_str_value: ::std::option::Option<KeyValue_oneof_one_of_str_value>,
pub one_of_int_value: ::std::option::Option<KeyValue_oneof_one_of_int_value>,
pub one_of_float_value: ::std::option::Option<KeyValue_oneof_one_of_float_value>,
pub one_of_bool_value: ::std::option::Option<KeyValue_oneof_one_of_bool_value>,
// special fields
pub unknown_fields: ::protobuf::UnknownFields,
pub cached_size: ::protobuf::CachedSize,
}
impl<'a> ::std::default::Default for &'a KeyValue {
fn default() -> &'a KeyValue {
<KeyValue as ::protobuf::Message>::default_instance()
}
}
#[derive(Clone,PartialEq,Debug)]
pub enum KeyValue_oneof_one_of_str_value {
str_value(::std::string::String),
}
#[derive(Clone,PartialEq,Debug)]
pub enum KeyValue_oneof_one_of_int_value {
int_value(i64),
}
#[derive(Clone,PartialEq,Debug)]
pub enum KeyValue_oneof_one_of_float_value {
float_value(f64),
}
#[derive(Clone,PartialEq,Debug)]
pub enum KeyValue_oneof_one_of_bool_value {
bool_value(bool),
}
impl KeyValue {
pub fn new() -> KeyValue {
::std::default::Default::default()
}
// string key = 1;
pub fn get_key(&self) -> &str {
&self.key
}
pub fn clear_key(&mut self) {
self.key.clear();
}
// Param is passed by value, moved
pub fn set_key(&mut self, v: ::std::string::String) {
self.key = v;
}
// Mutable pointer to the field.
// If field is not initialized, it is initialized with default value first.
pub fn mut_key(&mut self) -> &mut ::std::string::String {
&mut self.key
}
// Take field
pub fn take_key(&mut self) -> ::std::string::String {
::std::mem::replace(&mut self.key, ::std::string::String::new())
}
// string str_value = 2;
pub fn get_str_value(&self) -> &str {
match self.one_of_str_value {
::std::option::Option::Some(KeyValue_oneof_one_of_str_value::str_value(ref v)) => v,
_ => "",
}
}
pub fn clear_str_value(&mut self) {
self.one_of_str_value = ::std::option::Option::None;
}
pub fn has_str_value(&self) -> bool {
match self.one_of_str_value {
::std::option::Option::Some(KeyValue_oneof_one_of_str_value::str_value(..)) => true,
_ => false,
}
}
// Param is passed by value, moved
pub fn set_str_value(&mut self, v: ::std::string::String) {
self.one_of_str_value = ::std::option::Option::Some(KeyValue_oneof_one_of_str_value::str_value(v))
}
// Mutable pointer to the field.
pub fn mut_str_value(&mut self) -> &mut ::std::string::String {
if let ::std::option::Option::Some(KeyValue_oneof_one_of_str_value::str_value(_)) = self.one_of_str_value {
} else {
self.one_of_str_value = ::std::option::Option::Some(KeyValue_oneof_one_of_str_value::str_value(::std::string::String::new()));
}
match self.one_of_str_value {
::std::option::Option::Some(KeyValue_oneof_one_of_str_value::str_value(ref mut v)) => v,
_ => panic!(),
}
}
// Take field
pub fn take_str_value(&mut self) -> ::std::string::String {
if self.has_str_value() {
match self.one_of_str_value.take() {
::std::option::Option::Some(KeyValue_oneof_one_of_str_value::str_value(v)) => v,
_ => panic!(),
}
} else {
::std::string::String::new()
}
}
// int64 int_value = 3;
pub fn get_int_value(&self) -> i64 {
match self.one_of_int_value {
::std::option::Option::Some(KeyValue_oneof_one_of_int_value::int_value(v)) => v,
_ => 0,
}
}
pub fn clear_int_value(&mut self) {
self.one_of_int_value = ::std::option::Option::None;
}
pub fn has_int_value(&self) -> bool {
match self.one_of_int_value {
::std::option::Option::Some(KeyValue_oneof_one_of_int_value::int_value(..)) => true,
_ => false,
}
}
// Param is passed by value, moved
pub fn set_int_value(&mut self, v: i64) {
self.one_of_int_value = ::std::option::Option::Some(KeyValue_oneof_one_of_int_value::int_value(v))
}
// double float_value = 4;
pub fn get_float_value(&self) -> f64 {
match self.one_of_float_value {
::std::option::Option::Some(KeyValue_oneof_one_of_float_value::float_value(v)) => v,
_ => 0.,
}
}
pub fn clear_float_value(&mut self) {
self.one_of_float_value = ::std::option::Option::None;
}
pub fn has_float_value(&self) -> bool {
match self.one_of_float_value {
::std::option::Option::Some(KeyValue_oneof_one_of_float_value::float_value(..)) => true,
_ => false,
}
}
// Param is passed by value, moved
pub fn set_float_value(&mut self, v: f64) {
self.one_of_float_value = ::std::option::Option::Some(KeyValue_oneof_one_of_float_value::float_value(v))
}
// bool bool_value = 5;
pub fn get_bool_value(&self) -> bool {
match self.one_of_bool_value {
::std::option::Option::Some(KeyValue_oneof_one_of_bool_value::bool_value(v)) => v,
_ => false,
}
}
pub fn clear_bool_value(&mut self) {
self.one_of_bool_value = ::std::option::Option::None;
}
pub fn has_bool_value(&self) -> bool {
match self.one_of_bool_value {
::std::option::Option::Some(KeyValue_oneof_one_of_bool_value::bool_value(..)) => true,
_ => false,
}
}
// Param is passed by value, moved
pub fn set_bool_value(&mut self, v: bool) {
self.one_of_bool_value = ::std::option::Option::Some(KeyValue_oneof_one_of_bool_value::bool_value(v))
}
}
impl ::protobuf::Message for KeyValue {
fn is_initialized(&self) -> bool {
true
}
fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream<'_>) -> ::protobuf::ProtobufResult<()> {
while !is.eof()? {
let (field_number, wire_type) = is.read_tag_unpack()?;
match field_number {
1 => {
::protobuf::rt::read_singular_proto3_string_into(wire_type, is, &mut self.key)?;
},
2 => {
if wire_type != ::protobuf::wire_format::WireTypeLengthDelimited {
return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type));
}
self.one_of_str_value = ::std::option::Option::Some(KeyValue_oneof_one_of_str_value::str_value(is.read_string()?));
},
3 => {
if wire_type != ::protobuf::wire_format::WireTypeVarint {
return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type));
}
self.one_of_int_value = ::std::option::Option::Some(KeyValue_oneof_one_of_int_value::int_value(is.read_int64()?));
},
4 => {
if wire_type != ::protobuf::wire_format::WireTypeFixed64 {
return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type));
}
self.one_of_float_value = ::std::option::Option::Some(KeyValue_oneof_one_of_float_value::float_value(is.read_double()?));
},
5 => {
if wire_type != ::protobuf::wire_format::WireTypeVarint {
return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type));
}
self.one_of_bool_value = ::std::option::Option::Some(KeyValue_oneof_one_of_bool_value::bool_value(is.read_bool()?));
},
_ => {
::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
},
};
}
::std::result::Result::Ok(())
}
// Compute sizes of nested messages
#[allow(unused_variables)]
fn compute_size(&self) -> u32 {
let mut my_size = 0;
if !self.key.is_empty() {
my_size += ::protobuf::rt::string_size(1, &self.key);
}
if let ::std::option::Option::Some(ref v) = self.one_of_str_value {
match v {
&KeyValue_oneof_one_of_str_value::str_value(ref v) => {
my_size += ::protobuf::rt::string_size(2, &v);
},
};
}
if let ::std::option::Option::Some(ref v) = self.one_of_int_value {
match v {
&KeyValue_oneof_one_of_int_value::int_value(v) => {
my_size += ::protobuf::rt::value_size(3, v, ::protobuf::wire_format::WireTypeVarint);
},
};
}
if let ::std::option::Option::Some(ref v) = self.one_of_float_value {
match v {
&KeyValue_oneof_one_of_float_value::float_value(v) => {
my_size += 9;
},
};
}
if let ::std::option::Option::Some(ref v) = self.one_of_bool_value {
match v {
&KeyValue_oneof_one_of_bool_value::bool_value(v) => {
my_size += 2;
},
};
}
my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
self.cached_size.set(my_size);
my_size
}
fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream<'_>) -> ::protobuf::ProtobufResult<()> {
if !self.key.is_empty() {
os.write_string(1, &self.key)?;
}
if let ::std::option::Option::Some(ref v) = self.one_of_str_value {
match v {
&KeyValue_oneof_one_of_str_value::str_value(ref v) => {
os.write_string(2, v)?;
},
};
}
if let ::std::option::Option::Some(ref v) = self.one_of_int_value {
match v {
&KeyValue_oneof_one_of_int_value::int_value(v) => {
os.write_int64(3, v)?;
},
};
}
if let ::std::option::Option::Some(ref v) = self.one_of_float_value {
match v {
&KeyValue_oneof_one_of_float_value::float_value(v) => {
os.write_double(4, v)?;
},
};
}
if let ::std::option::Option::Some(ref v) = self.one_of_bool_value {
match v {
&KeyValue_oneof_one_of_bool_value::bool_value(v) => {
os.write_bool(5, v)?;
},
};
}
os.write_unknown_fields(self.get_unknown_fields())?;
::std::result::Result::Ok(())
}
fn get_cached_size(&self) -> u32 {
self.cached_size.get()
}
fn get_unknown_fields(&self) -> &::protobuf::UnknownFields {
&self.unknown_fields
}
fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields {
&mut self.unknown_fields
}
fn as_any(&self) -> &dyn (::std::any::Any) {
self as &dyn (::std::any::Any)
}
fn as_any_mut(&mut self) -> &mut dyn (::std::any::Any) {
self as &mut dyn (::std::any::Any)
}
fn into_any(self: ::std::boxed::Box<Self>) -> ::std::boxed::Box<dyn (::std::any::Any)> {
self
}
fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor {
Self::descriptor_static()
}
fn new() -> KeyValue {
KeyValue::new()
}
fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor {
static descriptor: ::protobuf::rt::LazyV2<::protobuf::reflect::MessageDescriptor> = ::protobuf::rt::LazyV2::INIT;
descriptor.get(|| {
let mut fields = ::std::vec::Vec::new();
fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
"key",
|m: &KeyValue| { &m.key },
|m: &mut KeyValue| { &mut m.key },
));
fields.push(::protobuf::reflect::accessor::make_singular_string_accessor::<_>(
"str_value",
KeyValue::has_str_value,
KeyValue::get_str_value,
));
fields.push(::protobuf::reflect::accessor::make_singular_i64_accessor::<_>(
"int_value",
KeyValue::has_int_value,
KeyValue::get_int_value,
));
fields.push(::protobuf::reflect::accessor::make_singular_f64_accessor::<_>(
"float_value",
KeyValue::has_float_value,
KeyValue::get_float_value,
));
fields.push(::protobuf::reflect::accessor::make_singular_bool_accessor::<_>(
"bool_value",
KeyValue::has_bool_value,
KeyValue::get_bool_value,
));
::protobuf::reflect::MessageDescriptor::new_pb_name::<KeyValue>(
"KeyValue",
fields,
file_descriptor_proto()
)
})
}
fn default_instance() -> &'static KeyValue {
static instance: ::protobuf::rt::LazyV2<KeyValue> = ::protobuf::rt::LazyV2::INIT;
instance.get(KeyValue::new)
}
}
impl ::protobuf::Clear for KeyValue {
fn clear(&mut self) {
self.key.clear();
self.one_of_str_value = ::std::option::Option::None;
self.one_of_int_value = ::std::option::Option::None;
self.one_of_float_value = ::std::option::Option::None;
self.one_of_bool_value = ::std::option::Option::None;
self.unknown_fields.clear();
}
}
impl ::std::fmt::Debug for KeyValue {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
::protobuf::text_format::fmt(self, f)
}
}
impl ::protobuf::reflect::ProtobufValue for KeyValue {
fn as_ref(&self) -> ::protobuf::reflect::ReflectValueRef {
::protobuf::reflect::ReflectValueRef::Message(self)
}
}
static file_descriptor_proto_data: &'static [u8] = b"\
\n\x08kv.proto\"\xf1\x01\n\x08KeyValue\x12\x10\n\x03key\x18\x01\x20\x01(\
\tR\x03key\x12\x1d\n\tstr_value\x18\x02\x20\x01(\tH\0R\x08strValue\x12\
\x1d\n\tint_value\x18\x03\x20\x01(\x03H\x01R\x08intValue\x12!\n\x0bfloat\
_value\x18\x04\x20\x01(\x01H\x02R\nfloatValue\x12\x1f\n\nbool_value\x18\
\x05\x20\x01(\x08H\x03R\tboolValueB\x12\n\x10one_of_str_valueB\x12\n\x10\
one_of_int_valueB\x14\n\x12one_of_float_valueB\x13\n\x11one_of_bool_valu\
eJ\xa9\x03\n\x06\x12\x04\0\0\x08\x01\n\x08\n\x01\x0c\x12\x03\0\0\x12\n\n\
\n\x02\x04\0\x12\x04\x02\0\x08\x01\n\n\n\x03\x04\0\x01\x12\x03\x02\x08\
\x10\n\x0b\n\x04\x04\0\x02\0\x12\x03\x03\x04\x13\n\x0c\n\x05\x04\0\x02\0\
\x05\x12\x03\x03\x04\n\n\x0c\n\x05\x04\0\x02\0\x01\x12\x03\x03\x0b\x0e\n\
\x0c\n\x05\x04\0\x02\0\x03\x12\x03\x03\x11\x12\n\x0b\n\x04\x04\0\x08\0\
\x12\x03\x04\x044\n\x0c\n\x05\x04\0\x08\0\x01\x12\x03\x04\n\x1a\n\x0b\n\
\x04\x04\0\x02\x01\x12\x03\x04\x1d2\n\x0c\n\x05\x04\0\x02\x01\x05\x12\
\x03\x04\x1d#\n\x0c\n\x05\x04\0\x02\x01\x01\x12\x03\x04$-\n\x0c\n\x05\
\x04\0\x02\x01\x03\x12\x03\x0401\n\x0b\n\x04\x04\0\x08\x01\x12\x03\x05\
\x043\n\x0c\n\x05\x04\0\x08\x01\x01\x12\x03\x05\n\x1a\n\x0b\n\x04\x04\0\
\x02\x02\x12\x03\x05\x1d1\n\x0c\n\x05\x04\0\x02\x02\x05\x12\x03\x05\x1d\
\"\n\x0c\n\x05\x04\0\x02\x02\x01\x12\x03\x05#,\n\x0c\n\x05\x04\0\x02\x02\
\x03\x12\x03\x05/0\n\x0b\n\x04\x04\0\x08\x02\x12\x03\x06\x048\n\x0c\n\
\x05\x04\0\x08\x02\x01\x12\x03\x06\n\x1c\n\x0b\n\x04\x04\0\x02\x03\x12\
\x03\x06\x1f6\n\x0c\n\x05\x04\0\x02\x03\x05\x12\x03\x06\x1f%\n\x0c\n\x05\
\x04\0\x02\x03\x01\x12\x03\x06&1\n\x0c\n\x05\x04\0\x02\x03\x03\x12\x03\
\x0645\n\x0b\n\x04\x04\0\x08\x03\x12\x03\x07\x044\n\x0c\n\x05\x04\0\x08\
\x03\x01\x12\x03\x07\n\x1b\n\x0b\n\x04\x04\0\x02\x04\x12\x03\x07\x1e2\n\
\x0c\n\x05\x04\0\x02\x04\x05\x12\x03\x07\x1e\"\n\x0c\n\x05\x04\0\x02\x04\
\x01\x12\x03\x07#-\n\x0c\n\x05\x04\0\x02\x04\x03\x12\x03\x0701b\x06proto\
3\
";
static file_descriptor_proto_lazy: ::protobuf::rt::LazyV2<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::rt::LazyV2::INIT;
fn parse_descriptor_proto() -> ::protobuf::descriptor::FileDescriptorProto {
::protobuf::Message::parse_from_bytes(file_descriptor_proto_data).unwrap()
}
pub fn file_descriptor_proto() -> &'static ::protobuf::descriptor::FileDescriptorProto {
file_descriptor_proto_lazy.get(|| {
parse_descriptor_proto()
})
}

View File

@ -0,0 +1,4 @@
// Auto-generated, do not edit
mod kv;
pub use kv::*;

View File

@ -0,0 +1,9 @@
syntax = "proto3";
message KeyValue {
string key = 1;
oneof one_of_str_value { string str_value = 2; };
oneof one_of_int_value { int64 int_value = 3; };
oneof one_of_float_value { double float_value = 4; };
oneof one_of_bool_value { bool bool_value = 5; };
}

View File

@ -0,0 +1,207 @@
use crate::retry::FixedInterval;
use pin_project::pin_project;
use std::{
future::Future,
iter::{IntoIterator, Iterator},
pin::Pin,
task::{Context, Poll},
};
use tokio::{
task::JoinHandle,
time::{sleep_until, Duration, Instant, Sleep},
};
#[pin_project(project = RetryStateProj)]
enum RetryState<A>
where
A: Action,
{
Running(#[pin] A::Future),
Sleeping(#[pin] Sleep),
}
impl<A: Action> RetryState<A> {
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> RetryFuturePoll<A> {
match self.project() {
RetryStateProj::Running(future) => RetryFuturePoll::Running(future.poll(cx)),
RetryStateProj::Sleeping(future) => RetryFuturePoll::Sleeping(future.poll(cx)),
}
}
}
enum RetryFuturePoll<A>
where
A: Action,
{
Running(Poll<Result<A::Item, A::Error>>),
Sleeping(Poll<()>),
}
/// Future that drives multiple attempts at an action via a retry strategy.
#[pin_project]
pub struct Retry<I, A>
where
I: Iterator<Item = Duration>,
A: Action,
{
#[pin]
retry_if: RetryIf<I, A, fn(&A::Error) -> bool>,
}
impl<I, A> Retry<I, A>
where
I: Iterator<Item = Duration>,
A: Action,
{
pub fn spawn<T: IntoIterator<IntoIter = I, Item = Duration>>(strategy: T, action: A) -> Retry<I, A> {
Retry {
retry_if: RetryIf::spawn(strategy, action, (|_| true) as fn(&A::Error) -> bool),
}
}
}
impl<I, A> Future for Retry<I, A>
where
I: Iterator<Item = Duration>,
A: Action,
{
type Output = Result<A::Item, A::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = self.project();
this.retry_if.poll(cx)
}
}
/// Future that drives multiple attempts at an action via a retry strategy.
/// Retries are only attempted if the `Error` returned by the future satisfies a
/// given condition.
#[pin_project]
pub struct RetryIf<I, A, C>
where
I: Iterator<Item = Duration>,
A: Action,
C: Condition<A::Error>,
{
strategy: I,
#[pin]
state: RetryState<A>,
action: A,
condition: C,
}
impl<I, A, C> RetryIf<I, A, C>
where
I: Iterator<Item = Duration>,
A: Action,
C: Condition<A::Error>,
{
pub fn spawn<T: IntoIterator<IntoIter = I, Item = Duration>>(
strategy: T,
mut action: A,
condition: C,
) -> RetryIf<I, A, C> {
RetryIf {
strategy: strategy.into_iter(),
state: RetryState::Running(action.run()),
action,
condition,
}
}
fn attempt(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<A::Item, A::Error>> {
let future = {
let this = self.as_mut().project();
this.action.run()
};
self.as_mut().project().state.set(RetryState::Running(future));
self.poll(cx)
}
fn retry(
mut self: Pin<&mut Self>,
err: A::Error,
cx: &mut Context,
) -> Result<Poll<Result<A::Item, A::Error>>, A::Error> {
match self.as_mut().project().strategy.next() {
None => Err(err),
Some(duration) => {
let deadline = Instant::now() + duration;
let future = sleep_until(deadline);
self.as_mut().project().state.set(RetryState::Sleeping(future));
Ok(self.poll(cx))
},
}
}
}
impl<I, A, C> Future for RetryIf<I, A, C>
where
I: Iterator<Item = Duration>,
A: Action,
C: Condition<A::Error>,
{
type Output = Result<A::Item, A::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
match self.as_mut().project().state.poll(cx) {
RetryFuturePoll::Running(poll_result) => match poll_result {
Poll::Ready(Ok(ok)) => Poll::Ready(Ok(ok)),
Poll::Pending => Poll::Pending,
Poll::Ready(Err(err)) => {
if self.as_mut().project().condition.should_retry(&err) {
match self.retry(err, cx) {
Ok(poll) => poll,
Err(err) => Poll::Ready(Err(err)),
}
} else {
Poll::Ready(Err(err))
}
},
},
RetryFuturePoll::Sleeping(poll_result) => match poll_result {
Poll::Pending => Poll::Pending,
Poll::Ready(_) => self.attempt(cx),
},
}
}
}
/// An action can be run multiple times and produces a future.
pub trait Action: Send + Sync {
type Future: Future<Output = Result<Self::Item, Self::Error>>;
type Item;
type Error;
fn run(&mut self) -> Self::Future;
}
// impl<R, E, T: Future<Output = Result<R, E>>, F: FnMut() -> T + Send + Sync>
// Action for F { type Future = T;
// type Item = R;
// type Error = E;
//
// fn run(&mut self) -> Self::Future { self() }
// }
pub trait Condition<E> {
fn should_retry(&mut self, error: &E) -> bool;
}
impl<E, F: FnMut(&E) -> bool> Condition<E> for F {
fn should_retry(&mut self, error: &E) -> bool { self(error) }
}
pub fn spawn_retry<A: Action + 'static>(
millis: u64,
retry_count: usize,
action: A,
) -> JoinHandle<Result<A::Item, A::Error>>
where
A::Item: Send + Sync,
A::Error: Send + Sync,
<A as Action>::Future: Send + Sync,
{
let strategy = FixedInterval::from_millis(millis).take(retry_count);
let retry = Retry::spawn(strategy, action);
tokio::spawn(async move { retry.await })
}

View File

@ -0,0 +1,5 @@
mod future;
mod strategy;
pub use future::*;
pub use strategy::*;

View File

@ -0,0 +1,127 @@
use std::{iter::Iterator, time::Duration};
/// A retry strategy driven by exponential back-off.
///
/// The power corresponds to the number of past attempts.
#[derive(Debug, Clone)]
pub struct ExponentialBackoff {
current: u64,
base: u64,
factor: u64,
max_delay: Option<Duration>,
}
impl ExponentialBackoff {
/// Constructs a new exponential back-off strategy,
/// given a base duration in milliseconds.
///
/// The resulting duration is calculated by taking the base to the `n`-th
/// power, where `n` denotes the number of past attempts.
pub fn from_millis(base: u64) -> ExponentialBackoff {
ExponentialBackoff {
current: base,
base,
factor: 1u64,
max_delay: None,
}
}
/// A multiplicative factor that will be applied to the retry delay.
///
/// For example, using a factor of `1000` will make each delay in units of
/// seconds.
///
/// Default factor is `1`.
pub fn factor(mut self, factor: u64) -> ExponentialBackoff {
self.factor = factor;
self
}
/// Apply a maximum delay. No retry delay will be longer than this
/// `Duration`.
pub fn max_delay(mut self, duration: Duration) -> ExponentialBackoff {
self.max_delay = Some(duration);
self
}
}
impl Iterator for ExponentialBackoff {
type Item = Duration;
fn next(&mut self) -> Option<Duration> {
// set delay duration by applying factor
let duration = if let Some(duration) = self.current.checked_mul(self.factor) {
Duration::from_millis(duration)
} else {
Duration::from_millis(u64::MAX)
};
// check if we reached max delay
if let Some(ref max_delay) = self.max_delay {
if duration > *max_delay {
return Some(*max_delay);
}
}
if let Some(next) = self.current.checked_mul(self.base) {
self.current = next;
} else {
self.current = u64::MAX;
}
Some(duration)
}
}
#[test]
fn returns_some_exponential_base_10() {
let mut s = ExponentialBackoff::from_millis(10);
assert_eq!(s.next(), Some(Duration::from_millis(10)));
assert_eq!(s.next(), Some(Duration::from_millis(100)));
assert_eq!(s.next(), Some(Duration::from_millis(1000)));
}
#[test]
fn returns_some_exponential_base_2() {
let mut s = ExponentialBackoff::from_millis(2);
assert_eq!(s.next(), Some(Duration::from_millis(2)));
assert_eq!(s.next(), Some(Duration::from_millis(4)));
assert_eq!(s.next(), Some(Duration::from_millis(8)));
}
#[test]
fn saturates_at_maximum_value() {
let mut s = ExponentialBackoff::from_millis(u64::MAX - 1);
assert_eq!(s.next(), Some(Duration::from_millis(u64::MAX - 1)));
assert_eq!(s.next(), Some(Duration::from_millis(u64::MAX)));
assert_eq!(s.next(), Some(Duration::from_millis(u64::MAX)));
}
#[test]
fn can_use_factor_to_get_seconds() {
let factor = 1000;
let mut s = ExponentialBackoff::from_millis(2).factor(factor);
assert_eq!(s.next(), Some(Duration::from_secs(2)));
assert_eq!(s.next(), Some(Duration::from_secs(4)));
assert_eq!(s.next(), Some(Duration::from_secs(8)));
}
#[test]
fn stops_increasing_at_max_delay() {
let mut s = ExponentialBackoff::from_millis(2).max_delay(Duration::from_millis(4));
assert_eq!(s.next(), Some(Duration::from_millis(2)));
assert_eq!(s.next(), Some(Duration::from_millis(4)));
assert_eq!(s.next(), Some(Duration::from_millis(4)));
}
#[test]
fn returns_max_when_max_less_than_base() {
let mut s = ExponentialBackoff::from_millis(20).max_delay(Duration::from_millis(10));
assert_eq!(s.next(), Some(Duration::from_millis(10)));
assert_eq!(s.next(), Some(Duration::from_millis(10)));
}

View File

@ -0,0 +1,35 @@
use std::{iter::Iterator, time::Duration};
/// A retry strategy driven by a fixed interval.
#[derive(Debug, Clone)]
pub struct FixedInterval {
duration: Duration,
}
impl FixedInterval {
/// Constructs a new fixed interval strategy.
pub fn new(duration: Duration) -> FixedInterval { FixedInterval { duration } }
/// Constructs a new fixed interval strategy,
/// given a duration in milliseconds.
pub fn from_millis(millis: u64) -> FixedInterval {
FixedInterval {
duration: Duration::from_millis(millis),
}
}
}
impl Iterator for FixedInterval {
type Item = Duration;
fn next(&mut self) -> Option<Duration> { Some(self.duration) }
}
#[test]
fn returns_some_fixed() {
let mut s = FixedInterval::new(Duration::from_millis(123));
assert_eq!(s.next(), Some(Duration::from_millis(123)));
assert_eq!(s.next(), Some(Duration::from_millis(123)));
assert_eq!(s.next(), Some(Duration::from_millis(123)));
}

View File

@ -0,0 +1,5 @@
use std::time::Duration;
pub fn jitter(duration: Duration) -> Duration {
duration.mul_f64(rand::random::<f64>())
}

View File

@ -0,0 +1,7 @@
mod exponential_backoff;
mod fixed_interval;
mod jitter;
pub use exponential_backoff::*;
pub use fixed_interval::*;
pub use jitter::*;