circuitbreaker

package
v1.7.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Copyright (C) 2019-2024 vdaas.org vald team <vald@vdaas.org>

Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (C) 2019-2024 vdaas.org vald team <vald@vdaas.org>

Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (C) 2019-2024 vdaas.org vald team <vald@vdaas.org>

Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (C) 2019-2024 vdaas.org vald team <vald@vdaas.org>

Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (C) 2019-2024 vdaas.org vald team <vald@vdaas.org>

Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (C) 2019-2024 vdaas.org vald team <vald@vdaas.org>

Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Metrics

func Metrics(context.Context) (ms map[string]map[State]int64)

Types

type BreakerOption

type BreakerOption func(*breaker) error

func WithClosedErrorRate

func WithClosedErrorRate(f float32) BreakerOption

WithClosedErrorRate returns an option that sets error rate when breaker state is "Closed". The rate is expected to be between 0 and 1.0. When the rate is exceeded, the breaker state will be changed from "Closed" to "Open".

func WithClosedErrorTripper

func WithClosedErrorTripper(tp Tripper) BreakerOption

WithClosedErrorTripper returns an option that sets whether it should trip when in "Closed" state.

func WithClosedRefreshTimeout

func WithClosedRefreshTimeout(timeout string) BreakerOption

WithClosedRefreshTimeout returns an option that sets the timeout of "Closed" state. After this period, the counter will be refreshed.

func WithHalfOpenErrorRate

func WithHalfOpenErrorRate(f float32) BreakerOption

WithHalfOpenErrorRate returns an option that sets error rate when breaker state is "HalfOpen". The rate is expected to be between 0 and 1.0. When the rate is exceeded, the breaker state will be changed from "HalfOpen" to "Open".

func WithHalfOpenErrorTripper

func WithHalfOpenErrorTripper(tp Tripper) BreakerOption

WithHalfOpenErrorTripper returns an option that sets whether it should trip when in "Half-Open" state.

func WithMinSamples

func WithMinSamples(min int64) BreakerOption

WithMinSamples returns an option that sets minimum sample count.

func WithOpenTimeout

func WithOpenTimeout(timeout string) BreakerOption

WithOpenTimeout returns an option that sets the timeout of "Open" state. After this period, the state will be changed from "Open" to "HalfOpen".

type CircuitBreaker

type CircuitBreaker interface {
	Do(ctx context.Context, key string, fn func(ctx context.Context) (any, error)) (val any, err error)
}

CircuitBreaker is a state machine to prevent doing processes that are likely to fail.

func NewCircuitBreaker

func NewCircuitBreaker(opts ...Option) (CircuitBreaker, error)

NewCircuitBreaker returns CircuitBreaker object if no error occurs.

type Counter

type Counter interface {
	Total() int64
	Successes() int64
	Fails() int64
	Ignores() int64
}

type Option

type Option func(*breakerManager) error

func WithBreakerOpts

func WithBreakerOpts(opts ...BreakerOption) Option

type State

type State uint32
const (
	StateUnknown State = iota
	StateClosed
	StateOpen
	StateHalfOpen
)

func (State) String

func (s State) String() string

type Tripper

type Tripper interface {
	ShouldTrip(Counter) bool
}

Tripper is a function type to determine if the CircuitBreaker should trip.

func NewRateTripper

func NewRateTripper(rate float32, min int64) Tripper

type TripperFunc

type TripperFunc func(Counter) bool

func (TripperFunc) ShouldTrip

func (f TripperFunc) ShouldTrip(c Counter) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL