Skip to content

Commit

Permalink
fix: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtdeh committed Feb 24, 2024
1 parent eed0d71 commit f2379fc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
7 changes: 2 additions & 5 deletions chanswitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ type Channels struct {
type ChanSwitch struct {
filters map[any]*Channels
val any
l sync.Mutex
distract chan struct{}
a int
b int
l sync.Mutex
}

func (b *ChanSwitch) currentChan() chan struct{} {
Expand All @@ -33,7 +31,6 @@ func repeater(b *ChanSwitch) {
select {
case b.currentChan() <- struct{}{}:
case <-b.distract:
b.b++
}
}

Expand Down Expand Up @@ -85,7 +82,6 @@ func (b *ChanSwitch) Make(v any) *Channels {
func (b *ChanSwitch) Set(v any) {
b.l.Lock()
defer b.l.Unlock()
b.a++
// get filter by value
ch := b.filters[v]
// update val
Expand All @@ -99,6 +95,7 @@ func (b *ChanSwitch) Set(v any) {
for k, c := range b.filters {
if v != k {
if len(c.open) > 0 {
// close once channel of old filter
closeChan(c.once)
// close open channel of old filter
closeChan(c.open)
Expand Down
13 changes: 4 additions & 9 deletions chanswitch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ func runIntTest(t *testing.T, m, n int) {
for j := 0; j < n; j++ {

go b.Set("connecting")
b.WaitFor(ctx, "connecting")
// b.WaitFor(ctx, "connecting")

go b.Set("connected")
b.WaitFor(ctx, "connected")
// b.WaitFor(ctx, "connected")

go b.Set("disconnecting")
b.WaitFor(ctx, "disconnecting")
// b.WaitFor(ctx, "disconnecting")

go b.Set("disconnected")
b.WaitFor(ctx, "disconnected")
// b.WaitFor(ctx, "disconnected")

}

Expand All @@ -79,11 +79,6 @@ func runIntTest(t *testing.T, m, n int) {
b.WaitFor(ctx, "shutdown")

time.Sleep(time.Second * 3)
// runtime.GC()
// printAlloc("a")

fmt.Println("a : ", b.a)
fmt.Println("b : ", b.b)

fmt.Println("count: ", c)

Expand Down

0 comments on commit f2379fc

Please sign in to comment.