You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.3 KiB
56 lines
1.3 KiB
package gomicro
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/afex/hystrix-go/hystrix"
|
|
"github.com/micro/go-micro/v2/client"
|
|
)
|
|
|
|
type HystrixWrapper struct {
|
|
client.Client
|
|
fuseResponse
|
|
}
|
|
|
|
type fuseResponse func(rsp interface{}) error
|
|
|
|
func NewHystrixWrapper(c client.Client) client.Client {
|
|
return &HystrixWrapper{Client: c}
|
|
}
|
|
|
|
func (c *HystrixWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
|
name := req.Service() + "." + req.Endpoint()
|
|
|
|
hystrix.ConfigureCommand(name,
|
|
hystrix.CommandConfig{
|
|
Timeout: 5000, // 超时时间(毫秒)
|
|
MaxConcurrentRequests: 1000,
|
|
ErrorPercentThreshold: 25,
|
|
SleepWindow: 5000,
|
|
})
|
|
|
|
return hystrix.Do(name, func() error {
|
|
return c.Client.Call(ctx, req, rsp)
|
|
}, func(e error) error {
|
|
if c.fuseResponse != nil {
|
|
return c.fuseResponse(rsp)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
/*func SendCircuitMetrics() {
|
|
hystrixStreamHandler := hystrix.NewStreamHandler()
|
|
hystrixStreamHandler.Start()
|
|
go http.ListenAndServe(net.JoinHostPort("", "81"), hystrixStreamHandler)
|
|
|
|
c, err := plugins.InitializeStatsdCollector(&plugins.StatsdCollectorConfig{
|
|
StatsdAddr: "localhost:8125",
|
|
Prefix: "myapp.hystrix",
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("could not initialize statsd client: %v", err)
|
|
}
|
|
|
|
metricCollector.Registry.Register(c.NewStatsdCollector)
|
|
}*/
|