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"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/etherlabsio/healthcheck"
|
"github.com/etherlabsio/healthcheck"
|
||||||
"github.com/mono83/slf/wd"
|
"github.com/mono83/slf/wd"
|
||||||
@ -55,7 +56,10 @@ var workerCmd = &cobra.Command{
|
|||||||
handler.Handle("/healthcheck", healthcheck.Handler(
|
handler.Handle("/healthcheck", healthcheck.Handler(
|
||||||
healthcheck.WithChecker(
|
healthcheck.WithChecker(
|
||||||
"mojang-batch-uuids-provider-response",
|
"mojang-batch-uuids-provider-response",
|
||||||
eventsubscribers.MojangBatchUuidsProviderChecker(dispatcher),
|
eventsubscribers.MojangBatchUuidsProviderChecker(
|
||||||
|
dispatcher,
|
||||||
|
viper.GetDuration("healthcheck.mojang_batch_uuids_provider_cool_down"),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
)).Methods("GET")
|
)).Methods("GET")
|
||||||
|
|
||||||
@ -80,4 +84,5 @@ var workerCmd = &cobra.Command{
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RootCmd.AddCommand(workerCmd)
|
RootCmd.AddCommand(workerCmd)
|
||||||
|
viper.SetDefault("healthcheck.mojang_batch_uuids_provider_cool_down", time.Minute)
|
||||||
}
|
}
|
||||||
|
@ -3,21 +3,33 @@ package eventsubscribers
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/etherlabsio/healthcheck"
|
"github.com/etherlabsio/healthcheck"
|
||||||
|
|
||||||
"github.com/elyby/chrly/api/mojang"
|
"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 mutex sync.Mutex
|
||||||
var lastCallErr error // TODO: need to reset this value after some time
|
var lastCallErr error
|
||||||
|
var expireTimer *time.Timer
|
||||||
dispatcher.Subscribe(
|
dispatcher.Subscribe(
|
||||||
"mojang_textures:batch_uuids_provider:result",
|
"mojang_textures:batch_uuids_provider:result",
|
||||||
func(usernames []string, profiles []*mojang.ProfileInfo, err error) {
|
func(usernames []string, profiles []*mojang.ProfileInfo, err error) {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
|
defer mutex.Unlock()
|
||||||
|
|
||||||
lastCallErr = err
|
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"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
@ -14,22 +15,32 @@ import (
|
|||||||
func TestMojangBatchUuidsProviderChecker(t *testing.T) {
|
func TestMojangBatchUuidsProviderChecker(t *testing.T) {
|
||||||
t.Run("empty state", func(t *testing.T) {
|
t.Run("empty state", func(t *testing.T) {
|
||||||
dispatcher := bootstrap.CreateEventDispatcher()
|
dispatcher := bootstrap.CreateEventDispatcher()
|
||||||
checker := MojangBatchUuidsProviderChecker(dispatcher)
|
checker := MojangBatchUuidsProviderChecker(dispatcher, time.Millisecond)
|
||||||
assert.Nil(t, checker(context.Background()))
|
assert.Nil(t, checker(context.Background()))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("when no error occurred", func(t *testing.T) {
|
t.Run("when no error occurred", func(t *testing.T) {
|
||||||
dispatcher := bootstrap.CreateEventDispatcher()
|
dispatcher := bootstrap.CreateEventDispatcher()
|
||||||
checker := MojangBatchUuidsProviderChecker(dispatcher)
|
checker := MojangBatchUuidsProviderChecker(dispatcher, time.Millisecond)
|
||||||
dispatcher.Emit("mojang_textures:batch_uuids_provider:result", []string{"username"}, []*mojang.ProfileInfo{}, nil)
|
dispatcher.Emit("mojang_textures:batch_uuids_provider:result", []string{"username"}, []*mojang.ProfileInfo{}, nil)
|
||||||
assert.Nil(t, checker(context.Background()))
|
assert.Nil(t, checker(context.Background()))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("when error occurred", func(t *testing.T) {
|
t.Run("when error occurred", func(t *testing.T) {
|
||||||
dispatcher := bootstrap.CreateEventDispatcher()
|
dispatcher := bootstrap.CreateEventDispatcher()
|
||||||
checker := MojangBatchUuidsProviderChecker(dispatcher)
|
checker := MojangBatchUuidsProviderChecker(dispatcher, time.Millisecond)
|
||||||
err := errors.New("some error occurred")
|
err := errors.New("some error occurred")
|
||||||
dispatcher.Emit("mojang_textures:batch_uuids_provider:result", []string{"username"}, nil, err)
|
dispatcher.Emit("mojang_textures:batch_uuids_provider:result", []string{"username"}, nil, err)
|
||||||
assert.Equal(t, err, checker(context.Background()))
|
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