Skip to content

Commit

Permalink
refactor: refact Or func in channel.go
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-git committed Apr 21, 2022
1 parent d1c6c57 commit c27ccad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
13 changes: 9 additions & 4 deletions concurrency/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,17 @@ func (c *Channel) Or(channels ...<-chan any) <-chan any {
case <-channels[1]:
}
default:
m := len(channels) / 2
select {
case <-channels[0]:
case <-channels[1]:
case <-channels[2]:
case <-c.Or(append(channels[3:], orDone)...):
case <-c.Or(channels[:m]...):
case <-c.Or(channels[m:]...):
}
// select {
// case <-channels[0]:
// case <-channels[1]:
// case <-channels[2]:
// case <-c.Or(append(channels[3:], orDone)...):
// }
}
}()

Expand Down
8 changes: 7 additions & 1 deletion concurrency/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ func TestOr(t *testing.T) {
start := time.Now()

c := NewChannel()
<-c.Or(sig(2*time.Hour), sig(5*time.Minute), sig(1*time.Second), sig(1*time.Hour), sig(1*time.Minute))
<-c.Or(
sig(1*time.Second),
sig(2*time.Second),
sig(3*time.Second),
sig(4*time.Second),
sig(5*time.Second),
)

t.Logf("done after %v", time.Since(start))

Expand Down

0 comments on commit c27ccad

Please sign in to comment.