diff --git a/README.md b/README.md index 766fa4f..5534bc4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ### resilience4go -[![Build](https://github.com/CharLemAznable/gofn/actions/workflows/go.yml/badge.svg)](https://github.com/CharLemAznable/resilience4go/actions/workflows/go.yml) +[![Build](https://github.com/CharLemAznable/resilience4go/actions/workflows/go.yml/badge.svg)](https://github.com/CharLemAznable/resilience4go/actions/workflows/go.yml) ![GitHub release (latest by date)](https://img.shields.io/github/v/release/CharLemAznable/resilience4go) [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php) diff --git a/bulkhead/decorate.go b/bulkhead/decorate.go index a858b67..2116c62 100644 --- a/bulkhead/decorate.go +++ b/bulkhead/decorate.go @@ -1,6 +1,6 @@ package bulkhead -import "github.com/CharLemAznable/gofn/common" +import "github.com/CharLemAznable/ge" func DecorateRunnable(bulkhead Bulkhead, fn func() error) func() error { return func() error { @@ -15,7 +15,7 @@ func DecorateRunnable(bulkhead Bulkhead, fn func() error) func() error { func DecorateSupplier[T any](bulkhead Bulkhead, fn func() (T, error)) func() (T, error) { return func() (T, error) { if err := bulkhead.acquire(); err != nil { - return common.Zero[T](), err + return ge.Zero[T](), err } defer bulkhead.release() return fn() @@ -35,7 +35,7 @@ func DecorateConsumer[T any](bulkhead Bulkhead, fn func(T) error) func(T) error func DecorateFunction[T any, R any](bulkhead Bulkhead, fn func(T) (R, error)) func(T) (R, error) { return func(t T) (R, error) { if err := bulkhead.acquire(); err != nil { - return common.Zero[R](), err + return ge.Zero[R](), err } defer bulkhead.release() return fn(t) diff --git a/bulkhead/event_listener.go b/bulkhead/event_listener.go index ca76ac5..aba12ac 100644 --- a/bulkhead/event_listener.go +++ b/bulkhead/event_listener.go @@ -1,7 +1,7 @@ package bulkhead import ( - "github.com/CharLemAznable/resilience4go/utils" + "github.com/CharLemAznable/ge" "sync" ) @@ -30,21 +30,21 @@ type eventListener struct { func (listener *eventListener) OnPermitted(consumer func(PermittedEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onPermitted = utils.AppendElementUnique(listener.onPermitted, consumer) + listener.onPermitted = ge.AppendElementUnique(listener.onPermitted, consumer) return listener } func (listener *eventListener) OnRejected(consumer func(RejectedEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onRejected = utils.AppendElementUnique(listener.onRejected, consumer) + listener.onRejected = ge.AppendElementUnique(listener.onRejected, consumer) return listener } func (listener *eventListener) OnFinished(consumer func(FinishedEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onFinished = utils.AppendElementUnique(listener.onFinished, consumer) + listener.onFinished = ge.AppendElementUnique(listener.onFinished, consumer) return listener } @@ -53,11 +53,11 @@ func (listener *eventListener) Dismiss(consumer any) EventListener { defer listener.Unlock() switch c := consumer.(type) { case func(PermittedEvent): - listener.onPermitted = utils.RemoveElementByValue(listener.onPermitted, c) + listener.onPermitted = ge.RemoveElementByValue(listener.onPermitted, c) case func(RejectedEvent): - listener.onRejected = utils.RemoveElementByValue(listener.onRejected, c) + listener.onRejected = ge.RemoveElementByValue(listener.onRejected, c) case func(FinishedEvent): - listener.onFinished = utils.RemoveElementByValue(listener.onFinished, c) + listener.onFinished = ge.RemoveElementByValue(listener.onFinished, c) } return listener } @@ -68,11 +68,11 @@ func (listener *eventListener) consumeEvent(event Event) { defer listener.RUnlock() switch e := event.(type) { case *permittedEvent: - utils.ConsumeEvent(listener.onPermitted, PermittedEvent(e)) + ge.ConsumeEach(listener.onPermitted, PermittedEvent(e)) case *rejectedEvent: - utils.ConsumeEvent(listener.onRejected, RejectedEvent(e)) + ge.ConsumeEach(listener.onRejected, RejectedEvent(e)) case *finishedEvent: - utils.ConsumeEvent(listener.onFinished, FinishedEvent(e)) + ge.ConsumeEach(listener.onFinished, FinishedEvent(e)) } }() } diff --git a/cache/cache.go b/cache/cache.go index 21a8b60..66dac6f 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -1,7 +1,7 @@ package cache import ( - "github.com/CharLemAznable/gofn/common" + "github.com/CharLemAznable/ge" "github.com/dgraph-io/ristretto" "sync" ) @@ -29,7 +29,7 @@ func NewCache[K any, V any](name string, configs ...ConfigBuilder) Cache[K, V] { KeyToHash: config.keyToHashFn, IgnoreInternalCost: true, }) - common.PanicIfError(err) + ge.PanicIfError(err) c := &cache[K, V]{ name: name, config: config, @@ -83,14 +83,14 @@ func (c *cache[K, V]) GetOrLoad(key K, loader func(K) (V, error)) (V, error) { if v, found := c.ristrettoCache.Get(key); found { c.eventListener.consumeEvent(newCacheHitEvent(c.name, key)) - vv, err := common.Cast[*valueWithError](v) - common.PanicIfError(err) + vv, err := ge.Cast[*valueWithError](v) + ge.PanicIfError(err) vvv := vv.value if c.unmarshalFn != nil { vvv = c.unmarshalFn(vv.value) } - value, err := common.CastOrZero[V](vvv) - common.PanicIfError(err) + value, err := ge.CastOrZero[V](vvv) + ge.PanicIfError(err) return value, vv.error } c.eventListener.consumeEvent(newCacheMissEvent(c.name, key)) diff --git a/cache/event_listener.go b/cache/event_listener.go index 154299c..a1babaf 100644 --- a/cache/event_listener.go +++ b/cache/event_listener.go @@ -1,7 +1,7 @@ package cache import ( - "github.com/CharLemAznable/resilience4go/utils" + "github.com/CharLemAznable/ge" "sync" ) @@ -27,14 +27,14 @@ type eventListener struct { func (listener *eventListener) OnCacheHit(consumer func(HitEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onCacheHit = utils.AppendElementUnique(listener.onCacheHit, consumer) + listener.onCacheHit = ge.AppendElementUnique(listener.onCacheHit, consumer) return listener } func (listener *eventListener) OnCacheMiss(consumer func(MissEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onCacheMiss = utils.AppendElementUnique(listener.onCacheMiss, consumer) + listener.onCacheMiss = ge.AppendElementUnique(listener.onCacheMiss, consumer) return listener } @@ -43,9 +43,9 @@ func (listener *eventListener) Dismiss(consumer any) EventListener { defer listener.Unlock() switch c := consumer.(type) { case func(HitEvent): - listener.onCacheHit = utils.RemoveElementByValue(listener.onCacheHit, c) + listener.onCacheHit = ge.RemoveElementByValue(listener.onCacheHit, c) case func(MissEvent): - listener.onCacheMiss = utils.RemoveElementByValue(listener.onCacheMiss, c) + listener.onCacheMiss = ge.RemoveElementByValue(listener.onCacheMiss, c) } return listener } @@ -56,9 +56,9 @@ func (listener *eventListener) consumeEvent(event Event) { defer listener.RUnlock() switch e := event.(type) { case *hitEvent: - utils.ConsumeEvent(listener.onCacheHit, HitEvent(e)) + ge.ConsumeEach(listener.onCacheHit, HitEvent(e)) case *missEvent: - utils.ConsumeEvent(listener.onCacheMiss, MissEvent(e)) + ge.ConsumeEach(listener.onCacheMiss, MissEvent(e)) } }() } diff --git a/circuitbreaker/circuitbreaker.go b/circuitbreaker/circuitbreaker.go index 48d1721..662e8d9 100644 --- a/circuitbreaker/circuitbreaker.go +++ b/circuitbreaker/circuitbreaker.go @@ -2,7 +2,7 @@ package circuitbreaker import ( "fmt" - "github.com/CharLemAznable/gofn/common" + "github.com/CharLemAznable/ge" "sync/atomic" "time" ) @@ -119,7 +119,7 @@ func (machine *stateMachine) execute(fn func() (any, error)) (any, error) { } start := time.Now() finished := make(chan *channelValue) - panicked := make(common.Panicked) + panicked := make(ge.Panicked) go func() { defer panicked.Recover() ret, err := fn() @@ -130,7 +130,7 @@ func (machine *stateMachine) execute(fn func() (any, error)) (any, error) { machine.onResult(start, result.ret, result.err) return result.ret, result.err case err := <-panicked.Caught(): - machine.onResult(start, nil, common.WrapPanic(err)) + machine.onResult(start, nil, ge.WrapPanic(err)) panic(err) } } diff --git a/circuitbreaker/config.go b/circuitbreaker/config.go index e450541..1ac1b31 100644 --- a/circuitbreaker/config.go +++ b/circuitbreaker/config.go @@ -2,7 +2,7 @@ package circuitbreaker import ( "fmt" - "github.com/CharLemAznable/resilience4go/utils" + "github.com/CharLemAznable/ge" "time" ) @@ -71,7 +71,7 @@ func WithSlidingWindow(slidingWindowType SlidingWindowType, slidingWindowSize in config.slidingWindowType = slidingWindowType config.slidingWindowSize = slidingWindowSize if CountBased == slidingWindowType { - config.minimumNumberOfCalls = utils.Min(minimumNumberOfCalls, slidingWindowSize) + config.minimumNumberOfCalls = ge.Min(minimumNumberOfCalls, slidingWindowSize) } else { config.minimumNumberOfCalls = minimumNumberOfCalls } diff --git a/circuitbreaker/decorate.go b/circuitbreaker/decorate.go index e33efd0..58aa904 100644 --- a/circuitbreaker/decorate.go +++ b/circuitbreaker/decorate.go @@ -1,6 +1,6 @@ package circuitbreaker -import "github.com/CharLemAznable/gofn/common" +import "github.com/CharLemAznable/ge" func DecorateRunnable(breaker CircuitBreaker, fn func() error) func() error { return func() error { @@ -16,7 +16,7 @@ func DecorateSupplier[T any](breaker CircuitBreaker, fn func() (T, error)) func( ret, err := breaker.execute(func() (any, error) { return fn() }) - return common.CastQuietly[T](ret), err + return ge.CastQuietly[T](ret), err } } @@ -34,6 +34,6 @@ func DecorateFunction[T any, R any](breaker CircuitBreaker, fn func(T) (R, error ret, err := breaker.execute(func() (any, error) { return fn(t) }) - return common.CastQuietly[R](ret), err + return ge.CastQuietly[R](ret), err } } diff --git a/circuitbreaker/event_listener.go b/circuitbreaker/event_listener.go index 25311ff..f5c2777 100644 --- a/circuitbreaker/event_listener.go +++ b/circuitbreaker/event_listener.go @@ -1,7 +1,7 @@ package circuitbreaker import ( - "github.com/CharLemAznable/resilience4go/utils" + "github.com/CharLemAznable/ge" "sync" ) @@ -39,42 +39,42 @@ type eventListener struct { func (listener *eventListener) OnSuccess(consumer func(SuccessEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onSuccess = utils.AppendElementUnique(listener.onSuccess, consumer) + listener.onSuccess = ge.AppendElementUnique(listener.onSuccess, consumer) return listener } func (listener *eventListener) OnError(consumer func(ErrorEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onError = utils.AppendElementUnique(listener.onError, consumer) + listener.onError = ge.AppendElementUnique(listener.onError, consumer) return listener } func (listener *eventListener) OnNotPermitted(consumer func(NotPermittedEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onNotPermitted = utils.AppendElementUnique(listener.onNotPermitted, consumer) + listener.onNotPermitted = ge.AppendElementUnique(listener.onNotPermitted, consumer) return listener } func (listener *eventListener) OnStateTransition(consumer func(StateTransitionEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onStateTransition = utils.AppendElementUnique(listener.onStateTransition, consumer) + listener.onStateTransition = ge.AppendElementUnique(listener.onStateTransition, consumer) return listener } func (listener *eventListener) OnFailureRateExceeded(consumer func(FailureRateExceededEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onFailureRateExceeded = utils.AppendElementUnique(listener.onFailureRateExceeded, consumer) + listener.onFailureRateExceeded = ge.AppendElementUnique(listener.onFailureRateExceeded, consumer) return listener } func (listener *eventListener) OnSlowCallRateExceeded(consumer func(SlowCallRateExceededEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onSlowCallRateExceeded = utils.AppendElementUnique(listener.onSlowCallRateExceeded, consumer) + listener.onSlowCallRateExceeded = ge.AppendElementUnique(listener.onSlowCallRateExceeded, consumer) return listener } @@ -83,17 +83,17 @@ func (listener *eventListener) Dismiss(consumer any) EventListener { defer listener.Unlock() switch c := consumer.(type) { case func(SuccessEvent): - listener.onSuccess = utils.RemoveElementByValue(listener.onSuccess, c) + listener.onSuccess = ge.RemoveElementByValue(listener.onSuccess, c) case func(ErrorEvent): - listener.onError = utils.RemoveElementByValue(listener.onError, c) + listener.onError = ge.RemoveElementByValue(listener.onError, c) case func(NotPermittedEvent): - listener.onNotPermitted = utils.RemoveElementByValue(listener.onNotPermitted, c) + listener.onNotPermitted = ge.RemoveElementByValue(listener.onNotPermitted, c) case func(StateTransitionEvent): - listener.onStateTransition = utils.RemoveElementByValue(listener.onStateTransition, c) + listener.onStateTransition = ge.RemoveElementByValue(listener.onStateTransition, c) case func(FailureRateExceededEvent): - listener.onFailureRateExceeded = utils.RemoveElementByValue(listener.onFailureRateExceeded, c) + listener.onFailureRateExceeded = ge.RemoveElementByValue(listener.onFailureRateExceeded, c) case func(SlowCallRateExceededEvent): - listener.onSlowCallRateExceeded = utils.RemoveElementByValue(listener.onSlowCallRateExceeded, c) + listener.onSlowCallRateExceeded = ge.RemoveElementByValue(listener.onSlowCallRateExceeded, c) } return listener } @@ -104,17 +104,17 @@ func (listener *eventListener) consumeEvent(event Event) { defer listener.RUnlock() switch e := event.(type) { case *successEvent: - utils.ConsumeEvent(listener.onSuccess, SuccessEvent(e)) + ge.ConsumeEach(listener.onSuccess, SuccessEvent(e)) case *errorEvent: - utils.ConsumeEvent(listener.onError, ErrorEvent(e)) + ge.ConsumeEach(listener.onError, ErrorEvent(e)) case *notPermittedEvent: - utils.ConsumeEvent(listener.onNotPermitted, NotPermittedEvent(e)) + ge.ConsumeEach(listener.onNotPermitted, NotPermittedEvent(e)) case *stateTransitionEvent: - utils.ConsumeEvent(listener.onStateTransition, StateTransitionEvent(e)) + ge.ConsumeEach(listener.onStateTransition, StateTransitionEvent(e)) case *failureRateExceededEvent: - utils.ConsumeEvent(listener.onFailureRateExceeded, FailureRateExceededEvent(e)) + ge.ConsumeEach(listener.onFailureRateExceeded, FailureRateExceededEvent(e)) case *slowCallRateExceededEvent: - utils.ConsumeEvent(listener.onSlowCallRateExceeded, SlowCallRateExceededEvent(e)) + ge.ConsumeEach(listener.onSlowCallRateExceeded, SlowCallRateExceededEvent(e)) } }() } diff --git a/circuitbreaker/metrics.go b/circuitbreaker/metrics.go index 4ded95b..7c608c1 100644 --- a/circuitbreaker/metrics.go +++ b/circuitbreaker/metrics.go @@ -1,7 +1,7 @@ package circuitbreaker import ( - "github.com/CharLemAznable/resilience4go/utils" + "github.com/CharLemAznable/ge" "sync" "sync/atomic" "time" @@ -27,7 +27,7 @@ func newMetrics(slidingWindowSize int64, slidingWindowType SlidingWindowType, co } if CountBased == slidingWindowType { m.recorder = newFixedSizeSlidingWindowRecorder(slidingWindowSize) - m.minimumNumberOfCalls = utils.Min(config.minimumNumberOfCalls, slidingWindowSize) + m.minimumNumberOfCalls = ge.Min(config.minimumNumberOfCalls, slidingWindowSize) } else { m.recorder = newSlidingTimeWindowRecorder(slidingWindowSize) m.minimumNumberOfCalls = config.minimumNumberOfCalls @@ -282,7 +282,7 @@ func (s *slidingTimeWindowRecorder) moveWindowToCurrentEpochSecond(latestPartial if differenceInSeconds == 0 { return latestPartialAggregation } - secondsToMoveTheWindow := utils.Min(differenceInSeconds, s.timeWindowSizeInSeconds) + secondsToMoveTheWindow := ge.Min(differenceInSeconds, s.timeWindowSizeInSeconds) var currentPartialAggregation *partialAggregation for secondsToMoveTheWindow > 0 { secondsToMoveTheWindow-- diff --git a/fallback/fallback.go b/fallback/fallback.go index 6ce0890..871c57e 100644 --- a/fallback/fallback.go +++ b/fallback/fallback.go @@ -1,11 +1,11 @@ package fallback -import "github.com/CharLemAznable/gofn/common" +import "github.com/CharLemAznable/ge" func execute[T any, R any](param T, fn func() (R, error)) Context[T, R, error] { ctx := &context[T, R, error]{param: param} finished := make(chan *context[T, R, error]) - panicked := make(common.Panicked) + panicked := make(ge.Panicked) go func() { defer panicked.Recover() ctx.ret, ctx.err = fn() diff --git a/go.mod b/go.mod index 5fcc406..4275151 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/CharLemAznable/resilience4go go 1.20 require ( - github.com/CharLemAznable/gofn v0.2.2 + github.com/CharLemAznable/ge v0.1.0 github.com/dgraph-io/ristretto v0.1.1 github.com/prometheus/client_golang v1.17.0 github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 diff --git a/ratelimiter/decorate.go b/ratelimiter/decorate.go index 829a300..eaa75e2 100644 --- a/ratelimiter/decorate.go +++ b/ratelimiter/decorate.go @@ -1,6 +1,6 @@ package ratelimiter -import "github.com/CharLemAznable/gofn/common" +import "github.com/CharLemAznable/ge" func DecorateRunnable(limiter RateLimiter, fn func() error) func() error { return func() error { @@ -14,7 +14,7 @@ func DecorateRunnable(limiter RateLimiter, fn func() error) func() error { func DecorateSupplier[T any](limiter RateLimiter, fn func() (T, error)) func() (T, error) { return func() (T, error) { if err := limiter.acquirePermission(); err != nil { - return common.Zero[T](), err + return ge.Zero[T](), err } return fn() } @@ -32,7 +32,7 @@ func DecorateConsumer[T any](limiter RateLimiter, fn func(T) error) func(T) erro func DecorateFunction[T any, R any](limiter RateLimiter, fn func(T) (R, error)) func(T) (R, error) { return func(t T) (R, error) { if err := limiter.acquirePermission(); err != nil { - return common.Zero[R](), err + return ge.Zero[R](), err } return fn(t) } diff --git a/ratelimiter/event_listener.go b/ratelimiter/event_listener.go index 3f34688..38bb53d 100644 --- a/ratelimiter/event_listener.go +++ b/ratelimiter/event_listener.go @@ -1,7 +1,7 @@ package ratelimiter import ( - "github.com/CharLemAznable/resilience4go/utils" + "github.com/CharLemAznable/ge" "sync" ) @@ -27,14 +27,14 @@ type eventListener struct { func (listener *eventListener) OnSuccess(consumer func(SuccessEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onSuccess = utils.AppendElementUnique(listener.onSuccess, consumer) + listener.onSuccess = ge.AppendElementUnique(listener.onSuccess, consumer) return listener } func (listener *eventListener) OnFailure(consumer func(FailureEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onFailure = utils.AppendElementUnique(listener.onFailure, consumer) + listener.onFailure = ge.AppendElementUnique(listener.onFailure, consumer) return listener } @@ -43,9 +43,9 @@ func (listener *eventListener) Dismiss(consumer any) EventListener { defer listener.Unlock() switch c := consumer.(type) { case func(SuccessEvent): - listener.onSuccess = utils.RemoveElementByValue(listener.onSuccess, c) + listener.onSuccess = ge.RemoveElementByValue(listener.onSuccess, c) case func(FailureEvent): - listener.onFailure = utils.RemoveElementByValue(listener.onFailure, c) + listener.onFailure = ge.RemoveElementByValue(listener.onFailure, c) } return listener } @@ -56,9 +56,9 @@ func (listener *eventListener) consumeEvent(event Event) { defer listener.RUnlock() switch e := event.(type) { case *successEvent: - utils.ConsumeEvent(listener.onSuccess, SuccessEvent(e)) + ge.ConsumeEach(listener.onSuccess, SuccessEvent(e)) case *failureEvent: - utils.ConsumeEvent(listener.onFailure, FailureEvent(e)) + ge.ConsumeEach(listener.onFailure, FailureEvent(e)) } }() } diff --git a/ratelimiter/ratelimiter.go b/ratelimiter/ratelimiter.go index 0f64887..b127ee2 100644 --- a/ratelimiter/ratelimiter.go +++ b/ratelimiter/ratelimiter.go @@ -2,7 +2,7 @@ package ratelimiter import ( "fmt" - "github.com/CharLemAznable/resilience4go/utils" + "github.com/CharLemAznable/ge" "sync/atomic" "time" ) @@ -105,7 +105,7 @@ func (limiter *atomicRateLimiter) calculateNextState(timeoutInNanos int64, activ elapsedCycles := currentCycle - nextCycle accumulatedPermissions := elapsedCycles * permissionsPerCycle nextCycle = currentCycle - nextPermissions = utils.Min(nextPermissions+accumulatedPermissions, permissionsPerCycle) + nextPermissions = ge.Min(nextPermissions+accumulatedPermissions, permissionsPerCycle) } nextNanosToWait := nanosToWaitForPermission(cyclePeriodInNanos, permissionsPerCycle, nextPermissions, currentNanos, currentCycle) diff --git a/retry/decorate.go b/retry/decorate.go index 07d317e..981e584 100644 --- a/retry/decorate.go +++ b/retry/decorate.go @@ -1,6 +1,6 @@ package retry -import "github.com/CharLemAznable/gofn/common" +import "github.com/CharLemAznable/ge" func DecorateRunnable(retry Retry, fn func() error) func() error { return func() error { @@ -16,7 +16,7 @@ func DecorateSupplier[T any](retry Retry, fn func() (T, error)) func() (T, error ret, err := retry.execute(func() (any, error) { return fn() }) - return common.CastQuietly[T](ret), err + return ge.CastQuietly[T](ret), err } } @@ -34,6 +34,6 @@ func DecorateFunction[T any, R any](retry Retry, fn func(T) (R, error)) func(T) ret, err := retry.execute(func() (any, error) { return fn(t) }) - return common.CastQuietly[R](ret), err + return ge.CastQuietly[R](ret), err } } diff --git a/retry/event_listener.go b/retry/event_listener.go index 9a2c004..98b9a29 100644 --- a/retry/event_listener.go +++ b/retry/event_listener.go @@ -1,7 +1,7 @@ package retry import ( - "github.com/CharLemAznable/resilience4go/utils" + "github.com/CharLemAznable/ge" "sync" ) @@ -30,21 +30,21 @@ type eventListener struct { func (listener *eventListener) OnSuccess(consumer func(SuccessEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onSuccess = utils.AppendElementUnique(listener.onSuccess, consumer) + listener.onSuccess = ge.AppendElementUnique(listener.onSuccess, consumer) return listener } func (listener *eventListener) OnRetry(consumer func(RetryEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onRetry = utils.AppendElementUnique(listener.onRetry, consumer) + listener.onRetry = ge.AppendElementUnique(listener.onRetry, consumer) return listener } func (listener *eventListener) OnError(consumer func(ErrorEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onError = utils.AppendElementUnique(listener.onError, consumer) + listener.onError = ge.AppendElementUnique(listener.onError, consumer) return listener } @@ -53,11 +53,11 @@ func (listener *eventListener) Dismiss(consumer any) EventListener { defer listener.Unlock() switch c := consumer.(type) { case func(SuccessEvent): - listener.onSuccess = utils.RemoveElementByValue(listener.onSuccess, c) + listener.onSuccess = ge.RemoveElementByValue(listener.onSuccess, c) case func(RetryEvent): - listener.onRetry = utils.RemoveElementByValue(listener.onRetry, c) + listener.onRetry = ge.RemoveElementByValue(listener.onRetry, c) case func(ErrorEvent): - listener.onError = utils.RemoveElementByValue(listener.onError, c) + listener.onError = ge.RemoveElementByValue(listener.onError, c) } return listener } @@ -68,11 +68,11 @@ func (listener *eventListener) consumeEvent(event Event) { defer listener.RUnlock() switch e := event.(type) { case *successEvent: - utils.ConsumeEvent(listener.onSuccess, SuccessEvent(e)) + ge.ConsumeEach(listener.onSuccess, SuccessEvent(e)) case *retryEvent: - utils.ConsumeEvent(listener.onRetry, RetryEvent(e)) + ge.ConsumeEach(listener.onRetry, RetryEvent(e)) case *errorEvent: - utils.ConsumeEvent(listener.onError, ErrorEvent(e)) + ge.ConsumeEach(listener.onError, ErrorEvent(e)) } }() } diff --git a/retry/retry.go b/retry/retry.go index 3a9d055..ff767fb 100644 --- a/retry/retry.go +++ b/retry/retry.go @@ -2,7 +2,7 @@ package retry import ( "fmt" - "github.com/CharLemAznable/gofn/common" + "github.com/CharLemAznable/ge" "time" ) @@ -70,7 +70,7 @@ func (r *retry) execute(fn func() (any, error)) (any, error) { r.publishEvent(newErrorEvent(r.name, numOfAttempts, context.ret, context.err)) if r.config.failAfterMaxAttempts { - context.err = common.DefaultErrorFn(context.err, func() error { + context.err = ge.DefaultErrorFn(context.err, func() error { return &MaxRetriesExceeded{name: r.name, maxAttempts: r.config.maxAttempts} }) } @@ -84,7 +84,7 @@ func (r *retry) execute(fn func() (any, error)) (any, error) { func (r *retry) executeOnce(fn func() (any, error)) *channelValue { finished := make(chan *channelValue) - panicked := make(common.Panicked) + panicked := make(ge.Panicked) go func() { defer panicked.Recover() ret, err := fn() @@ -94,7 +94,7 @@ func (r *retry) executeOnce(fn func() (any, error)) *channelValue { case result := <-finished: return result case err := <-panicked.Caught(): - return &channelValue{nil, common.WrapPanic(err), err} + return &channelValue{nil, ge.WrapPanic(err), err} } } diff --git a/timelimiter/decorate.go b/timelimiter/decorate.go index 9616835..b1406f5 100644 --- a/timelimiter/decorate.go +++ b/timelimiter/decorate.go @@ -1,6 +1,6 @@ package timelimiter -import "github.com/CharLemAznable/gofn/common" +import "github.com/CharLemAznable/ge" func DecorateRunnable(limiter TimeLimiter, fn func() error) func() error { return func() error { @@ -16,7 +16,7 @@ func DecorateSupplier[T any](limiter TimeLimiter, fn func() (T, error)) func() ( ret, err := limiter.execute(func() (any, error) { return fn() }) - return common.CastQuietly[T](ret), err + return ge.CastQuietly[T](ret), err } } @@ -34,6 +34,6 @@ func DecorateFunction[T any, R any](limiter TimeLimiter, fn func(T) (R, error)) ret, err := limiter.execute(func() (any, error) { return fn(t) }) - return common.CastQuietly[R](ret), err + return ge.CastQuietly[R](ret), err } } diff --git a/timelimiter/event_listener.go b/timelimiter/event_listener.go index de7c461..b662530 100644 --- a/timelimiter/event_listener.go +++ b/timelimiter/event_listener.go @@ -1,7 +1,7 @@ package timelimiter import ( - "github.com/CharLemAznable/resilience4go/utils" + "github.com/CharLemAznable/ge" "sync" ) @@ -30,21 +30,21 @@ type eventListener struct { func (listener *eventListener) OnSuccess(consumer func(SuccessEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onSuccess = utils.AppendElementUnique(listener.onSuccess, consumer) + listener.onSuccess = ge.AppendElementUnique(listener.onSuccess, consumer) return listener } func (listener *eventListener) OnTimeout(consumer func(TimeoutEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onTimeout = utils.AppendElementUnique(listener.onTimeout, consumer) + listener.onTimeout = ge.AppendElementUnique(listener.onTimeout, consumer) return listener } func (listener *eventListener) OnPanic(consumer func(PanicEvent)) EventListener { listener.Lock() defer listener.Unlock() - listener.onPanic = utils.AppendElementUnique(listener.onPanic, consumer) + listener.onPanic = ge.AppendElementUnique(listener.onPanic, consumer) return listener } @@ -53,11 +53,11 @@ func (listener *eventListener) Dismiss(consumer any) EventListener { defer listener.Unlock() switch c := consumer.(type) { case func(SuccessEvent): - listener.onSuccess = utils.RemoveElementByValue(listener.onSuccess, c) + listener.onSuccess = ge.RemoveElementByValue(listener.onSuccess, c) case func(TimeoutEvent): - listener.onTimeout = utils.RemoveElementByValue(listener.onTimeout, c) + listener.onTimeout = ge.RemoveElementByValue(listener.onTimeout, c) case func(PanicEvent): - listener.onPanic = utils.RemoveElementByValue(listener.onPanic, c) + listener.onPanic = ge.RemoveElementByValue(listener.onPanic, c) } return listener } @@ -68,11 +68,11 @@ func (listener *eventListener) consumeEvent(event Event) { defer listener.RUnlock() switch e := event.(type) { case *successEvent: - utils.ConsumeEvent(listener.onSuccess, SuccessEvent(e)) + ge.ConsumeEach(listener.onSuccess, SuccessEvent(e)) case *timeoutEvent: - utils.ConsumeEvent(listener.onTimeout, TimeoutEvent(e)) + ge.ConsumeEach(listener.onTimeout, TimeoutEvent(e)) case *panicEvent: - utils.ConsumeEvent(listener.onPanic, PanicEvent(e)) + ge.ConsumeEach(listener.onPanic, PanicEvent(e)) } }() } diff --git a/timelimiter/timelimiter.go b/timelimiter/timelimiter.go index ca3d08f..8d7e17a 100644 --- a/timelimiter/timelimiter.go +++ b/timelimiter/timelimiter.go @@ -3,7 +3,7 @@ package timelimiter import ( "context" "fmt" - "github.com/CharLemAznable/gofn/common" + "github.com/CharLemAznable/ge" ) type TimeLimiter interface { @@ -52,7 +52,7 @@ func (limiter *timeLimiter) execute(fn func() (any, error)) (any, error) { timeout, cancelFunc := context.WithTimeout(limiter.rootContext, limiter.config.timeoutDuration) defer cancelFunc() finished := make(chan *channelValue) - panicked := make(common.Panicked) + panicked := make(ge.Panicked) go func() { defer panicked.Recover() ret, err := fn() diff --git a/utils/math.go b/utils/math.go deleted file mode 100644 index 9ff0cd1..0000000 --- a/utils/math.go +++ /dev/null @@ -1,8 +0,0 @@ -package utils - -func Min(x, y int64) int64 { - if x > y { - return y - } - return x -} diff --git a/utils/math_test.go b/utils/math_test.go deleted file mode 100644 index 5f8b978..0000000 --- a/utils/math_test.go +++ /dev/null @@ -1,29 +0,0 @@ -package utils_test - -import ( - "github.com/CharLemAznable/resilience4go/utils" - "testing" -) - -func TestMin(t *testing.T) { - tests := []struct { - x, y, want int64 - }{ - {3, 5, 3}, - {5, 3, 3}, - {0, 0, 0}, - {-5, -3, -5}, - {-3, -5, -5}, - {9223372036854775807, 9223372036854775807, 9223372036854775807}, - {-9223372036854775808, -9223372036854775808, -9223372036854775808}, - {9223372036854775807, -9223372036854775808, -9223372036854775808}, - {-9223372036854775808, 9223372036854775807, -9223372036854775808}, - } - - for _, test := range tests { - got := utils.Min(test.x, test.y) - if got != test.want { - t.Errorf("Min(%d, %d) = %d; want %d", test.x, test.y, got, test.want) - } - } -} diff --git a/utils/slice.go b/utils/slice.go deleted file mode 100644 index 3e056c4..0000000 --- a/utils/slice.go +++ /dev/null @@ -1,32 +0,0 @@ -package utils - -import "reflect" - -func AppendElementUnique[T any](slice []T, value T) []T { - var removed []T = RemoveElementByValue(slice, value) - return append(removed, value) -} - -func RemoveElementByValue[T any](slice []T, value T) []T { - result := make([]T, 0) - start, cursor := 0, 0 - for ; cursor < len(slice); cursor++ { - if EqualsPointer(slice[cursor], value) { - if start != cursor { - result = append(result, slice[start:cursor]...) - } - start = cursor + 1 - } - } - return append(result, slice[start:cursor]...) -} - -func EqualsPointer[T any](x, y T) bool { - return reflect.ValueOf(x).Pointer() == reflect.ValueOf(y).Pointer() -} - -func ConsumeEvent[T any](consumers []func(T), event T) { - for _, consumer := range consumers { - go consumer(event) - } -} diff --git a/utils/slice_test.go b/utils/slice_test.go deleted file mode 100644 index 7305edb..0000000 --- a/utils/slice_test.go +++ /dev/null @@ -1,65 +0,0 @@ -package utils_test - -import ( - "github.com/CharLemAznable/resilience4go/utils" - "sync" - "sync/atomic" - "testing" -) - -type testFn func() - -func TestRemoveElementByValue_Func(t *testing.T) { - fn1 := func() {} - fn2 := func() {} - slice := []testFn{fn1, fn2, fn2, fn1} - result := utils.RemoveElementByValue(slice, fn2) - expected := []testFn{fn1, fn1} - if !slicesEqualFunc(result, expected) { - t.Errorf("Expected %v, but got %v", expected, result) - } - fn3 := func() {} - result = utils.RemoveElementByValue(slice, fn3) - expected = []testFn{fn1, fn2, fn2, fn1} - if !slicesEqualFunc(result, expected) { - t.Errorf("Expected %v, but got %v", expected, result) - } - result = utils.AppendElementUnique(slice, fn2) - expected = []testFn{fn1, fn1, fn2} - if !slicesEqualFunc(result, expected) { - t.Errorf("Expected %v, but got %v", expected, result) - } -} - -func slicesEqualFunc(a, b []testFn) bool { - if len(a) != len(b) { - return false - } - for i, v := range a { - if !utils.EqualsPointer(v, b[i]) { - return false - } - } - return true -} - -func TestConsumeEvent(t *testing.T) { - wg := &sync.WaitGroup{} - wg.Add(1) - var sum atomic.Int64 - fn1 := func(i int) { - sum.Add(int64(i)) - wg.Done() - } - wg.Add(1) - fn2 := func(i int) { - sum.Add(int64(i)) - wg.Done() - } - slice := []func(int){fn1, fn2} - utils.ConsumeEvent(slice, 2) - wg.Wait() - if sum.Load() != 4 { - t.Errorf("Expected sum 4, but got %d", sum.Load()) - } -}