8000 [Suggestion] Add taskified variants of timeout combinators by tkroman · Pull Request #838 · monix/monix · GitHub
[go: up one dir, main page]

Skip to content

[Suggestion] Add taskified variants of timeout combinators - #838

Merged
oleg-py merged 7 commits into
monix:masterfrom
tkroman:taskified-timeouts
Apr 1, 2019
Merged

oleg-py merged 7 commits into
monix:masterfrom
tkroman:taskified-timeouts

Conversation

@tkroman
@tkroman tkroman commented Mar 6, 2019
Copy link
Copy Markdown
Contributor

I propose adding timeoutL/timeoutLTo variants of timeout/timeoutTo. Motivating examples are in the code. If you think this deserves to be in the library, I'll add tests etc.

@oleg-py
oleg-py requested a review from alexandru March 8, 2019 10:52
@oleg-py
oleg-py commented Mar 8, 2019
Copy link
Copy Markdown
Collaborator

I'm 👍 on adding these, given we have Ref and TaskLocal stuff, these can be useful.

@tkroman
tkroman commented Mar 8, 2019 •
Copy link
Copy Markdown
Contributor Author

Cool. Any suggestions on names? I admit that timeoutLTo is far from being nicely discoverable except by signature.

@tkroman
tkroman commented Mar 8, 2019
Copy link
Copy Markdown
Contributor Author

Another question - since timeout is a task now, should I try to recover somehow? If a timeout task fails, then the whole task fails. Not sure if this is the desired behaviour.

@oleg-py
oleg-py commented Mar 9, 2019 •
Copy link
Copy Markdown
Collaborator

As far as naming goes, I'd suggest keeping L as strictly a suffix. I.e. timeoutToL.

The issue I'm currently seeing is that currently you're waiting for a duration to compute, and then also for that duration. Maybe you should subtract the time it took to compute the duration from actual delay?

I don't think sensible recovery is possible. You can't just ignore the delay, as that might cause resource leak. You can return the result if it managed to compute before the delay failed, but I think that should be already done by race (although a test wouldn't hurt).

@tkroman
tkroman commented Mar 9, 2019
Copy link
Copy Markdown
Contributor Author

Good catch on the evaluation time. I changed the code to account for it but now is seems like some users would want to actually ignore that time. Should I put it under a flag?

def timeoutL(timeout: Task[Duration], respectTimeoutTimings: Boolean = true) = ???

*
* Example:
* {{{
* import cats.effect.IO
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reduntant import I think

@Avasil Avasil left a comment
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @tkroman :)
I've left few minor docs/formatting related comments but it looks good to me overall

* to a value of `5 seconds`, then,
* if the value of this parameter is `true`,
* this task will timeout in exactly 5 seconds
* from the momemnt computation started;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* from the momemnt computation started;
* from the moment computation started;

*
* Example:
* {{{
* import cats.effect.IO
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* import cats.effect.IO

* this will timeout in 5 seconds _after_
* the evaluation of timeout task was completed.
**/
final def timeoutToL[B >: A](after: Task[FiniteDuration],
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please change formatting like this:

final def timeoutToL[B >: A](
  after: Task[FiniteDuration],
  backup: Task[B],
  respectTimeoutTimings: Boolean = true): Task[B] = 

}

test("Task#timeoutL considers time taken to evaluate the duration task") { implicit s =>
// "3 seconds" after delay of 2s
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the comments

@Avasil
Avasil commented Mar 24, 2019
Copy link
Copy Markdown
Collaborator

Good catch on the evaluation time. I changed the code to account for it but now is seems like some users would want to actually ignore that time. Should I put it under a flag?

def timeoutL(timeout: Task[Duration], respectTimeoutTimings: Boolean = true) = ???

In other places we use different methods and call it fixedRate and fixedDelay but I think in this case flag is reasonable. WDYT @oleg-py ?

@oleg-py
oleg-py commented Mar 24, 2019
Copy link
Copy Markdown
Collaborator

We have task.timed. If we have one of these versions, the other one is always constructible via timed + flatMap + either subtraction or addition, depending on variant we have.

So, I think having one version is good enough (and I'm not a huge fan of boolean flags tbh). We don't need to address every use case with one method, if we have enough reasonably convenient composable pieces to let user solve the problem.

Which one should be default is the next question. I have a vague feeling that subtracting elapsed time would be less surprising, but I'm not insisting on it.

tkroman commented Mar 29, 2019
Copy link
Copy Markdown
Contributor Author

@Avasil @oleg-py addressed comments & merged. I also ran scalafmt on Task.scala which lead to a larger diff, should I revert?

@Avasil
Avasil commented Mar 29, 2019
Copy link
Copy Markdown
Collaborator

@tkroman Revert scalafmt please, I think we were undecided whether to use it

@tkroman
tkroman commented Mar 29, 2019
Copy link
Copy Markdown
Contributor Author

Done. Feel free to merge :)

@oleg-py
oleg-py merged commit 2243ea7 into monix:master Apr 1, 2019
mdedetrich pushed a commit to mdedetrich/monix that referenced this pull request Mar 28, 2020
* Add taskified variants of timeout combinators

* add tests

* account for timeout evaluation time

* add scaladoc

* Address PR comments

* Update Task.scala
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0