Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cancel All My Appointments!

Cancel All My Appointments!

(Video: https://www.youtube.com/watch?v=VDaKLQE03ss)

The most complicated state in an application is time. Like cooking a bag of microwave popcorn, modern web applications desperately try to keep up with their state through an orchestrated chaos of asynchronous operations popping off in indeterminate cadence. No question, coordinating all this concurrency is hard.

But there's a pitfall at the heart of asynchrony that stays mostly unaddressed.

When an operation doesn't finish right away, synchronously, it's possible the operation may never finish. Or, it may be destined to finish eventually, but a second operation may be raised which means we no longer care about the first. Whatever the case, we need to be able to cancel it. This critical check ultimately protects users from the vagaries of unpredictable systems.

Cancellation should be core to our async programming; no asynchrony should run without it. Too often, such handling is just a corner case exception. Without a cancellation strategy, your applications are incomplete at best.

We'll discuss use-cases for async cancellation and various approaches to managing them.

Kyle Simpson

December 07, 2018
Tweet

More Decks by Kyle Simpson

Other Decks in Programming

Transcript

  1. Cancel All My Appointments!
    Kyle Simpson @getify

    View full-size slide

  2. HAPPENS
    Failure
    @getify

    View full-size slide

  3. HAPPENS
    Cancellation
    @getify

    View full-size slide

  4. Cancel
    Abort
    Call-Off
    Abandon
    Stop
    Kill
    Revoke
    Quit
    Pause?
    Terminate
    Undo?
    @getify

    View full-size slide

  5. Cancel
    Abort
    Call-Off
    Abandon
    Stop
    Kill
    Revoke
    Quit
    Pause?
    Terminate
    Undo?
    Final
    } {
    Recover

    Rollback
    Event?
    State
    @getify

    View full-size slide

  6. Events are unexpected. States are expected.
    Cancellation should never be an
    unexpected exception.
    Cancellation should be a predictable state.
    @getify

    View full-size slide

  7. ¯\_(ϑ)_/¯

    UNCERTAINTY
    @getify

    View full-size slide

  8. ing
    Problem
    @getify

    View full-size slide

  9. Approximate!
    @getify

    View full-size slide

  10. Timeouts
    @getify

    View full-size slide

  11. Schrödinger's Cat
    Quantum Superposition
    @getify

    View full-size slide

  12. Schrödinger's Asynchrony
    @getify

    View full-size slide

  13. Cancel...a Cancellation?
    @getify

    View full-size slide

  14. Cancelable Promise?
    @getify

    View full-size slide

  15. Wrong Level of Abstraction
    @getify

    View full-size slide

  16. Schrödinger's Cancellation
    @getify

    View full-size slide

  17. Cancelable Promise:
    "Action At A Distance"
    Unpredictable State
    Just Say No.
    @getify

    View full-size slide

  18. Complexity Randomness
    ~
    ~
    @getify

    View full-size slide

  19. UNCERTAINTY
    @getify

    View full-size slide

  20. Cancel what?
    @getify

    View full-size slide

  21. I FIGHT FOR THE

    USERS . . .
    . . . AND THEY WANT
    CANCELLATION!
    @getify

    View full-size slide

  22. Cancellation Must Be

    A First Class Citizen
    @getify

    View full-size slide

  23. Developers,
    Developers,
    Developers?
    @getify

    View full-size slide

  24. Timers
    @getify

    View full-size slide

  25. Ajax Calls
    @getify

    View full-size slide

  26. User Events
    @getify

    View full-size slide

  27. What about...?
    @getify

    View full-size slide

  28. File System Operations
    @getify

    View full-size slide

  29. Stream Operations
    @getify

    View full-size slide

  30. Network I/O
    @getify

    View full-size slide

  31. Database Queries
    @getify

    View full-size slide

  32. Child Processes
    @getify

    View full-size slide

  33. Yadda, Yadda, Yadda...
    @getify

    View full-size slide

  34. Debounce: Spinner
    @getify

    View full-size slide

  35. Debounce: Spinner
    @getify

    View full-size slide

  36. Un-observable-ation
    Implied Cancellation
    @getify

    View full-size slide

  37. Starvation
    @getify

    View full-size slide

  38. https://vorpus.org/blog/timeouts-and-
    cancellation-for-humans/
    @getify

    View full-size slide

  39. Edge-Triggered vs Level-Triggered
    Event vs State
    @getify

    View full-size slide

  40. Events can be missed.
    State isn't.
    @getify

    View full-size slide

  41. Event composition can be complex.
    State transition isn't.
    @getify

    View full-size slide

  42. Timeouts vs Deadlines
    @getify

    View full-size slide

  43. Timeouts vs Deadlines
    @getify

    View full-size slide

  44. Cancellation:

    Serial vs Concurrent
    @getify

    View full-size slide

  45. ...oops, only Shallow Cancellation
    @getify

    View full-size slide

  46. Deep(er) Cancellation
    @getify

    View full-size slide

  47. Race: A xor B
    @getify

    View full-size slide

  48. Promises are a bit awkward here
    @getify

    View full-size slide

  49. Cancellation Tokens
    @getify

    View full-size slide

  50. cancelable fetch!
    @getify

    View full-size slide

  51. Async Functions:

    not (externally) cancelable
    @getify

    View full-size slide

  52. CAF
    (Cancelable Async Flows)
    https://github.com/getify/CAF
    @getify

    View full-size slide

  53. still cancelable fetch!
    @getify

    View full-size slide

  54. Tedious?
    @getify

    View full-size slide

  55. Idea*:

    Cancellation Zones
    (*still very rough idea)
    @getify

    View full-size slide

  56. What's missing: atomicity
    Not just convenience.
    DB transactions are "easy".

    Atomic cancellation as state is hard.
    @getify

    View full-size slide

  57. Cancellation: Abstraction Levels
    Exception (Uncertainty)
    Event
    State
    Collection of States
    @getify

    View full-size slide

  58. If it's async, it's gonna get

    canceled
    finally {
    ...whether you handle that
    state or not.
    cancellation shouldn't be an
    unexpected exception.
    @getify

    View full-size slide

  59. unexpected cancellation
    states feed uncertainty.
    }
    uncertainty spreads
    unnecessary complexity.
    complexity cancels out software
    quality and momentum.
    @getify

    View full-size slide