juan_gandhi: (VP)
[personal profile] juan_gandhi
Guys, I do appreciate your opinions a lot.

  val patience = 10

  case class UploadProgress(uploaded: Set[File] = Set.empty,
                            failed:   Set[Result[File]] = Set.empty,
                            badLuckStreak: Int = 0) {
    def errors = Result.traverse(failed)
    def +(file:File) = {
      if (badLuckStreak > patience) this
      else {
        uploadOneFile(file) match {
          case Good(file) => UploadProgress(uploaded + file, failed, 0)
          case bad => UploadProgress(uploaded, failed + bad, badLuckStreak + 1)
        }
      }
    }
  }

  def uploadScheduledFiles():UploadProgress = {
    if (!client.isAlive) UploadProgress(Set.empty, Set(Result.error("Upload server is dead, sorry")), 0)
    else {
      (UploadProgress() /: listFilesForUpload)(_ + _)
    }
  }


What happens here: we upload until we are out of patience with a streak of bad luck, probably meaning the server is dead.

Date: 2014-05-17 04:03 am (UTC)
From: [identity profile] thedeemon.livejournal.com
>Guys, I do appreciate our opinions a lot.

We appreciate ours too. ;)

Date: 2014-05-17 04:57 am (UTC)
From: [identity profile] jdevelop.livejournal.com
I would create a set of futures and then waited upon completion of all of them, with certain timeout

if not using the Akka actors.

Date: 2014-05-17 08:08 am (UTC)
From: [identity profile] shabunc.livejournal.com
Я на всякий случай.

Вот про такое место вы знаете?
http://codereview.stackexchange.com

Мне интересно читать такие посты, не подумайте, просто там заведомо шире аудитория.

Date: 2014-05-18 12:11 am (UTC)
From: [identity profile] mstone.livejournal.com
+1 for futures or any other machinery for asynchronous programming in your language/platform.

If there are N files to upload, start N asynchronous upload tasks; the continuation for each task should check the result and update the corresponding item in the collection of N results. After all the N tasks are started, call asynchronous wait_all/when_all for the collection of the started tasks.

Bonus point if the platform's library provides an asynchronous implementation of HTTP POST: it's naturally IO-bound so shouldn't block any threads. Double-bonus if it supports cooperative cancellation.

This pattern is first-class citizen in C#/.NET these days with full library support of asynchronous I/O functions and compiler support for automatic code rewriting into continuation-passing style for asynchrony. There should be something for Scala too (first link of https://www.google.com/search?q=async+await+scala seems relevant).

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

July 2025

S M T W T F S
  12345
6789 1011 12
131415 1617 1819
20212223242526
2728293031  

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 22nd, 2025 01:42 pm
Powered by Dreamwidth Studios