Skip to content

Commit

Permalink
refactor: remove unused Update channel from checks and simplify relat…
Browse files Browse the repository at this point in the history
…ed logic

Signed-off-by: lvlcn-t <[email protected]>
  • Loading branch information
lvlcn-t committed Nov 17, 2024
1 parent cb31835 commit fc2aa54
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 45 deletions.
3 changes: 0 additions & 3 deletions pkg/checks/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ type Base struct {
Mutex sync.Mutex
// Done channel is used to notify about shutdown of a check.
Done chan struct{}
// Update is a channel used to notify about configuration updates.
Update chan struct{}
// closed is a flag indicating if the check has been shut down.
closed bool
}
Expand All @@ -80,7 +78,6 @@ func NewBase() Base {
return Base{
Mutex: sync.Mutex{},
Done: make(chan struct{}, 1),
Update: make(chan struct{}, 3),
closed: false,
}
}
Expand Down
21 changes: 8 additions & 13 deletions pkg/checks/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,30 @@ func TestBase_Shutdown(t *testing.T) {

tests := []struct {
name string
b *Base
base *Base
}{
{
name: "shutdown",
b: &Base{
Done: make(chan struct{}, 1),
},
base: &Base{Done: make(chan struct{}, 1)},
},
{
name: "already shutdown",
b: &Base{
Done: make(chan struct{}, 1),
closed: true,
},
base: &Base{Done: make(chan struct{}, 1), closed: true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.b.closed {
close(tt.b.Done)
if tt.base.closed {
close(tt.base.Done)
}
tt.b.Shutdown()
tt.base.Shutdown()

if !tt.b.closed {
if !tt.base.closed {
t.Error("Base.Shutdown() should close Base.Done")
}

assert.Panics(t, func() {
tt.b.Done <- struct{}{}
tt.base.Done <- struct{}{}
}, "Base.Done should be closed")
})
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/checks/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ func (ch *check) Run(ctx context.Context, cResult chan checks.ResultDTO) error {
return ctx.Err()
case <-ch.Done:
return nil
case <-ch.Update:
ch.Mutex.Lock()
timer.Reset(ch.config.Interval)
log.DebugContext(ctx, "Interval of dns check updated", "interval", ch.config.Interval.String())
ch.Mutex.Unlock()
case <-timer.C:
res := ch.check(ctx)
cResult <- checks.ResultDTO{
Expand All @@ -119,7 +114,7 @@ func (ch *check) UpdateConfig(cfg checks.Runtime) error {
if c, ok := cfg.(*Config); ok {
ch.Mutex.Lock()
defer ch.Mutex.Unlock()
if reflect.DeepEqual(ch.config, *c) {
if c == nil || reflect.DeepEqual(&ch.config, c) {
return nil
}

Expand All @@ -133,7 +128,6 @@ func (ch *check) UpdateConfig(cfg checks.Runtime) error {
}

ch.config = *c
ch.Update <- struct{}{}
return nil
}

Expand Down
8 changes: 1 addition & 7 deletions pkg/checks/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ func (ch *check) Run(ctx context.Context, cResult chan checks.ResultDTO) error {
return ctx.Err()
case <-ch.Done:
return nil
case <-ch.Update:
ch.Mutex.Lock()
timer.Reset(ch.config.Interval)
log.DebugContext(ctx, "Interval of health check updated", "interval", ch.config.Interval.String())
ch.Mutex.Unlock()
case <-timer.C:
res := ch.check(ctx)
cResult <- checks.ResultDTO{
Expand All @@ -107,7 +102,7 @@ func (ch *check) UpdateConfig(cfg checks.Runtime) error {
if c, ok := cfg.(*Config); ok {
ch.Mutex.Lock()
defer ch.Mutex.Unlock()
if reflect.DeepEqual(ch.config, *c) {
if c == nil || reflect.DeepEqual(&ch.config, c) {
return nil
}

Expand All @@ -121,7 +116,6 @@ func (ch *check) UpdateConfig(cfg checks.Runtime) error {
}

ch.config = *c
ch.Update <- struct{}{}
return nil
}

Expand Down
8 changes: 1 addition & 7 deletions pkg/checks/latency/latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ func (ch *check) Run(ctx context.Context, cResult chan checks.ResultDTO) error {
return ctx.Err()
case <-ch.Done:
return nil
case <-ch.Update:
ch.Mutex.Lock()
timer.Reset(ch.config.Interval)
log.DebugContext(ctx, "Interval of latency check updated", "interval", ch.config.Interval.String())
ch.Mutex.Unlock()
case <-timer.C:
res := ch.check(ctx)
cResult <- checks.ResultDTO{
Expand All @@ -109,7 +104,7 @@ func (ch *check) UpdateConfig(cfg checks.Runtime) error {
if c, ok := cfg.(*Config); ok {
ch.Mutex.Lock()
defer ch.Mutex.Unlock()
if reflect.DeepEqual(ch.config, *c) {
if c == nil || reflect.DeepEqual(&ch.config, c) {
return nil
}

Expand All @@ -123,7 +118,6 @@ func (ch *check) UpdateConfig(cfg checks.Runtime) error {
}

ch.config = *c
ch.Update <- struct{}{}
return nil
}

Expand Down
10 changes: 2 additions & 8 deletions pkg/checks/traceroute/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type check struct {
func NewCheck() checks.Check {
c := &check{
Base: checks.NewBase(),
config: Config{},
config: Config{Retry: checks.DefaultRetry},
traceroute: TraceRoute,
metrics: newMetrics(),
}
Expand Down Expand Up @@ -105,11 +105,6 @@ func (ch *check) Run(ctx context.Context, cResult chan checks.ResultDTO) error {
return ctx.Err()
case <-ch.Done:
return nil
case <-ch.Update:
ch.Mutex.Lock()
timer.Reset(ch.config.Interval)
log.DebugContext(ctx, "Interval of traceroute check updated", "interval", ch.config.Interval.String())
ch.Mutex.Unlock()
case <-timer.C:
res := ch.check(ctx)
ch.metrics.MinHops(res)
Expand Down Expand Up @@ -229,7 +224,7 @@ func (ch *check) UpdateConfig(cfg checks.Runtime) error {
if c, ok := cfg.(*Config); ok {
ch.Mutex.Lock()
defer ch.Mutex.Unlock()
if reflect.DeepEqual(ch.config, *c) {
if c == nil || reflect.DeepEqual(&ch.config, c) {
return nil
}

Expand All @@ -243,7 +238,6 @@ func (ch *check) UpdateConfig(cfg checks.Runtime) error {
}

ch.config = *c
ch.Update <- struct{}{}
return nil
}

Expand Down

0 comments on commit fc2aa54

Please sign in to comment.