fn: add basic generic chan recv utility funcs

This commit is contained in:
Olaoluwa Osuntokun 2024-01-02 17:17:52 -08:00
parent f7e2e6ddd6
commit a2694009cd
No known key found for this signature in database
GPG Key ID: 3BBD59E99B280306

38
fn/recv.go Normal file
View File

@ -0,0 +1,38 @@
package fn
import (
"fmt"
"time"
)
// RecvOrTimeout attempts to recv over chan c, returning the value. If the
// timeout passes before the recv succeeds, an error is returned
func RecvOrTimeout[T any](c <-chan T, timeout time.Duration) (T, error) {
select {
case m := <-c:
return m, nil
case <-time.After(timeout):
var zero T
return zero, fmt.Errorf("timeout hit")
}
}
// RecvResp takes three channels: a response channel, an error channel and a
// quit channel. If either of these channels are sent on, then the function
// will exit with that response. This can be used to wait for a response,
// error, or a quit signal.
func RecvResp[T any](r <-chan T, e <-chan error, q <-chan struct{}) (T, error) {
var noResp T
select {
case resp := <-r:
return resp, nil
case err := <-e:
return noResp, err
case <-q:
return noResp, fmt.Errorf("quitting")
}
}