Skip to content

Commit

Permalink
Handle sporadic tonic transport errors (#179)
Browse files Browse the repository at this point in the history
* Handle Tonic transport error with unknown status

* Clippy fixes

* Clippy fixes
  • Loading branch information
abdolence authored May 30, 2024
1 parent f1ce69f commit 3e44659
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 101 deletions.
8 changes: 4 additions & 4 deletions src/cache/backends/persistent_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl FirestorePersistentCacheBackend {
fn write_document(&self, doc: &Document) -> FirestoreResult<()> {
let (collection_path, document_id) = split_document_path(&doc.name);

if self.config.collections.get(collection_path).is_some() {
if self.config.collections.contains_key(collection_path) {
let td: TableDefinition<&str, &[u8]> = TableDefinition::new(collection_path);

let write_txn = self.redb.begin_write()?;
Expand Down Expand Up @@ -338,7 +338,7 @@ impl FirestoreCacheDocsByPathSupport for FirestorePersistentCacheBackend {
document_path: &str,
) -> FirestoreResult<Option<FirestoreDocument>> {
let (collection_path, document_id) = split_document_path(document_path);
if self.config.collections.get(collection_path).is_some() {
if self.config.collections.contains_key(collection_path) {
let td: TableDefinition<&str, &[u8]> = TableDefinition::new(collection_path);
let read_tx = self.redb.begin_read()?;
let table = read_tx.open_table(td)?;
Expand All @@ -359,7 +359,7 @@ impl FirestoreCacheDocsByPathSupport for FirestorePersistentCacheBackend {
collection_path: &str,
) -> FirestoreResult<FirestoreCachedValue<BoxStream<'b, FirestoreResult<FirestoreDocument>>>>
{
if self.config.collections.get(collection_path).is_some() {
if self.config.collections.contains_key(collection_path) {
let td: TableDefinition<&str, &[u8]> = TableDefinition::new(collection_path);

let read_tx = self.redb.begin_read()?;
Expand Down Expand Up @@ -388,7 +388,7 @@ impl FirestoreCacheDocsByPathSupport for FirestorePersistentCacheBackend {
query: &FirestoreQueryParams,
) -> FirestoreResult<FirestoreCachedValue<BoxStream<'b, FirestoreResult<FirestoreDocument>>>>
{
if self.config.collections.get(collection_path).is_some() {
if self.config.collections.contains_key(collection_path) {
// For now only basic/simple query all supported
let simple_query_engine = FirestoreCacheQueryEngine::new(query);
if simple_query_engine.params_supported() {
Expand Down
2 changes: 2 additions & 0 deletions src/db/query_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum FirestoreQueryCollection {
Group(Vec<String>),
}

#[allow(clippy::to_string_trait_impl)]
impl ToString for FirestoreQueryCollection {
fn to_string(&self) -> String {
match self {
Expand Down Expand Up @@ -312,6 +313,7 @@ pub enum FirestoreQueryDirection {
Descending,
}

#[allow(clippy::to_string_trait_impl)]
impl ToString for FirestoreQueryDirection {
fn to_string(&self) -> String {
match self {
Expand Down
11 changes: 10 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ fn check_hyper_errors(status: gcloud_sdk::tonic::Status) -> FirestoreError {
format!("Hyper error: {err}"),
false,
)),
_ if status.code() == gcloud_sdk::tonic::Code::Unknown
&& status.message().contains("transport error") =>
{
FirestoreError::DatabaseError(FirestoreDatabaseError::new(
FirestoreErrorPublicGenericDetails::new("CONNECTION_ERROR".into()),
format!("{status}"),
true,
))
}
_ => FirestoreError::DatabaseError(FirestoreDatabaseError::new(
FirestoreErrorPublicGenericDetails::new(format!("{:?}", status.code())),
format!("{status}"),
Expand All @@ -260,7 +269,7 @@ fn check_hyper_errors(status: gcloud_sdk::tonic::Status) -> FirestoreError {
},
_ => FirestoreError::DatabaseError(FirestoreDatabaseError::new(
FirestoreErrorPublicGenericDetails::new(format!("{:?}", status.code())),
format!("{status}"),
format!("{status} without root cause"),
false,
)),
}
Expand Down
28 changes: 8 additions & 20 deletions src/firestore_serde/latlng_serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ pub fn serialize_latlng_for_firestore<T: ?Sized + Serialize>(
type Ok = FirestoreValue;
type Error = FirestoreError;

fn serialize_field<T: ?Sized>(
fn serialize_field<T: ?Sized + Serialize>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Self::Error>
where
T: Serialize,
{
) -> Result<(), Self::Error> {
let serializer = FirestoreValueSerializer {
none_as_null: false,
};
Expand All @@ -63,7 +60,7 @@ pub fn serialize_latlng_for_firestore<T: ?Sized + Serialize>(
FirestoreSerializationError::from_message(
"LatLng serializer doesn't recognize the structure of the object",
),
))
));
}
};

Expand Down Expand Up @@ -209,10 +206,7 @@ pub fn serialize_latlng_for_firestore<T: ?Sized + Serialize>(
))
}

fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> Result<Self::Ok, Self::Error> {
value.serialize(self)
}

Expand All @@ -235,27 +229,21 @@ pub fn serialize_latlng_for_firestore<T: ?Sized + Serialize>(
self.serialize_str(variant)
}

fn serialize_newtype_struct<T: ?Sized>(
fn serialize_newtype_struct<T: ?Sized + Serialize>(
self,
_name: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
) -> Result<Self::Ok, Self::Error> {
value.serialize(self)
}

fn serialize_newtype_variant<T: ?Sized>(
fn serialize_newtype_variant<T: ?Sized + Serialize>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"LatLng serializer doesn't support this type",
Expand Down
19 changes: 5 additions & 14 deletions src/firestore_serde/reference_serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,7 @@ pub fn serialize_reference_for_firestore<T: ?Sized + Serialize>(
}
}

fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> Result<Self::Ok, Self::Error> {
value.serialize(self)
}

Expand All @@ -233,27 +230,21 @@ pub fn serialize_reference_for_firestore<T: ?Sized + Serialize>(
self.serialize_str(variant)
}

fn serialize_newtype_struct<T: ?Sized>(
fn serialize_newtype_struct<T: ?Sized + Serialize>(
self,
_name: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
) -> Result<Self::Ok, Self::Error> {
value.serialize(self)
}

fn serialize_newtype_variant<T: ?Sized>(
fn serialize_newtype_variant<T: ?Sized + Serialize>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Reference serializer doesn't support this type",
Expand Down
63 changes: 15 additions & 48 deletions src/firestore_serde/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,7 @@ impl serde::Serializer for FirestoreValueSerializer {
}
}

fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> Result<Self::Ok, Self::Error> {
value.serialize(self)
}

Expand All @@ -202,14 +199,11 @@ impl serde::Serializer for FirestoreValueSerializer {
self.serialize_str(variant)
}

fn serialize_newtype_struct<T: ?Sized>(
fn serialize_newtype_struct<T: ?Sized + Serialize>(
self,
name: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
) -> Result<Self::Ok, Self::Error> {
match name {
crate::firestore_serde::timestamp_serializers::FIRESTORE_TS_TYPE_TAG_TYPE => {
crate::firestore_serde::timestamp_serializers::serialize_timestamp_for_firestore(
Expand Down Expand Up @@ -241,16 +235,13 @@ impl serde::Serializer for FirestoreValueSerializer {
}
}

fn serialize_newtype_variant<T: ?Sized>(
fn serialize_newtype_variant<T: ?Sized + Serialize>(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
) -> Result<Self::Ok, Self::Error> {
let mut fields = HashMap::new();
fields.insert(String::from(variant), value.serialize(self)?.value);
Ok(FirestoreValue::from(
Expand Down Expand Up @@ -330,10 +321,7 @@ impl serde::ser::SerializeSeq for SerializeVec {
type Ok = FirestoreValue;
type Error = FirestoreError;

fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: Serialize,
{
fn serialize_element<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
let serialized_value = value
.serialize(FirestoreValueSerializer {
none_as_null: self.none_as_null,
Expand All @@ -360,10 +348,7 @@ impl serde::ser::SerializeTuple for SerializeVec {
type Ok = FirestoreValue;
type Error = FirestoreError;

fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: Serialize,
{
fn serialize_element<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
serde::ser::SerializeSeq::serialize_element(self, value)
}

Expand All @@ -376,10 +361,7 @@ impl serde::ser::SerializeTupleStruct for SerializeVec {
type Ok = FirestoreValue;
type Error = FirestoreError;

fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: Serialize,
{
fn serialize_field<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
serde::ser::SerializeSeq::serialize_element(self, value)
}

Expand All @@ -392,10 +374,7 @@ impl serde::ser::SerializeTupleVariant for SerializeTupleVariant {
type Ok = FirestoreValue;
type Error = FirestoreError;

fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: Serialize,
{
fn serialize_field<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
let serialized_value = value
.serialize(FirestoreValueSerializer {
none_as_null: self.none_as_null,
Expand Down Expand Up @@ -432,10 +411,7 @@ impl serde::ser::SerializeMap for SerializeMap {
type Ok = FirestoreValue;
type Error = FirestoreError;

fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
where
T: Serialize,
{
fn serialize_key<T: ?Sized + Serialize>(&mut self, key: &T) -> Result<(), Self::Error> {
let serializer = FirestoreValueSerializer {
none_as_null: self.none_as_null,
};
Expand All @@ -454,10 +430,7 @@ impl serde::ser::SerializeMap for SerializeMap {
}
}

fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
where
T: Serialize,
{
fn serialize_value<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
match self.next_key.take() {
Some(key) => {
let serializer = FirestoreValueSerializer {
Expand Down Expand Up @@ -492,14 +465,11 @@ impl serde::ser::SerializeStruct for SerializeMap {
type Ok = FirestoreValue;
type Error = FirestoreError;

fn serialize_field<T: ?Sized>(
fn serialize_field<T: ?Sized + Serialize>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Self::Error>
where
T: Serialize,
{
) -> Result<(), Self::Error> {
let serializer = FirestoreValueSerializer {
none_as_null: self.none_as_null,
};
Expand Down Expand Up @@ -527,14 +497,11 @@ impl serde::ser::SerializeStructVariant for SerializeStructVariant {
type Ok = FirestoreValue;
type Error = FirestoreError;

fn serialize_field<T: ?Sized>(
fn serialize_field<T: ?Sized + Serialize>(
&mut self,
key: &'static str,
value: &T,
) -> Result<(), Self::Error>
where
T: Serialize,
{
) -> Result<(), Self::Error> {
let serializer = FirestoreValueSerializer {
none_as_null: self.none_as_null,
};
Expand Down
19 changes: 5 additions & 14 deletions src/firestore_serde/timestamp_serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,7 @@ pub fn serialize_timestamp_for_firestore<T: ?Sized + Serialize>(
}
}

fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> Result<Self::Ok, Self::Error> {
value.serialize(self)
}

Expand All @@ -257,27 +254,21 @@ pub fn serialize_timestamp_for_firestore<T: ?Sized + Serialize>(
self.serialize_str(variant)
}

fn serialize_newtype_struct<T: ?Sized>(
fn serialize_newtype_struct<T: ?Sized + Serialize>(
self,
_name: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
) -> Result<Self::Ok, Self::Error> {
value.serialize(self)
}

fn serialize_newtype_variant<T: ?Sized>(
fn serialize_newtype_variant<T: ?Sized + Serialize>(
self,
_name: &'static str,
_variant_index: u32,
_variant: &'static str,
_value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
) -> Result<Self::Ok, Self::Error> {
Err(FirestoreError::SerializeError(
FirestoreSerializationError::from_message(
"Timestamp serializer doesn't support this type",
Expand Down

0 comments on commit 3e44659

Please sign in to comment.