-
-
Save rightfold/1c9f64faef24921f9e94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <- for send and receive | |
import mill.time | |
import mill.conc.chan | |
import mill.conc.thread | |
sub withTimeout[R](timeout: time.Duration, s: () => R): R { | |
let resultChan = chan.make[R]() | |
thread.spawn(sub(): () { resultChan <- s() }) | |
select { | |
case let result = <-resultChan => result | |
case let _ = <-time.after(timeout) => throw(timeoutError) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// chan.send and chan.recv for send and receive | |
import mill.time | |
import mill.conc.chan | |
import mill.conc.thread | |
sub withTimeout[R](timeout: time.Duration, s: () => R): R { | |
let resultChan = chan.make[R]() | |
thread.spawn(sub(): () { chan.send(resultChan, s()) }) | |
select { | |
case let result = chan.recv(resultChan) => result | |
case let _ = chan.recv(time.after(timeout)) => throw(timeoutError) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// always-in-scope (unless shadowed) send and recv for send and receive | |
import mill.time | |
import mill.conc.chan | |
import mill.conc.thread | |
sub withTimeout[R](timeout: time.Duration, s: () => R): R { | |
let resultChan = chan.make[R]() | |
thread.spawn(sub(): () { send(resultChan, s()) }) | |
select { | |
case let result = recv(resultChan) => result | |
case let _ = recv(time.after(timeout)) => throw(timeoutError) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment