Threads and servicesSlide 47 of 86
47spawnThreads and services

Services

A service is a piece of state with a mailbox, reached by its Address<M>, where M is the type of message it takes. The same statement reaches it from this process or from another machine. Three builtins manage services:

  • spawn(handler, state) starts one and answers with a Result<Address<M>, hive.syslink.SyslinkError>, reading M off the handler. spawn(handler, state, #Name) also registers it under an atom, and fails as Taken if another service holds the name.
  • at(#Name) is the service registered under that name here, and at(endpoint, #Name) the one on another node. It builds an address without looking anything up. A name says nothing about what the service takes, so at takes its M from where it lands — Address<Note> box = at(#Cache) — or, bound with :=, from the first send after it, and keeps it.
  • kill(address) stops one, on any node. Killing one twice is harmless, and its name is free again before anyone hears it stopped.

A handler is one turn, proc(state: mut S, msg: M): M: it changes the state in place and answers with a message of the same type. It may take a third parameter, its own Address<M>, to hand to work it starts. Only the service's mailbox holds the state, one message at a time, so nothing needs a lock — and a test calls a handler directly with a mut variable of its own. When every branch of a handler returns, in one chain covering the message type, a variant added to the message type later is a compile error in the handler.

Calling an address is a request:

  • box(msg) waits for the answer, as a Result<M, SyslinkError>, for up to five seconds unless with timeout says otherwise.
  • async box(msg) sends and forgets; the answer is dropped.
  • x := async box(msg) and await [a(m), b(m)] send now and wait later.

A request that times out still waits its turn in the mailbox, ahead of the ones behind it. Because an address carries its message type, sending the wrong type is a compile error; a message sent to a name whose service takes another type fails when it arrives, as Decode.

A panic in a handler kills only that service: the node writes hive: service Counter crashed: … to stderr, and its callers get Down. hive.syslink.monitor(watcher, target, notice) posts notice to a service on this node when target stops. A named address outlives its service — spawn a new one under the name and the old address reaches it — while an anonymous address dies with its service.

Every failure is a variant of SyslinkError: Taken, Down, NoProc for a name nothing holds, Timeout, Unreachable, Decode, and, across nodes, NoPeer, NoListener and NoKey. A switch over it covers every way a request can fail.

A service on this node
type Note {
	Say   { text: Str }
	Count
	Total { count: Int }
}

proc tally(state: mut Int, msg: Note): Note {
	if msg is Note.Say(text) {
		echo text
		return msg
	} else if msg is Note.Count {
		state += 1
		return Note.Total(state)
	} else if msg is Note.Total(_) {
		return msg
	}
}

proc main(): void {
	if spawn(tally, 0, #Cache) is Result.Error(err) {
		panic err
	}
	Address<Note> address = at(#Cache)

	async address(Note.Say("hi"))                    // fire and forget
	reply := address(Note.Count()) with timeout 250   // send and wait, bounded
	if reply is Result.Ok(msg) {
		echo msg
	}
}
Slow work, off the turn
type Note {
	Fetch
	Fetched { text: Str }
	Latest
	Answer  { text: Str }
}

proc desk(latest: mut Str, note: Note, me: Address<Note>): Note {
	if note is Note.Fetch {
		async fetch(me)                // the turn ends now
		return Note.Answer("started")
	} else if note is Note.Fetched(text) {
		latest = text                  // the result arrives as a message
		return note
	} else if note is Note.Latest {
		return Note.Answer(latest)
	} else if note is Note.Answer(_) {
		return note
	}
}

proc fetch(back: Address<Note>): void {
	hive.task.sleep(300)
	async back(Note.Fetched("worth the wait"))
}

proc main(): void {
	if spawn(desk, "nothing yet") is Result.Ok(box) {
		if box(Note.Fetch()) with timeout 500 is Result.Ok(now) { echo now }
		if box(Note.Latest()) with timeout 500 is Result.Ok(first) { echo first }
		hive.task.sleep(500)
		if box(Note.Latest()) with timeout 500 is Result.Ok(later) { echo later }
	}
}