hystrix

package module
v0.0.0-...-fa6fafb Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

Micro Hystrix Client Wrapper

A go-micro plugin for go-hystrix.

Usage

package main

import (
	"github.com/smart-echo/micro"
	hystrix "github.com/go-micro/plugins/v4/wrapper/breaker/hystrix"
)

func main() {
	service := micro.NewService(micro.WrapClient(hystrix.NewClientWrapper()))
	service.Init(micro.Name("test.srv"), micro.Address(":80"))
	if err := service.Run(); err != nil {
		panic(err)
	}
}

Filter

package main

import (
	"github.com/smart-echo/micro"
	hystrix "github.com/go-micro/plugins/v4/wrapper/breaker/hystrix"
)

func main() {
	service := micro.NewService(micro.WrapClient(hystrix.NewClientWrapper(hystrix.WithFilter(func(c context.Context, e error) error {
			if e == ErrLetItPass {
				return true 
			}
			return false
	}))))
	service.Init(micro.Name("test.srv"), micro.Address(":80"))
	if err := service.Run(); err != nil {
		panic(err)
	}
}

Default Configure in hystrix

var (
	// DefaultTimeout is how long to wait for command to complete, in milliseconds
	DefaultTimeout = 1000
	// DefaultMaxConcurrent is how many commands of the same type can run at the same time
	DefaultMaxConcurrent = 10
	// DefaultVolumeThreshold is the minimum number of requests needed before a circuit can be tripped due to health
	DefaultVolumeThreshold = 20
	// DefaultSleepWindow is how long, in milliseconds, to wait after a circuit opens before testing for recovery
	DefaultSleepWindow = 5000
	// DefaultErrorPercentThreshold causes circuits to open once the rolling measure of errors exceeds this percent of requests
	DefaultErrorPercentThreshold = 50
)

Update default config in hystrix

package main

import (
	"github.com/smart-echo/micro"
	hystrix "github.com/go-micro/plugins/v4/wrapper/breaker/hystrix"
)

func main() {
	hystrix.ConfigureDefault(hystrix.CommandConfig{Timeout: 1000})
	service := micro.NewService(micro.WrapClient(hystrix.NewClientWrapper()))
	service.Init(micro.Name("test.srv"), micro.Address(":80"))
	if err := service.Run(); err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure

func Configure(cmds map[string]CommandConfig)

Configure applies settings for a set of circuits.

func ConfigureCommand

func ConfigureCommand(name string, config CommandConfig)

ConfigureCommand applies settings for a circuit.

func ConfigureDefault

func ConfigureDefault(config CommandConfig)

ConfigureDefault applies default settings for all circuits.

func NewClientWrapper

func NewClientWrapper(opts ...Option) client.Wrapper

NewClientWrapper returns a hystrix client Wrapper.

Types

type CommandConfig

type CommandConfig struct {
	Timeout                int
	MaxConcurrentRequests  int
	RequestVolumeThreshold int
	SleepWindow            int
	ErrorPercentThreshold  int
}

CommandConfig is used to tune circuit settings at runtime.

type Option

type Option func(*Options)

Option represents options update func.

func WithFallback

func WithFallback(fallback func(context.Context, error) error) Option

WithFallback used to set fallback func for options.

func WithFilter

func WithFilter(filter func(context.Context, error) bool) Option

WithFilter used to set filter func for options.

type Options

type Options struct {
	// Filter used to prevent errors from trigger circuit breaker.
	// return true if you want to ignore target error
	Filter func(context.Context, error) bool
	// Fallback used to define some code to execute during outages.
	Fallback func(context.Context, error) error
}

Options represents hystrix client wrapper options.

Jump to

Keyboard shortcuts

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