hyperion_framework/utilities/tx_sender.rs
1// -------------------------------------------------------------------------------------------------
2// Hyperion Framework
3// https://github.com/robert-hannah/hyperion-framework
4//
5// A lightweight component-based TCP framework for building service-oriented Rust applications with
6// CLI control, async messaging, and lifecycle management.
7//
8// Copyright 2025 Robert Hannah
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14// http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21// -------------------------------------------------------------------------------------------------
22
23// Package
24use tokio::sync::mpsc;
25use tokio::time::{Duration, sleep};
26
27pub async fn add_to_tx_with_retry<T>(
28 tx: &mpsc::Sender<T>,
29 message: &T,
30 from_location: &str,
31 to_location: &str,
32) where
33 T: Clone + Send + Sync,
34{
35 let mut attempts = 0;
36 let max_attempts = 5;
37
38 loop {
39 match tx.try_send(message.clone()) {
40 Ok(_) => {
41 // log::debug!("Message forwarded to main successfully.");
42 break;
43 }
44 Err(mpsc::error::TrySendError::Full(_)) => {
45 attempts += 1;
46 if attempts >= max_attempts {
47 log::error!(
48 "Failed to add message to TX channel from {from_location} to {to_location} after {max_attempts} attempts"
49 );
50 break;
51 }
52 // Exponential backoff before retrying
53 sleep(Duration::from_millis(100 * 2u64.pow(attempts))).await;
54 }
55 Err(mpsc::error::TrySendError::Closed(_)) => {
56 log::error!(
57 "TX channel is closed. Cannot send message from {from_location} to {to_location}"
58 );
59 break;
60 }
61 }
62 }
63}