88)
99
1010// Multiplex combines multiple empty struct channels into one.
11+ // TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133
1112func Multiplex (channels ... <- chan struct {}) <- chan struct {} {
1213 ch := make (chan struct {})
1314 if len (channels ) == 0 {
@@ -25,13 +26,29 @@ func Multiplex(channels ...<-chan struct{}) <-chan struct{} {
2526 return ch
2627}
2728
28- // WithDeadline returns empty struct channel based on Time channel.
29+ // WithDeadline returns empty struct channel above on `time.Timer` channel.
30+ // TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133
2931func WithDeadline (deadline time.Time ) <- chan struct {} {
30- // go 1.5 doesn't support time.Until(deadline)
31- return WithTimeout (deadline .Sub (time .Now ())) //nolint: gosimple
32+ ch := make (chan struct {})
33+ if time .Now ().After (deadline ) {
34+ close (ch )
35+ return ch
36+ }
37+ go func () {
38+ // go 1.5 doesn't support time.Until(deadline)
39+ after := deadline .Sub (time .Now ()) //nolint: gosimple
40+ if after <= 0 {
41+ close (ch )
42+ return
43+ }
44+ <- time .After (after )
45+ close (ch )
46+ }()
47+ return ch
3248}
3349
34- // WithSignal returns empty struct channel based on Signal channel.
50+ // WithSignal returns empty struct channel above on `os.Signal` channel.
51+ // TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133
3552func WithSignal (s os.Signal ) <- chan struct {} {
3653 ch := make (chan struct {})
3754 if s == nil {
@@ -48,16 +65,13 @@ func WithSignal(s os.Signal) <-chan struct{} {
4865 return ch
4966}
5067
51- // WithTimeout returns empty struct channel based on Time channel.
68+ // WithTimeout returns empty struct channel above on `time.Timer` channel.
69+ // TODO can be leaky, https://github.com/kamilsk/semaphore/issues/133
5270func WithTimeout (timeout time.Duration ) <- chan struct {} {
5371 ch := make (chan struct {})
5472 if timeout <= 0 {
5573 close (ch )
5674 return ch
5775 }
58- go func () {
59- <- time .After (timeout )
60- close (ch )
61- }()
62- return ch
76+ return WithDeadline (time .Now ().Add (timeout ))
6377}
0 commit comments