Daniel Lyons' Notes

Video - Leveraging structured concurrency in your applications - Franz Busch

Problems with unstructured concurrency

Screenshot 2024-10-02 at 3.07.54 PM.webp

  • Structured tasks:
    • Cancelled Automatically
    • Examples:
      • group tasks: withTaskGroup and group.add
      • async-let tasks
  • Unstructured tasks:
    • Must be cancelled manually
    • Should be avoided
    • Examples:
      • Task { }
      • Task.detached { }

Avoiding unstructured concurrency

swift-service-lifecycle package

  • Instead of: 04:09
    • Screenshot 2024-10-02 at 3.14.17 PM.webp
  • Use structured concurrency: 04:15
    • Screenshot 2024-10-02 at 3.15.52 PM.webp
  • swift-service-lifecycle 2.0: 04:34
    • Service Lifecycle is a Swift package designed to help server applications, also known as services, manage their startup and shutdown sequences.
    • adds graceful shutdown.
    • built-in support for UNIX signals: 06:16
      • Kubernetes sends a SIGTERM command to tell a service to fail gracefully.
        • If it doesn't fail gracefully, Kubernetes will send a SIGKILL and force the service to shutdown.
  • with-Style methods10:16:
    • Screenshot 2024-10-02 at 3.24.11 PM.webp
    • Common throughout the swift ecosystem:
      • e.g. withTaskGroup()
    • when to use with-style methods: 11:45
      • provide scoped access to a resource
    • This pattern forces safe cleanup after using the resource:
      • Screenshot 2024-10-02 at 3.27.23 PM.webp
    • Screenshot 2024-10-02 at 3.33.58 PM.webp

Composing it all together

Conclusion

Video - Leveraging structured concurrency in your applications - Franz Busch
Interactive graph
On this page
Problems with unstructured concurrency
Avoiding unstructured concurrency
swift-service-lifecycle package
Composing it all together
Conclusion