Skip to content

Commit

Permalink
tiny refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chanderlud committed Nov 30, 2023
1 parent 1a1f943 commit b3fabf1
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/sender/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ mod reader;
type JobQueue = UnlimitedQueue<Job>;
type JobCache = Arc<RwLock<BTreeMap<u64, Job>>>;

#[derive(Clone)]
pub(crate) struct Job {
struct Job {
data: Vec<u8>, // index (8 bytes) + the file chunk
index: u64, // the index of the file chunk
cached_at: Option<Instant>,
Expand Down Expand Up @@ -123,21 +122,15 @@ pub(crate) async fn main(options: Options, stats: TransferStats) -> Result<()> {
async fn sender(queue: JobQueue, socket: UdpSocket, cache: JobCache, send: Arc<Semaphore>) {
let mut retries = 0;

loop {
while retries < MAX_RETRIES {
let permit = send.acquire().await.unwrap(); // acquire a permit
let mut job = queue.pop().await; // get the next job

// send the job data to the socket
if let Err(error) = socket.send(&job.data).await {
error!("failed to send data: {}", error);
queue.push(job); // put the job back in the queue

if retries < MAX_RETRIES {
retries += 1;
} else {
error!("sender: too many retries, exiting");
break;
}
retries += 1;
} else {
// cache the job
job.cached_at = Some(Instant::now());
Expand Down

0 comments on commit b3fabf1

Please sign in to comment.