mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-28 09:58:52 +03:00
fe8be3701f
Merge in DNS/adguard-home from websvc-config-manager to master Squashed commit of the following: commit 2143b47c6528030dfe059172888fddf9061e42da Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Oct 4 14:50:47 2022 +0300 next: add config manager
33 lines
785 B
Go
33 lines
785 B
Go
// Package aghchan contains channel utilities.
|
|
package aghchan
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// Receive returns an error if it cannot receive a value form c before timeout
|
|
// runs out.
|
|
func Receive[T any](c <-chan T, timeout time.Duration) (v T, ok bool, err error) {
|
|
var zero T
|
|
timeoutCh := time.After(timeout)
|
|
select {
|
|
case <-timeoutCh:
|
|
// TODO(a.garipov): Consider implementing [errors.Aser] for
|
|
// os.ErrTimeout.
|
|
return zero, false, fmt.Errorf("did not receive after %s", timeout)
|
|
case v, ok = <-c:
|
|
return v, ok, nil
|
|
}
|
|
}
|
|
|
|
// MustReceive panics if it cannot receive a value form c before timeout runs
|
|
// out.
|
|
func MustReceive[T any](c <-chan T, timeout time.Duration) (v T, ok bool) {
|
|
v, ok, err := Receive(c, timeout)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return v, ok
|
|
}
|