Implemented error expiring for batch mojang uuids provider response checker

This commit is contained in:
ErickSkrauch 2020-04-10 15:19:00 +03:00
parent 677f48ff3f
commit 153efdcce6
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
3 changed files with 35 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"time"
"github.com/etherlabsio/healthcheck"
"github.com/mono83/slf/wd"
@ -55,7 +56,10 @@ var workerCmd = &cobra.Command{
handler.Handle("/healthcheck", healthcheck.Handler(
healthcheck.WithChecker(
"mojang-batch-uuids-provider-response",
eventsubscribers.MojangBatchUuidsProviderChecker(dispatcher),
eventsubscribers.MojangBatchUuidsProviderChecker(
dispatcher,
viper.GetDuration("healthcheck.mojang_batch_uuids_provider_cool_down"),
),
),
)).Methods("GET")
@ -80,4 +84,5 @@ var workerCmd = &cobra.Command{
func init() {
RootCmd.AddCommand(workerCmd)
viper.SetDefault("healthcheck.mojang_batch_uuids_provider_cool_down", time.Minute)
}

View File

@ -3,21 +3,33 @@ package eventsubscribers
import (
"context"
"sync"
"time"
"github.com/etherlabsio/healthcheck"
"github.com/elyby/chrly/api/mojang"
)
func MojangBatchUuidsProviderChecker(dispatcher Subscriber) healthcheck.CheckerFunc {
func MojangBatchUuidsProviderChecker(dispatcher Subscriber, resetDuration time.Duration) healthcheck.CheckerFunc {
var mutex sync.Mutex
var lastCallErr error // TODO: need to reset this value after some time
var lastCallErr error
var expireTimer *time.Timer
dispatcher.Subscribe(
"mojang_textures:batch_uuids_provider:result",
func(usernames []string, profiles []*mojang.ProfileInfo, err error) {
mutex.Lock()
defer mutex.Unlock()
lastCallErr = err
if expireTimer != nil {
expireTimer.Stop()
}
expireTimer = time.AfterFunc(resetDuration, func() {
mutex.Lock()
lastCallErr = nil
mutex.Unlock()
})
},
)

View File

@ -4,6 +4,7 @@ import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
@ -14,22 +15,32 @@ import (
func TestMojangBatchUuidsProviderChecker(t *testing.T) {
t.Run("empty state", func(t *testing.T) {
dispatcher := bootstrap.CreateEventDispatcher()
checker := MojangBatchUuidsProviderChecker(dispatcher)
checker := MojangBatchUuidsProviderChecker(dispatcher, time.Millisecond)
assert.Nil(t, checker(context.Background()))
})
t.Run("when no error occurred", func(t *testing.T) {
dispatcher := bootstrap.CreateEventDispatcher()
checker := MojangBatchUuidsProviderChecker(dispatcher)
checker := MojangBatchUuidsProviderChecker(dispatcher, time.Millisecond)
dispatcher.Emit("mojang_textures:batch_uuids_provider:result", []string{"username"}, []*mojang.ProfileInfo{}, nil)
assert.Nil(t, checker(context.Background()))
})
t.Run("when error occurred", func(t *testing.T) {
dispatcher := bootstrap.CreateEventDispatcher()
checker := MojangBatchUuidsProviderChecker(dispatcher)
checker := MojangBatchUuidsProviderChecker(dispatcher, time.Millisecond)
err := errors.New("some error occurred")
dispatcher.Emit("mojang_textures:batch_uuids_provider:result", []string{"username"}, nil, err)
assert.Equal(t, err, checker(context.Background()))
})
t.Run("should reset value after passed duration", func(t *testing.T) {
dispatcher := bootstrap.CreateEventDispatcher()
checker := MojangBatchUuidsProviderChecker(dispatcher, 20*time.Millisecond)
err := errors.New("some error occurred")
dispatcher.Emit("mojang_textures:batch_uuids_provider:result", []string{"username"}, nil, err)
assert.Equal(t, err, checker(context.Background()))
time.Sleep(40 * time.Millisecond)
assert.Nil(t, checker(context.Background()))
})
}