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

The Pitfalls of Cancellation

The Pitfalls of Cancellation

E505897a79eede1a676f92740261e8f8?s=128

Masatoshi Kubode

December 22, 2020
Tweet

Transcript

  1. ©2020 Wantedly, Inc. The Pitfalls of Cancellation Presentation Subtitle 2020/12/22

    Masatoshi Kubode https://unsplash.com/photos/J8sh37XZ8ww
  2. ©2020 Wantedly, Inc. ٱอग़խढ़ 8BOUFEMZ 8BOUFEMZ7JTJUͷ"OESPJEΞϓϦΛ։ൃ͍ͯ͠·͢ɻ ,PUMJO .BHJDUIF(BUIFSJOH UIJTUP4USJOH 5XJUUFS

    !TXJ[@BSE (JU)VC !LVCPEF
  3. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2020 Wantedly, Inc. $PSPVUJOFTͰى͖ͨΫϥογϡͷ࿩

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

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

  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ͯ͠Δ͚ͩͳͷʹ ͳͥ?
  7. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2020 Wantedly, Inc. 4UBDL5SBDFΛΑ͘ݟΔ

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

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

  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ͷγεςϜతͳྫ֎

  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ͷγεςϜతͳྫ֎Λ ϥοϓͯ͠͠·͔ͬͨΒΫϥογϡͨ͠
  12. ©2020 Wantedly, Inc. JobCancellationExceptionৄղ ΋ͱ΋ͱJavaͷฒྻॲཧAPIͰ࢖ΘΕ͍ͯͨྫ֎

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

  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) } }
  16. ©2020 Wantedly, Inc. w ,PUMJOʹ͸ݕࠪྫ֎͕ͳ͍ͷͰࡶʹ͕͕ͪͩ͠ɺ $PSPVUJOFTͰ5PP(FOFSJDͳDBUDI͸ةݥ w DBUDI͔ͨͬͨ͠Β$BODFMMBUJPO͸SFUISPX w ΋͘͠͸$PSPVUJOF&YDFQUJPO)BOEMFSΛ࢖͏

    w -JOU͸ແࢹ͠ͳ͍ ·ͱΊ
  17. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2020 Wantedly, Inc.