-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(healthcheck): Add better exporter health check
Better logic around the exporter health. Related #110 Signed-off-by: oluwole fadeyi <[email protected]>
- Loading branch information
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package barrier | ||
|
||
import ( | ||
"context" | ||
"github.com/juju/errors" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type ( | ||
Barrier struct { | ||
Capacity int | ||
Value int | ||
RefillPeriod time.Duration | ||
mutex sync.RWMutex | ||
} | ||
) | ||
|
||
func New(cap int, rate time.Duration) *Barrier { | ||
return &Barrier{ | ||
Capacity: cap, | ||
Value: cap, | ||
RefillPeriod: rate, | ||
} | ||
} | ||
|
||
func (b *Barrier) RecordBadEvent() { | ||
b.mutex.Lock() | ||
defer b.mutex.Unlock() | ||
if b.Value <= 0 { | ||
return | ||
} | ||
b.Value = b.Value - 1 | ||
} | ||
|
||
func (b *Barrier) IsBadState() bool { | ||
b.mutex.Lock() | ||
defer b.mutex.Unlock() | ||
return b.Value == 0 | ||
} | ||
|
||
func (b *Barrier) refill() { | ||
b.mutex.Lock() | ||
defer b.mutex.Unlock() | ||
if b.Value == b.Capacity { | ||
return | ||
} | ||
b.Value = b.Value + 1 | ||
} | ||
|
||
func (b *Barrier) Start(ctx context.Context) error { | ||
if b.Value < 0 { | ||
return errors.New("The instance cannot be initialised with a negative number") | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-time.Tick(b.RefillPeriod): | ||
b.refill() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package barrier | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBarrier(t *testing.T) { | ||
t.Parallel() | ||
t.Run("successfully record bad event", func(t *testing.T) { | ||
b := New(10, 1*time.Second) | ||
b.RecordBadEvent() | ||
assert.Equal(t, 9, b.Value) | ||
}) | ||
t.Run("errors if the instance capacity is set to a negative number", func(t *testing.T) { | ||
ctx := context.Background() | ||
b := New(-1, 1*time.Second) | ||
assert.Error(t, b.Start(ctx)) | ||
}) | ||
t.Run("record bad event doesn't go to a negative number", func(t *testing.T) { | ||
b := New(0, 1*time.Second) | ||
b.RecordBadEvent() | ||
assert.Equal(t, 0, b.Value) | ||
}) | ||
t.Run("isBadState returns true when instance reaches 0", func(t *testing.T) { | ||
b := New(0, 1*time.Second) | ||
assert.True(t, true, b.IsBadState()) | ||
}) | ||
t.Run("successfully refill barrier at the refill rate", func(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
b := New(10, 1*time.Second) | ||
go func() { | ||
require.NoError(t, b.Start(ctx)) | ||
}() | ||
b.RecordBadEvent() | ||
b.RecordBadEvent() | ||
assert.Equal(t, 8, b.Value) | ||
time.Sleep(3 * time.Second) | ||
cancel() | ||
assert.Equal(t, 10, b.Value) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters