timing/deadline.protos
- Repository
- guillermomolina/protos
- Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75- Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6- Blob
4a6fe1499475b3ee58bfc2b0067d25e06bef2e37- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
// Requires: bin/protos --allow-timing protos/examples/timing/deadline.protos
// Timing coordinates are exact Integer nanoseconds of a monotonic, non-civil
// temporal domain. Relative delays and deadlines are ordinary Integer arithmetic.
millisecond: 1000000
start: timing.monotonicNanoseconds()
// A one-shot timer resolves to null no earlier than its absolute deadline.
timing.atNanoseconds(start + 5 * millisecond).value()
print(timing.monotonicNanoseconds() - start >= 5 * millisecond)
// Observing owned work against an absolute deadline mirrors the work's outcome
// when the work becomes terminal before the deadline.
work: (() => { 6 * 7 }).future()
print(timing.observeUntil(work, timing.monotonicNanoseconds() + 1000 * millisecond).value())
// When the deadline is reached first, the observation fails with DeadlineExceeded.
slow: timing.atNanoseconds(timing.monotonicNanoseconds() + 60000 * millisecond)
deadline: timing.monotonicNanoseconds() + millisecond
print(DeadlineExceeded.handle(
() => { timing.observeUntil(slow, deadline).value() },
(error) => { "deadline exceeded" }
))
// The observation neither owns nor cancels the observed work: cancelling it is
// a separate, explicit decision of its owner.
slow.cancel()
print(Cancelled.handle(() => { slow.value() }, (error) => { "cancelled by its owner" }))
// Timing reaches another Actor only through explicit Actor.spawn provisioning;
// the worker's representation shares this temporal domain.
worker: Actor.spawn("./deadline-worker.protos", "start", timing)
before: timing.monotonicNanoseconds()
after: worker.request("pause", millisecond).value()
print(after >= before + millisecond)
worker.stop()