-
Notifications
You must be signed in to change notification settings - Fork 80
Use AsyncTimerSequence from swift-async-algorithms #986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
2fb8d2a
7c2b908
5477873
f0d11b2
ef06ce6
78accc3
27ce9ec
7faef59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| // | ||
| // This source file is part of the Swift Distributed Actors open source project | ||
| // | ||
| // Copyright (c) 2018-2021 Apple Inc. and the Swift Distributed Actors project authors | ||
| // Copyright (c) 2018-2022 Apple Inc. and the Swift Distributed Actors project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
|
|
@@ -12,6 +12,7 @@ | |
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import AsyncAlgorithms | ||
| import Distributed | ||
| import DistributedActors | ||
| import Logging | ||
|
|
@@ -24,7 +25,8 @@ distributed actor Philosopher: CustomStringConvertible { | |
| private let rightFork: Fork | ||
| private var state: State = .thinking | ||
|
|
||
| private lazy var timers = DistributedActors.ActorTimers<Philosopher>(self) | ||
| private var becomeHungryTimerTask: Task<Void, Error>? | ||
| private var finishEatingTimerTask: Task<Void, Error>? | ||
|
|
||
| init(name: String, leftFork: Fork, rightFork: Fork, actorSystem: ActorSystem) { | ||
| self.actorSystem = actorSystem | ||
|
|
@@ -58,15 +60,19 @@ distributed actor Philosopher: CustomStringConvertible { | |
| } | ||
|
|
||
| self.state = .thinking | ||
| self.timers.startSingle(key: .becomeHungry, delay: .seconds(1)) { | ||
| await self.attemptToTakeForks() | ||
| self.becomeHungryTimerTask = Task { | ||
| for await _ in AsyncTimerSequence(interval: .seconds(1), clock: ContinuousClock()) { | ||
| await self.attemptToTakeForks() | ||
| self.becomeHungryTimerTask?.cancel() | ||
| break | ||
| } | ||
|
||
| } | ||
| self.log.info("\(self.self.name) is thinking...") | ||
| self.log.info("\(self.name) is thinking...") | ||
| } | ||
|
|
||
| distributed func attemptToTakeForks() async { | ||
| guard self.state == .thinking else { | ||
| self.log.error("\(self.self.name) tried to take a fork but was not in the thinking state!") | ||
| self.log.error("\(self.name) tried to take a fork but was not in the thinking state!") | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -89,14 +95,14 @@ distributed actor Philosopher: CustomStringConvertible { | |
| } | ||
| self.forkTaken(self.rightFork) | ||
| } catch { | ||
| self.log.info("\(self.self.name) wasn't able to take both forks!") | ||
| self.log.info("\(self.name) wasn't able to take both forks!") | ||
| self.think() | ||
| } | ||
| } | ||
|
|
||
| /// Message sent to oneself after a timer exceeds and we're done `eating` and can become `thinking` again. | ||
| distributed func stopEating() { | ||
| self.log.info("\(self.self.name) is done eating and replaced both forks!") | ||
| self.log.info("\(self.name) is done eating and replaced both forks!") | ||
| Task { | ||
| do { | ||
| try await self.leftFork.putBack() | ||
|
|
@@ -128,10 +134,10 @@ distributed actor Philosopher: CustomStringConvertible { | |
|
|
||
| switch fork { | ||
| case self.leftFork: | ||
| self.log.info("\(self.self.name) received their left fork!") | ||
| self.log.info("\(self.name) received their left fork!") | ||
| self.state = .takingForks(leftTaken: true, rightTaken: rightForkIsTaken) | ||
| case self.rightFork: | ||
| self.log.info("\(self.self.name) received their right fork!") | ||
| self.log.info("\(self.name) received their right fork!") | ||
| self.state = .takingForks(leftTaken: leftForkIsTaken, rightTaken: true) | ||
| default: | ||
| self.log.error("Received unknown fork! Got: \(fork). Known forks: \(self.leftFork), \(self.rightFork)") | ||
|
|
@@ -144,16 +150,21 @@ distributed actor Philosopher: CustomStringConvertible { | |
|
|
||
| private func becomeEating() { | ||
| self.state = .eating | ||
| self.log.notice("\(self.self.name) began eating!") | ||
| self.timers.startSingle(key: .becomeHungry, delay: .seconds(3)) { | ||
| await self.stopEating() | ||
| self.log.notice("\(self.name) began eating!") | ||
| self.finishEatingTimerTask = Task { | ||
| for await _ in AsyncTimerSequence(interval: .seconds(3), clock: ContinuousClock()) { | ||
| self.stopEating() | ||
| self.finishEatingTimerTask?.cancel() | ||
| break | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension TimerKey { | ||
| static let becomeHungry: Self = "become-hungry" | ||
| static let finishEating: Self = "finish-eating" | ||
| deinit { | ||
| // FIXME: these are async | ||
| // self.becomeHungryTimerTask?.cancel() | ||
| // self.finishEatingTimerTask?.cancel() | ||
| } | ||
| } | ||
|
|
||
| extension Philosopher { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.