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
PRO

December 07, 2018
Tweet

More Decks by Kyle Simpson

Other Decks in Programming

Transcript

  1. Cancel All My Appointments!
    Kyle Simpson @getify

    View Slide

  2. HAPPENS
    Failure
    @getify

    View Slide

  3. HAPPENS
    Cancellation
    @getify

    View Slide

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

    View Slide

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

    Rollback
    Event?
    State
    @getify

    View Slide

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

    View Slide

  7. ?
    @getify

    View Slide

  8. ¯\_(ϑ)_/¯

    UNCERTAINTY
    @getify

    View Slide

  9. ing
    Problem
    @getify

    View Slide

  10. How?
    @getify

    View Slide

  11. Approximate!
    @getify

    View Slide

  12. Timeouts
    @getify

    View Slide

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

    View Slide

  14. @getify

    View Slide

  15. Schrödinger's Asynchrony
    @getify

    View Slide

  16. @getify

    View Slide

  17. Cancel...a Cancellation?
    @getify

    View Slide

  18. @getify

    View Slide

  19. Cancelable Promise?
    @getify

    View Slide

  20. Wrong Level of Abstraction
    @getify

    View Slide

  21. Schrödinger's Cancellation
    @getify

    View Slide

  22. @getify

    View Slide

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

    View Slide

  24. Complexity Randomness
    ~
    ~
    @getify

    View Slide

  25. UNCERTAINTY
    @getify

    View Slide

  26. Cancel what?
    @getify

    View Slide

  27. I FIGHT FOR THE

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

    View Slide

  28. @getify

    View Slide

  29. @getify

    View Slide

  30. @getify

    View Slide

  31. @getify

    View Slide

  32. @getify

    View Slide

  33. @getify

    View Slide

  34. @getify

    View Slide

  35. Cancellation Must Be

    A First Class Citizen
    @getify

    View Slide

  36. Developers,
    Developers,
    Developers?
    @getify

    View Slide

  37. Timers
    @getify

    View Slide

  38. Ajax Calls
    @getify

    View Slide

  39. User Events
    @getify

    View Slide

  40. What about...?
    @getify

    View Slide

  41. File System Operations
    @getify

    View Slide

  42. Stream Operations
    @getify

    View Slide

  43. Network I/O
    @getify

    View Slide

  44. Database Queries
    @getify

    View Slide

  45. Child Processes
    @getify

    View Slide

  46. Yadda, Yadda, Yadda...
    @getify

    View Slide

  47. @getify

    View Slide

  48. @getify

    View Slide

  49. Debounce: Spinner
    @getify

    View Slide

  50. Debounce: Spinner
    @getify

    View Slide

  51. Un-observable-ation
    Implied Cancellation
    @getify

    View Slide

  52. Starvation
    @getify

    View Slide

  53. @getify

    View Slide

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

    View Slide

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

    View Slide

  56. @getify

    View Slide

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

    View Slide

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

    View Slide

  59. Timeouts vs Deadlines
    @getify

    View Slide

  60. Timeouts vs Deadlines
    @getify

    View Slide

  61. Cancellation:

    Serial vs Concurrent
    @getify

    View Slide

  62. @getify

    View Slide

  63. @getify

    View Slide

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

    View Slide

  65. Deep(er) Cancellation
    @getify

    View Slide

  66. Race: A xor B
    @getify

    View Slide

  67. @getify

    View Slide

  68. Promises are a bit awkward here
    @getify

    View Slide

  69. Cancellation Tokens
    @getify

    View Slide

  70. @getify

    View Slide

  71. cancelable fetch!
    @getify

    View Slide

  72. Async Functions:

    not (externally) cancelable
    @getify

    View Slide

  73. @getify

    View Slide

  74. @getify

    View Slide

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

    View Slide

  76. @getify

    View Slide

  77. still cancelable fetch!
    @getify

    View Slide

  78. Tedious?
    @getify

    View Slide

  79. Idea*:

    Cancellation Zones
    (*still very rough idea)
    @getify

    View Slide

  80. @getify

    View Slide

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

    Atomic cancellation as state is hard.
    @getify

    View Slide

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

    View Slide

  83. 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 Slide

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

    View Slide