mirror of
https://github.com/elyby/chrly.git
synced 2024-12-23 13:40:11 +05:30
Implemented error expiring for batch mojang uuids provider response checker
This commit is contained in:
parent
677f48ff3f
commit
153efdcce6
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
mutex.Unlock()
|
||||
if expireTimer != nil {
|
||||
expireTimer.Stop()
|
||||
}
|
||||
|
||||
expireTimer = time.AfterFunc(resetDuration, func() {
|
||||
mutex.Lock()
|
||||
lastCallErr = nil
|
||||
mutex.Unlock()
|
||||
})
|
||||
},
|
||||
)
|
||||
|
||||
|
@ -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()))
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user