Skip to content
This repository has been archived by the owner on Jul 29, 2020. It is now read-only.

Commit

Permalink
filter: Make the code mor idiomatic and drop the context dependency
Browse files Browse the repository at this point in the history
There is no need to await ctx.Done() in the filter loop as we can
simply stop looping when the input channel is closed. We'll also close
our output channel now and make this code more consistent.
  • Loading branch information
joa committed Mar 26, 2019
1 parent b1b88db commit 05c7d86
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
2 changes: 1 addition & 1 deletion broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type broadcast struct {
func newBroadcast(ctx context.Context, src <-chan ServerList) *broadcast {
b := &broadcast{
ctx: ctx,
src: newServerListFilter(ctx, src),
src: filterEqualServerList(src),
tgts: make(map[Listener]bool),
add: make(chan Listener),
rem: make(chan Listener),
Expand Down
31 changes: 10 additions & 21 deletions filter.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
package main

import "context"

func newServerListFilter(ctx context.Context, in <-chan ServerList) <-chan ServerList {
func filterEqualServerList(in <-chan ServerList) <-chan ServerList {
out := make(chan ServerList)
go doFilter(ctx, in, out)
return out
}

func doFilter(ctx context.Context, in <-chan ServerList, out chan ServerList) {
var prev ServerList
go func() {
defer close(out)

var prev ServerList

looping:
for {
select {
case <-ctx.Done():
return
case next := <-in:
for next := range in {
if len(prev) == len(next) {
equal := true

// make a sacrifice for the ssa range check elimination gods
n := len(prev)
_ = prev[n-1]
_ = next[n-1]

for i := range prev {
if !prev[i].Equal(next[i]) {
equal = false
Expand All @@ -33,12 +20,14 @@ looping:
}

if equal {
continue looping
continue
}
}

prev = next
out <- next
}
}
}()

return out
}

0 comments on commit 05c7d86

Please sign in to comment.