| Port | What it provides |
|---|---|
| Clock | Wall-clock time and delays |
| Timer | A time limit for a piece of work (within) |
| Gate | A counting, FIFO admission gate |
| Conn | An owned, full-duplex byte connection |
| Dialer | Opening connections and upgrading them to TLS |
| Signal | A sticky one-shot notification |
| Coordination | Structured concurrency: a primary task and its monitor |
///|
/// Waits `attempts` times, doubling the delay each time, and returns the
/// time spent.
async fn backoff(clock : &@rt.Clock, attempts : Int32) -> Int64 {
let start = clock.now_unix_ms()
for i = 0, delay = 100; i < attempts; i = i + 1, delay = delay * 2 {
clock.sleep(delay)
}
clock.now_unix_ms() - start
}
///|
/// A clock whose sleeps move its time forward at once.
struct Instant {
mut now : Int64
}
///|
impl @rt.Clock for Instant with fn sleep(self, ms) {
if ms > 0 {
self.now += ms.to_int64()
}
}
///|
impl @rt.Clock for Instant with fn now_unix_ms(self) {
self.now
}
///|
test "back off on a test clock" {
let clock = Instant::{ now: 0, }
assert_eq(@driver.run_sync(() => backoff(clock, 3)), 700L)
}pub(open) trait Clock {
async fn sleep(Self, Int) -> Unit noraise
fn now_unix_ms(Self) -> Int64
}pub(open) trait Conn {
async fn read(Self, max_len~ : Int) -> Bytes? raise TransportError
async fn write(Self, Bytes) -> Unit raise TransportError
fn close(Self) -> Unit
}pub(open) trait Coordination {
fn signal(Self) -> &Signal
async fn supervise(Self, primary~ : async () -> Unit raise TransportError, monitor~ : async () -> Unit raise TransportError) -> Unit raise TransportError
}pub(open) trait Dialer {
async fn connect(Self, host~ : String, port~ : Int) -> &Conn raise TransportError
async fn start_tls(Self, &Conn, host~ : String) -> &Conn raise TransportError
}pub(open) trait Gate {
async fn acquire(Self) -> Unit noraise
fn release(Self) -> Unit
}pub(open) trait Signal {
async fn wait(Self) -> Unit noraise
fn notify(Self) -> Unit
}pub(open) trait Timer {
async fn within(Self, Int, async () -> Unit noraise) -> Bool noraise
}fn default_random() -> DoubleInstall
Download zipThe HTTP vocabulary, a sans-IO HTTP/1.1 codec and runtime-independent client and server connection engines for MoonBit. Depends on moonbitlang/core only; runs on every backend.