Once.do
func Once.do[T](once: Once, f: func(): T): voidOnce.do runs f on the first call for once and discards its value; later calls wait for that run to finish and do nothing. If f panics, once is complete and every call, waiting or later, panics with the same message.
Details
- Only the first call runs its function. Every later call ignores the function it is given, even a different one.
- A call made while the first run is still going waits for it to finish, so after any call returns, the run's effects are visible.
- A panic in
fcompletes the once too: that call and every later one panic with the same message and trace, andfdoes not run again. - A call inside
fon the same once waits forfto finish, so it never returns.
Basic examples
Only the first of three calls runs its function:
let once = Once.new()
for _ in Array.range(0, 3) {
Once.do(once, { println("setting up") })
}
println("done")setting up
doneSeveral goroutines need the same setup. The first one runs it, the others wait for it, and all of them see its result:
type Config = { region: string }
let once = Once.new()
let loads = Atomic.new(0)
let config = Config{ region: "" }
func loadConfig() {
Time.sleep(10ms)
config.region = "eu-west"
loads.add(1)
}
let regions = Array.filled(4, "")
let wg = WaitGroup.new()
for i in Array.range(0, 4) {
wg.add(1)
go {
defer { wg.done() }
Once.do(once, loadConfig)
regions[i] = config.region
}
}
wg.wait()
println(regions)
println("loaded ${loads.load()} time")["eu-west", "eu-west", "eu-west", "eu-west"]
loaded 1 timeScope
The function may return any type. Once.do runs it for its effects and drops the value:
func readGreeting(): string {
println("reading greeting")
"hello"
}
let once = Once.new()
once.do(readGreeting)
once.do(readGreeting)reading greetingA Once in a record field runs once per record. A field default gives each record its own:
type Server = { name: string, started: Once = Once.new() }
func Server.start(s: Server) {
s.started.do({ println("starting ${s.name}") })
}
let api = Server{ name: "api" }
let web = Server{ name: "web" }
api.start()
web.start()
api.start()starting api
starting webApplications
Closing a channel twice panics. Several goroutines may each try to close the same channel; the once closes it exactly once:
let stop: Channel[bool] = Channel.new()
let closeStop = Once.new()
let wg = WaitGroup.new()
for _ in Array.range(0, 3) {
wg.add(1)
go {
defer { wg.done() }
closeStop.do({ stop.close() })
}
}
wg.wait()
println(stop.isClosed())trueProperties and relations
The function runs once, however many calls there are, and the functions of later calls never run:
let once = Once.new()
var runs = 0
var later = false
once.do({ runs += 1 })
once.do({ runs += 1 })
once.do({ later = true })
assert(runs == 1)
assert(!later)Once.value is the same guarantee for a function whose result you need. It returns the cached value on every call:
let theme = Once.value({
println("reading theme")
"dark"
})
println(theme())
println(theme())reading theme
dark
darkPossible issues
Once.do returns void, so it does not hand back the function's value. Use Once.value instead:
let once = Once.new()
let ready = once.do({ "ready" })
println(ready.length()) // error: void has no field or function lengthA failed run is not retried. After f panics, every later call panics with the same message, here caught the first time with Panic.catch:
let once = Once.new()
let first = Panic.catch({ once.do({ panic("config missing") }) })
first is Err // true
once.do({ println("retry") }) // panic: config missingA Once created inside a function is new on every call, so it guards nothing. Create it where it outlives the calls, at the top level or in a record:
func setup() {
let once = Once.new()
once.do({ println("setting up") })
}
setup()
setup()setting up
setting upSee also
Once.new · Once.value · Once · Mutex.withLock · WaitGroup · Panic.catch · Concurrency guide
