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

The Pitfalls of Cancellation

The Pitfalls of Cancellation

Masatoshi Kubode

December 22, 2020
Tweet

More Decks by Masatoshi Kubode

Other Decks in Programming

Transcript

  1. ©2020 Wantedly, Inc.
    The Pitfalls of Cancellation
    Presentation Subtitle
    2020/12/22 Masatoshi Kubode
    https://unsplash.com/photos/J8sh37XZ8ww

    View Slide

  2. ©2020 Wantedly, Inc.
    ٱอग़խढ़
    8BOUFEMZ
    8BOUFEMZ7JTJUͷ"OESPJEΞϓϦΛ։ൃ͍ͯ͠·͢ɻ
    ,PUMJO .BHJDUIF(BUIFSJOH
    UIJTUP4USJOH

    5XJUUFS
    !TXJ[@BSE
    (JU)VC
    !LVCPEF

    View Slide

  3. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ
    ©2020 Wantedly, Inc.
    $PSPVUJOFTͰى͖ͨΫϥογϡͷ࿩

    View Slide

  4. ©2020 Wantedly, Inc.
    ͋Δ೔ͷΫϥογϡϨϙʔτ

    View Slide

  5. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ
    ©2020 Wantedly, Inc.

    View Slide

  6. ©2020 Wantedly, Inc.
    ֘౰ίʔυ
    class FetchDiscoverPostsUseCaseImpl(
    private val discoverRepository: DiscoverRepository,
    ) : FetchDiscoverPostsUseCase {
    override suspend operator fun invoke(sectionId: DiscoverSectionId) {
    try {
    return discoverRepository.fetchDiscoverPosts(sectionId)
    } catch (@Suppress("TooGenericExceptionCaught") e: Throwable) {
    throw FetchDiscoverProjectsUseCase.Error(
    "Failed to fetch discover posts for section: $sectionId”,
    e
    )
    }
    }
    }
    ←ίϐϖͰTypoͯ͠Δ
    Repositoryͷྫ֎Λϥοϓͯ͠
    Rethrowͯ͠Δ͚ͩͳͷʹ
    ͳͥ?

    View Slide

  7. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ
    ©2020 Wantedly, Inc.
    4UBDL5SBDFΛΑ͘ݟΔ

    View Slide

  8. ©2020 Wantedly, Inc.
    ͋Δ೔ͷΫϥογϡϨϙʔτ
    JobCancellationException͕ݪҼ

    View Slide

  9. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ
    ©2020 Wantedly, Inc.
    +PC$BODFMMBUJPO&YDFQUJPOͱ͸

    View Slide

  10. ©2020 Wantedly, Inc.
    JobCancellationException
    https://atoulme.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job-cancellation-exception/index.html
    Job.cancel()͞Εͨͱ͖ʹ
    suspend͍ͯ͠Δؔ਺͔ΒThrow͞ΕΔ
    CoroutinesͷγεςϜతͳྫ֎

    View Slide

  11. ©2020 Wantedly, Inc.
    ֘౰ίʔυ
    class FetchDiscoverPostsUseCaseImpl(
    private val discoverRepository: DiscoverRepository,
    ) : FetchDiscoverPostsUseCase {
    override suspend operator fun invoke(sectionId: DiscoverSectionId) {
    try {
    return discoverRepository.fetchDiscoverPosts(sectionId)
    } catch (@Suppress("TooGenericExceptionCaught") e: Throwable) {
    throw FetchDiscoverProjectsUseCase.Error(
    "Failed to fetch discover posts for section: $sectionId”,
    e
    )
    }
    }
    } ͭ·ΓɺCoroutinesͷγεςϜతͳྫ֎Λ
    ϥοϓͯ͠͠·͔ͬͨΒΫϥογϡͨ͠

    View Slide

  12. ©2020 Wantedly, Inc.
    JobCancellationExceptionৄղ
    ΋ͱ΋ͱJavaͷฒྻॲཧAPIͰ࢖ΘΕ͍ͯͨྫ֎

    View Slide

  13. ©2020 Wantedly, Inc.
    Javaͷੈք
    java.util.concurrent.Future#get()
    try {
    future.get();
    } catch (ExecutionException e) {
    e.printStackTrace();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    CancellationException͸RuntimeException
    ͳͷͰ໌ࣔతͳcatchෆཁ
    CancellationΛcatchͪ͠Ό͏͜ͱ͕ى͖ͮΒ͍

    View Slide

  14. ©2020 Wantedly, Inc.
    Kotlinͷੈք
    Kotlinʹ͸ݕࠪྫ֎͕ͳ͍
    catch(Throwable)͕ͪ͠
    CoroutinesͰ͸ࠓճͷΑ͏ͳ໰୊ʹܨ͕Γ΍͍͢
    cancel()ͯ͠໰୊ͳ͍͔ͱ͍͏ςετ͸;ͭ͏ॻ͔ͳ͍͠ɺ
    ݱঢ়͸ϓϩάϥϚ͕ؾΛ͚ͭΔ͔͠ͳ͍ʁ

    View Slide

  15. ©2020 Wantedly, Inc.
    Ͳ͏͢΂͖͔
    launch {
    try {
    codeThatCanThrowExceptions()
    //
    // } catch (e: Throwable) {
    // } catch (e: Exception) {
    // } catch (e: RuntimeException) {
    // } catch (e: IllegalStateException) {
    // } catch (e: CancellationException) {
    } catch (e: SpecificException) { //
    throw MyException(e)
    }
    }
    // OR
    launch {
    try {
    codeThatCanThrowExceptions()
    } catch (e: Throwable) {
    throw e as? CancellationException ?: MyException(e)
    }
    }

    View Slide

  16. ©2020 Wantedly, Inc.
    w ,PUMJOʹ͸ݕࠪྫ֎͕ͳ͍ͷͰࡶʹ͕͕ͪͩ͠ɺ
    $PSPVUJOFTͰ5PP(FOFSJDͳDBUDI͸ةݥ
    w DBUDI͔ͨͬͨ͠Β$BODFMMBUJPO͸SFUISPX
    w ΋͘͠͸$PSPVUJOF&YDFQUJPO)BOEMFSΛ࢖͏
    w -JOU͸ແࢹ͠ͳ͍
    ·ͱΊ

    View Slide

  17. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ
    ©2020 Wantedly, Inc.

    View Slide