diff --git a/codegen/template_bundle/template_files.go b/codegen/template_bundle/template_files.go index 007487a2e..6bc395b06 100644 --- a/codegen/template_bundle/template_files.go +++ b/codegen/template_bundle/template_files.go @@ -1269,6 +1269,18 @@ func {{$exportName}}(deps *module.Dependencies) Client { {{- end -}} {{- end}} } + + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.{{$clientID}}.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.circuitBreakerDisabled") + } + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutInMS, methodNames[methodKey]) + } + } + return &{{$clientName}}{ {{range $i, $s := $services -}} {{camel $s.Name}}Client: gen.New{{pascal $s.Name}}YARPCClient(oc), @@ -1281,21 +1293,13 @@ func {{$exportName}}(deps *module.Dependencies) Client { "{{$clientID}}", routingKey, requestUUIDHeaderKey, - !configureCircuitBreaker(deps, timeoutInMS), + circuitBreakerDisabled, timeoutInMS, ), } } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.{{$clientID}}.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.circuitBreakerDisabled") - } - if circuitBreakerDisabled { - return false - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -1319,15 +1323,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.{{$clientID}}.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.{{$clientID}}.requestVolumeThreshold")) } - - hystrix.ConfigureCommand("{{$clientID}}", hystrix.CommandConfig{ + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ MaxConcurrentRequests: maxConcurrentRequests, ErrorPercentThreshold: errorPercentThreshold, SleepWindow: sleepWindowInMilliseconds, RequestVolumeThreshold: requestVolumeThreshold, Timeout: timeoutVal, }) - return true } {{range $i, $svc := .ProtoServices -}} @@ -1363,7 +1365,7 @@ func (e *{{$clientName}}) {{$methodName}}( if e.opts.CircuitBreakerDisabled { result, err = runFunc(ctx, request, opts...) } else { - err = hystrix.DoC(ctx, "{{$clientID}}", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "{{$methodName}}", func(ctx context.Context) error { result, err = runFunc(ctx, request, opts...) return err }, nil) @@ -1387,7 +1389,7 @@ func grpc_clientTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "grpc_client.tmpl", size: 6611, mode: os.FileMode(420), modTime: time.Unix(1, 0)} + info := bindataFileInfo{name: "grpc_client.tmpl", size: 6680, mode: os.FileMode(420), modTime: time.Unix(1, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -1503,8 +1505,21 @@ func {{$exportName}}(deps *module.Dependencies) Client { followRedirect = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.followRedirect") } - - circuitBreakerDisabled := configureCircuitBreaker(deps, timeoutVal) + methodNames := map[string]string{ + {{range $serviceMethod, $methodName := $exposedMethods -}} + "{{$methodName}}": "{{$serviceMethod}}", + {{end}} + } + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.{{$clientID}}.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.circuitBreakerDisabled") + } + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutVal, methodKey) + } + } return &{{$clientName}}{ clientID: "{{$clientID}}", @@ -1518,11 +1533,7 @@ func {{$exportName}}(deps *module.Dependencies) Client { httpClient: zanzibar.NewHTTPClientContext( deps.Default.ContextLogger, deps.Default.ContextMetrics, deps.Default.JSONWrapper, "{{$clientID}}", - map[string]string{ - {{range $serviceMethod, $methodName := $exposedMethods -}} - "{{$methodName}}": "{{$serviceMethod}}", - {{end}} - }, + methodNames, baseURL, defaultHeaders, timeout, @@ -1549,12 +1560,7 @@ func initializeAltRoutingMap(altServiceDetail config.AlternateServiceDetail) map } {{end -}} -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.{{$clientID}}.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.circuitBreakerDisabled") - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -1578,16 +1584,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.{{$clientID}}.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.{{$clientID}}.requestVolumeThreshold")) } - if !circuitBreakerDisabled { - hystrix.ConfigureCommand("{{$clientID}}", hystrix.CommandConfig{ + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ MaxConcurrentRequests: maxConcurrentRequests, ErrorPercentThreshold: errorPercentThreshold, SleepWindow: sleepWindowInMilliseconds, RequestVolumeThreshold: requestVolumeThreshold, Timeout: timeoutVal, }) - } - return circuitBreakerDisabled } // HTTPClient returns the underlying HTTP client, should only be @@ -1696,7 +1699,7 @@ func (c *{{$clientName}}) {{$methodName}}( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "{{$clientID}}", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "{{$methodName}}", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -1901,7 +1904,7 @@ func http_clientTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "http_client.tmpl", size: 16830, mode: os.FileMode(420), modTime: time.Unix(1, 0)} + info := bindataFileInfo{name: "http_client.tmpl", size: 16856, mode: os.FileMode(420), modTime: time.Unix(1, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -2911,7 +2914,17 @@ func {{$exportName}}(deps *module.Dependencies) Client { {{ end -}} } - circuitBreakerDisabled := configureCircuitBreaker(deps, timeoutVal) + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.{{$clientID}}.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.circuitBreakerDisabled") + } + + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutVal, methodNames[methodKey]) + } + } client := zanzibar.NewTChannelClientContext( deps.Default.Channel, @@ -2970,12 +2983,7 @@ func initializeDynamicChannel(deps *module.Dependencies, headerPatterns []string return headerPatterns, re } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.{{$clientID}}.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.circuitBreakerDisabled") - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -2999,16 +3007,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.{{$clientID}}.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.{{$clientID}}.requestVolumeThreshold")) } - if !circuitBreakerDisabled { - hystrix.ConfigureCommand("{{$clientID}}", hystrix.CommandConfig{ + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ MaxConcurrentRequests: maxConcurrentRequests, ErrorPercentThreshold: errorPercentThreshold, SleepWindow: sleepWindowInMilliseconds, RequestVolumeThreshold: requestVolumeThreshold, Timeout: timeoutVal, }) - } - return circuitBreakerDisabled } // {{$clientName}} is the TChannel client for downstream service. @@ -3056,7 +3061,7 @@ type {{$clientName}} struct { "methodName" : "{{$methodName}}", }) start := time.Now() - err = hystrix.DoC(ctx, "{{$clientID}}", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "{{$methodName}}", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -3123,7 +3128,7 @@ func tchannel_clientTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "tchannel_client.tmpl", size: 11749, mode: os.FileMode(420), modTime: time.Unix(1, 0)} + info := bindataFileInfo{name: "tchannel_client.tmpl", size: 11764, mode: os.FileMode(420), modTime: time.Unix(1, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/codegen/templates/grpc_client.tmpl b/codegen/templates/grpc_client.tmpl index 8f4926fed..2255b83ac 100644 --- a/codegen/templates/grpc_client.tmpl +++ b/codegen/templates/grpc_client.tmpl @@ -69,6 +69,18 @@ func {{$exportName}}(deps *module.Dependencies) Client { {{- end -}} {{- end}} } + + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.{{$clientID}}.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.circuitBreakerDisabled") + } + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutInMS, methodNames[methodKey]) + } + } + return &{{$clientName}}{ {{range $i, $s := $services -}} {{camel $s.Name}}Client: gen.New{{pascal $s.Name}}YARPCClient(oc), @@ -81,21 +93,13 @@ func {{$exportName}}(deps *module.Dependencies) Client { "{{$clientID}}", routingKey, requestUUIDHeaderKey, - !configureCircuitBreaker(deps, timeoutInMS), + circuitBreakerDisabled, timeoutInMS, ), } } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.{{$clientID}}.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.circuitBreakerDisabled") - } - if circuitBreakerDisabled { - return false - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -119,15 +123,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.{{$clientID}}.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.{{$clientID}}.requestVolumeThreshold")) } - - hystrix.ConfigureCommand("{{$clientID}}", hystrix.CommandConfig{ + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ MaxConcurrentRequests: maxConcurrentRequests, ErrorPercentThreshold: errorPercentThreshold, SleepWindow: sleepWindowInMilliseconds, RequestVolumeThreshold: requestVolumeThreshold, Timeout: timeoutVal, }) - return true } {{range $i, $svc := .ProtoServices -}} @@ -163,7 +165,7 @@ func (e *{{$clientName}}) {{$methodName}}( if e.opts.CircuitBreakerDisabled { result, err = runFunc(ctx, request, opts...) } else { - err = hystrix.DoC(ctx, "{{$clientID}}", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "{{$methodName}}", func(ctx context.Context) error { result, err = runFunc(ctx, request, opts...) return err }, nil) diff --git a/codegen/templates/http_client.tmpl b/codegen/templates/http_client.tmpl index 3d5e469bf..05d68b245 100644 --- a/codegen/templates/http_client.tmpl +++ b/codegen/templates/http_client.tmpl @@ -109,8 +109,21 @@ func {{$exportName}}(deps *module.Dependencies) Client { followRedirect = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.followRedirect") } - - circuitBreakerDisabled := configureCircuitBreaker(deps, timeoutVal) + methodNames := map[string]string{ + {{range $serviceMethod, $methodName := $exposedMethods -}} + "{{$methodName}}": "{{$serviceMethod}}", + {{end}} + } + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.{{$clientID}}.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.circuitBreakerDisabled") + } + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutVal, methodKey) + } + } return &{{$clientName}}{ clientID: "{{$clientID}}", @@ -124,11 +137,7 @@ func {{$exportName}}(deps *module.Dependencies) Client { httpClient: zanzibar.NewHTTPClientContext( deps.Default.ContextLogger, deps.Default.ContextMetrics, deps.Default.JSONWrapper, "{{$clientID}}", - map[string]string{ - {{range $serviceMethod, $methodName := $exposedMethods -}} - "{{$methodName}}": "{{$serviceMethod}}", - {{end}} - }, + methodNames, baseURL, defaultHeaders, timeout, @@ -155,12 +164,7 @@ func initializeAltRoutingMap(altServiceDetail config.AlternateServiceDetail) map } {{end -}} -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.{{$clientID}}.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.circuitBreakerDisabled") - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -184,16 +188,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.{{$clientID}}.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.{{$clientID}}.requestVolumeThreshold")) } - if !circuitBreakerDisabled { - hystrix.ConfigureCommand("{{$clientID}}", hystrix.CommandConfig{ + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ MaxConcurrentRequests: maxConcurrentRequests, ErrorPercentThreshold: errorPercentThreshold, SleepWindow: sleepWindowInMilliseconds, RequestVolumeThreshold: requestVolumeThreshold, Timeout: timeoutVal, }) - } - return circuitBreakerDisabled } // HTTPClient returns the underlying HTTP client, should only be @@ -302,7 +303,7 @@ func (c *{{$clientName}}) {{$methodName}}( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "{{$clientID}}", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "{{$methodName}}", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded diff --git a/codegen/templates/tchannel_client.tmpl b/codegen/templates/tchannel_client.tmpl index cfe329f6f..4ce66f267 100644 --- a/codegen/templates/tchannel_client.tmpl +++ b/codegen/templates/tchannel_client.tmpl @@ -127,7 +127,17 @@ func {{$exportName}}(deps *module.Dependencies) Client { {{ end -}} } - circuitBreakerDisabled := configureCircuitBreaker(deps, timeoutVal) + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.{{$clientID}}.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.circuitBreakerDisabled") + } + + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutVal, methodNames[methodKey]) + } + } client := zanzibar.NewTChannelClientContext( deps.Default.Channel, @@ -186,12 +196,7 @@ func initializeDynamicChannel(deps *module.Dependencies, headerPatterns []string return headerPatterns, re } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.{{$clientID}}.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.{{$clientID}}.circuitBreakerDisabled") - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -215,16 +220,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.{{$clientID}}.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.{{$clientID}}.requestVolumeThreshold")) } - if !circuitBreakerDisabled { - hystrix.ConfigureCommand("{{$clientID}}", hystrix.CommandConfig{ + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ MaxConcurrentRequests: maxConcurrentRequests, ErrorPercentThreshold: errorPercentThreshold, SleepWindow: sleepWindowInMilliseconds, RequestVolumeThreshold: requestVolumeThreshold, Timeout: timeoutVal, }) - } - return circuitBreakerDisabled } // {{$clientName}} is the TChannel client for downstream service. @@ -272,7 +274,7 @@ type {{$clientName}} struct { "methodName" : "{{$methodName}}", }) start := time.Now() - err = hystrix.DoC(ctx, "{{$clientID}}", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "{{$methodName}}", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( diff --git a/examples/example-gateway/build/clients/bar/bar.go b/examples/example-gateway/build/clients/bar/bar.go index b6f353a5e..fa2974a53 100644 --- a/examples/example-gateway/build/clients/bar/bar.go +++ b/examples/example-gateway/build/clients/bar/bar.go @@ -258,50 +258,60 @@ func NewClient(deps *module.Dependencies) Client { followRedirect = deps.Default.Config.MustGetBoolean("clients.bar.followRedirect") } - circuitBreakerDisabled := configureCircuitBreaker(deps, timeoutVal) + methodNames := map[string]string{ + "ArgNotStruct": "Bar::argNotStruct", + "ArgWithHeaders": "Bar::argWithHeaders", + "ArgWithManyQueryParams": "Bar::argWithManyQueryParams", + "ArgWithNearDupQueryParams": "Bar::argWithNearDupQueryParams", + "ArgWithNestedQueryParams": "Bar::argWithNestedQueryParams", + "ArgWithParams": "Bar::argWithParams", + "ArgWithParamsAndDuplicateFields": "Bar::argWithParamsAndDuplicateFields", + "ArgWithQueryHeader": "Bar::argWithQueryHeader", + "ArgWithQueryParams": "Bar::argWithQueryParams", + "DeleteFoo": "Bar::deleteFoo", + "DeleteWithBody": "Bar::deleteWithBody", + "DeleteWithQueryParams": "Bar::deleteWithQueryParams", + "Hello": "Bar::helloWorld", + "ListAndEnum": "Bar::listAndEnum", + "MissingArg": "Bar::missingArg", + "NoRequest": "Bar::noRequest", + "Normal": "Bar::normal", + "NormalRecur": "Bar::normalRecur", + "TooManyArgs": "Bar::tooManyArgs", + "EchoBinary": "Echo::echoBinary", + "EchoBool": "Echo::echoBool", + "EchoDouble": "Echo::echoDouble", + "EchoEnum": "Echo::echoEnum", + "EchoI16": "Echo::echoI16", + "EchoI32": "Echo::echoI32", + "EchoI32Map": "Echo::echoI32Map", + "EchoI64": "Echo::echoI64", + "EchoI8": "Echo::echoI8", + "EchoString": "Echo::echoString", + "EchoStringList": "Echo::echoStringList", + "EchoStringMap": "Echo::echoStringMap", + "EchoStringSet": "Echo::echoStringSet", + "EchoStructList": "Echo::echoStructList", + "EchoStructSet": "Echo::echoStructSet", + "EchoTypedef": "Echo::echoTypedef", + } + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.bar.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.bar.circuitBreakerDisabled") + } + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutVal, methodKey) + } + } return &barClient{ clientID: "bar", httpClient: zanzibar.NewHTTPClientContext( deps.Default.ContextLogger, deps.Default.ContextMetrics, deps.Default.JSONWrapper, "bar", - map[string]string{ - "ArgNotStruct": "Bar::argNotStruct", - "ArgWithHeaders": "Bar::argWithHeaders", - "ArgWithManyQueryParams": "Bar::argWithManyQueryParams", - "ArgWithNearDupQueryParams": "Bar::argWithNearDupQueryParams", - "ArgWithNestedQueryParams": "Bar::argWithNestedQueryParams", - "ArgWithParams": "Bar::argWithParams", - "ArgWithParamsAndDuplicateFields": "Bar::argWithParamsAndDuplicateFields", - "ArgWithQueryHeader": "Bar::argWithQueryHeader", - "ArgWithQueryParams": "Bar::argWithQueryParams", - "DeleteFoo": "Bar::deleteFoo", - "DeleteWithBody": "Bar::deleteWithBody", - "DeleteWithQueryParams": "Bar::deleteWithQueryParams", - "Hello": "Bar::helloWorld", - "ListAndEnum": "Bar::listAndEnum", - "MissingArg": "Bar::missingArg", - "NoRequest": "Bar::noRequest", - "Normal": "Bar::normal", - "NormalRecur": "Bar::normalRecur", - "TooManyArgs": "Bar::tooManyArgs", - "EchoBinary": "Echo::echoBinary", - "EchoBool": "Echo::echoBool", - "EchoDouble": "Echo::echoDouble", - "EchoEnum": "Echo::echoEnum", - "EchoI16": "Echo::echoI16", - "EchoI32": "Echo::echoI32", - "EchoI32Map": "Echo::echoI32Map", - "EchoI64": "Echo::echoI64", - "EchoI8": "Echo::echoI8", - "EchoString": "Echo::echoString", - "EchoStringList": "Echo::echoStringList", - "EchoStringMap": "Echo::echoStringMap", - "EchoStringSet": "Echo::echoStringSet", - "EchoStructList": "Echo::echoStructList", - "EchoStructSet": "Echo::echoStructSet", - "EchoTypedef": "Echo::echoTypedef", - }, + methodNames, baseURL, defaultHeaders, timeout, @@ -313,12 +323,7 @@ func NewClient(deps *module.Dependencies) Client { } } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.bar.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.bar.circuitBreakerDisabled") - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -342,16 +347,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.bar.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.bar.requestVolumeThreshold")) } - if !circuitBreakerDisabled { - hystrix.ConfigureCommand("bar", hystrix.CommandConfig{ - MaxConcurrentRequests: maxConcurrentRequests, - ErrorPercentThreshold: errorPercentThreshold, - SleepWindow: sleepWindowInMilliseconds, - RequestVolumeThreshold: requestVolumeThreshold, - Timeout: timeoutVal, - }) - } - return circuitBreakerDisabled + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ + MaxConcurrentRequests: maxConcurrentRequests, + ErrorPercentThreshold: errorPercentThreshold, + SleepWindow: sleepWindowInMilliseconds, + RequestVolumeThreshold: requestVolumeThreshold, + Timeout: timeoutVal, + }) } // HTTPClient returns the underlying HTTP client, should only be @@ -393,7 +395,7 @@ func (c *barClient) ArgNotStruct( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "ArgNotStruct", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -498,7 +500,7 @@ func (c *barClient) ArgWithHeaders( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "ArgWithHeaders", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -678,7 +680,7 @@ func (c *barClient) ArgWithManyQueryParams( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "ArgWithManyQueryParams", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -780,7 +782,7 @@ func (c *barClient) ArgWithNearDupQueryParams( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "ArgWithNearDupQueryParams", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -906,7 +908,7 @@ func (c *barClient) ArgWithNestedQueryParams( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "ArgWithNestedQueryParams", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -991,7 +993,7 @@ func (c *barClient) ArgWithParams( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "ArgWithParams", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -1076,7 +1078,7 @@ func (c *barClient) ArgWithParamsAndDuplicateFields( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "ArgWithParamsAndDuplicateFields", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -1161,7 +1163,7 @@ func (c *barClient) ArgWithQueryHeader( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "ArgWithQueryHeader", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -1263,7 +1265,7 @@ func (c *barClient) ArgWithQueryParams( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "ArgWithQueryParams", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -1354,7 +1356,7 @@ func (c *barClient) DeleteFoo( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "DeleteFoo", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -1431,7 +1433,7 @@ func (c *barClient) DeleteWithBody( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "DeleteWithBody", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -1517,7 +1519,7 @@ func (c *barClient) DeleteWithQueryParams( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "DeleteWithQueryParams", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -1594,7 +1596,7 @@ func (c *barClient) Hello( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "Hello", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -1706,7 +1708,7 @@ func (c *barClient) ListAndEnum( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "ListAndEnum", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -1800,7 +1802,7 @@ func (c *barClient) MissingArg( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "MissingArg", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -1895,7 +1897,7 @@ func (c *barClient) NoRequest( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "NoRequest", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -1991,7 +1993,7 @@ func (c *barClient) Normal( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "Normal", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -2087,7 +2089,7 @@ func (c *barClient) NormalRecur( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "NormalRecur", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -2182,7 +2184,7 @@ func (c *barClient) TooManyArgs( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "TooManyArgs", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -2292,7 +2294,7 @@ func (c *barClient) EchoBinary( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoBinary", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -2375,7 +2377,7 @@ func (c *barClient) EchoBool( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoBool", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -2464,7 +2466,7 @@ func (c *barClient) EchoDouble( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoDouble", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -2553,7 +2555,7 @@ func (c *barClient) EchoEnum( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoEnum", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -2642,7 +2644,7 @@ func (c *barClient) EchoI16( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoI16", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -2731,7 +2733,7 @@ func (c *barClient) EchoI32( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoI32", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -2820,7 +2822,7 @@ func (c *barClient) EchoI32Map( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoI32Map", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -2909,7 +2911,7 @@ func (c *barClient) EchoI64( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoI64", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -2998,7 +3000,7 @@ func (c *barClient) EchoI8( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoI8", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -3087,7 +3089,7 @@ func (c *barClient) EchoString( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoString", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -3176,7 +3178,7 @@ func (c *barClient) EchoStringList( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoStringList", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -3265,7 +3267,7 @@ func (c *barClient) EchoStringMap( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoStringMap", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -3354,7 +3356,7 @@ func (c *barClient) EchoStringSet( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoStringSet", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -3443,7 +3445,7 @@ func (c *barClient) EchoStructList( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoStructList", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -3532,7 +3534,7 @@ func (c *barClient) EchoStructSet( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoStructSet", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -3621,7 +3623,7 @@ func (c *barClient) EchoTypedef( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "bar", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoTypedef", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded diff --git a/examples/example-gateway/build/clients/baz/baz.go b/examples/example-gateway/build/clients/baz/baz.go index d451ce24a..caf1146a7 100644 --- a/examples/example-gateway/build/clients/baz/baz.go +++ b/examples/example-gateway/build/clients/baz/baz.go @@ -268,7 +268,17 @@ func NewClient(deps *module.Dependencies) Client { "SimpleService::urlTest": "URLTest", } - circuitBreakerDisabled := configureCircuitBreaker(deps, timeoutVal) + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.baz.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.baz.circuitBreakerDisabled") + } + + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutVal, methodNames[methodKey]) + } + } client := zanzibar.NewTChannelClientContext( deps.Default.Channel, @@ -327,12 +337,7 @@ func initializeDynamicChannel(deps *module.Dependencies, headerPatterns []string return headerPatterns, re } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.baz.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.baz.circuitBreakerDisabled") - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -356,16 +361,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.baz.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.baz.requestVolumeThreshold")) } - if !circuitBreakerDisabled { - hystrix.ConfigureCommand("baz", hystrix.CommandConfig{ - MaxConcurrentRequests: maxConcurrentRequests, - ErrorPercentThreshold: errorPercentThreshold, - SleepWindow: sleepWindowInMilliseconds, - RequestVolumeThreshold: requestVolumeThreshold, - Timeout: timeoutVal, - }) - } - return circuitBreakerDisabled + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ + MaxConcurrentRequests: maxConcurrentRequests, + ErrorPercentThreshold: errorPercentThreshold, + SleepWindow: sleepWindowInMilliseconds, + RequestVolumeThreshold: requestVolumeThreshold, + Timeout: timeoutVal, + }) } // bazClient is the TChannel client for downstream service. @@ -401,7 +403,7 @@ func (c *bazClient) EchoBinary( "methodName": "EchoBinary", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoBinary", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -466,7 +468,7 @@ func (c *bazClient) EchoBool( "methodName": "EchoBool", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoBool", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -531,7 +533,7 @@ func (c *bazClient) EchoDouble( "methodName": "EchoDouble", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoDouble", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -596,7 +598,7 @@ func (c *bazClient) EchoEnum( "methodName": "EchoEnum", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoEnum", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -661,7 +663,7 @@ func (c *bazClient) EchoI16( "methodName": "EchoI16", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoI16", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -726,7 +728,7 @@ func (c *bazClient) EchoI32( "methodName": "EchoI32", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoI32", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -791,7 +793,7 @@ func (c *bazClient) EchoI64( "methodName": "EchoI64", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoI64", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -856,7 +858,7 @@ func (c *bazClient) EchoI8( "methodName": "EchoI8", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoI8", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -921,7 +923,7 @@ func (c *bazClient) EchoString( "methodName": "EchoString", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoString", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -986,7 +988,7 @@ func (c *bazClient) EchoStringList( "methodName": "EchoStringList", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoStringList", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1051,7 +1053,7 @@ func (c *bazClient) EchoStringMap( "methodName": "EchoStringMap", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoStringMap", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1116,7 +1118,7 @@ func (c *bazClient) EchoStringSet( "methodName": "EchoStringSet", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoStringSet", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1181,7 +1183,7 @@ func (c *bazClient) EchoStructList( "methodName": "EchoStructList", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoStructList", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1246,7 +1248,7 @@ func (c *bazClient) EchoStructSet( "methodName": "EchoStructSet", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoStructSet", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1311,7 +1313,7 @@ func (c *bazClient) EchoTypedef( "methodName": "EchoTypedef", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoTypedef", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1375,7 +1377,7 @@ func (c *bazClient) Call( "methodName": "Call", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "Call", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1435,7 +1437,7 @@ func (c *bazClient) Compare( "methodName": "Compare", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "Compare", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1504,7 +1506,7 @@ func (c *bazClient) GetProfile( "methodName": "GetProfile", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "GetProfile", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1571,7 +1573,7 @@ func (c *bazClient) HeaderSchema( "methodName": "HeaderSchema", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "HeaderSchema", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1640,7 +1642,7 @@ func (c *bazClient) Ping( "methodName": "Ping", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "Ping", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1704,7 +1706,7 @@ func (c *bazClient) DeliberateDiffNoop( "methodName": "DeliberateDiffNoop", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "DeliberateDiffNoop", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1765,7 +1767,7 @@ func (c *bazClient) TestUUID( "methodName": "TestUUID", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "TestUUID", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1823,7 +1825,7 @@ func (c *bazClient) Trans( "methodName": "Trans", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "Trans", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1892,7 +1894,7 @@ func (c *bazClient) TransHeaders( "methodName": "TransHeaders", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "TransHeaders", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -1961,7 +1963,7 @@ func (c *bazClient) TransHeadersNoReq( "methodName": "TransHeadersNoReq", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "TransHeadersNoReq", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -2028,7 +2030,7 @@ func (c *bazClient) TransHeadersType( "methodName": "TransHeadersType", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "TransHeadersType", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( @@ -2096,7 +2098,7 @@ func (c *bazClient) URLTest( "methodName": "URLTest", }) start := time.Now() - err = hystrix.DoC(ctx, "baz", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "URLTest", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( diff --git a/examples/example-gateway/build/clients/contacts/contacts.go b/examples/example-gateway/build/clients/contacts/contacts.go index 0a687a4d5..5bbdfed44 100644 --- a/examples/example-gateway/build/clients/contacts/contacts.go +++ b/examples/example-gateway/build/clients/contacts/contacts.go @@ -90,17 +90,27 @@ func NewClient(deps *module.Dependencies) Client { followRedirect = deps.Default.Config.MustGetBoolean("clients.contacts.followRedirect") } - circuitBreakerDisabled := configureCircuitBreaker(deps, timeoutVal) + methodNames := map[string]string{ + "SaveContacts": "Contacts::saveContacts", + "TestURLURL": "Contacts::testUrlUrl", + } + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.contacts.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.contacts.circuitBreakerDisabled") + } + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutVal, methodKey) + } + } return &contactsClient{ clientID: "contacts", httpClient: zanzibar.NewHTTPClientContext( deps.Default.ContextLogger, deps.Default.ContextMetrics, deps.Default.JSONWrapper, "contacts", - map[string]string{ - "SaveContacts": "Contacts::saveContacts", - "TestURLURL": "Contacts::testUrlUrl", - }, + methodNames, baseURL, defaultHeaders, timeout, @@ -112,12 +122,7 @@ func NewClient(deps *module.Dependencies) Client { } } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.contacts.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.contacts.circuitBreakerDisabled") - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -141,16 +146,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.contacts.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.contacts.requestVolumeThreshold")) } - if !circuitBreakerDisabled { - hystrix.ConfigureCommand("contacts", hystrix.CommandConfig{ - MaxConcurrentRequests: maxConcurrentRequests, - ErrorPercentThreshold: errorPercentThreshold, - SleepWindow: sleepWindowInMilliseconds, - RequestVolumeThreshold: requestVolumeThreshold, - Timeout: timeoutVal, - }) - } - return circuitBreakerDisabled + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ + MaxConcurrentRequests: maxConcurrentRequests, + ErrorPercentThreshold: errorPercentThreshold, + SleepWindow: sleepWindowInMilliseconds, + RequestVolumeThreshold: requestVolumeThreshold, + Timeout: timeoutVal, + }) } // HTTPClient returns the underlying HTTP client, should only be @@ -193,7 +195,7 @@ func (c *contactsClient) SaveContacts( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "contacts", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "SaveContacts", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -282,7 +284,7 @@ func (c *contactsClient) TestURLURL( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "contacts", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "TestURLURL", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded diff --git a/examples/example-gateway/build/clients/corge-http/corge-http.go b/examples/example-gateway/build/clients/corge-http/corge-http.go index 78f991ecb..e0a33d834 100644 --- a/examples/example-gateway/build/clients/corge-http/corge-http.go +++ b/examples/example-gateway/build/clients/corge-http/corge-http.go @@ -121,7 +121,22 @@ func NewClient(deps *module.Dependencies) Client { followRedirect = deps.Default.Config.MustGetBoolean("clients.corge-http.followRedirect") } - circuitBreakerDisabled := configureCircuitBreaker(deps, timeoutVal) + methodNames := map[string]string{ + "EchoString": "Corge::echoString", + "NoContent": "Corge::noContent", + "NoContentNoException": "Corge::noContentNoException", + "CorgeNoContentOnException": "Corge::noContentOnException", + } + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.corge-http.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.corge-http.circuitBreakerDisabled") + } + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutVal, methodKey) + } + } return &corgeHTTPClient{ clientID: "corge-http", @@ -133,12 +148,7 @@ func NewClient(deps *module.Dependencies) Client { httpClient: zanzibar.NewHTTPClientContext( deps.Default.ContextLogger, deps.Default.ContextMetrics, deps.Default.JSONWrapper, "corge-http", - map[string]string{ - "EchoString": "Corge::echoString", - "NoContent": "Corge::noContent", - "NoContentNoException": "Corge::noContentNoException", - "CorgeNoContentOnException": "Corge::noContentOnException", - }, + methodNames, baseURL, defaultHeaders, timeout, @@ -162,12 +172,7 @@ func initializeAltRoutingMap(altServiceDetail config.AlternateServiceDetail) map } return routingMap } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.corge-http.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.corge-http.circuitBreakerDisabled") - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -191,16 +196,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.corge-http.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.corge-http.requestVolumeThreshold")) } - if !circuitBreakerDisabled { - hystrix.ConfigureCommand("corge-http", hystrix.CommandConfig{ - MaxConcurrentRequests: maxConcurrentRequests, - ErrorPercentThreshold: errorPercentThreshold, - SleepWindow: sleepWindowInMilliseconds, - RequestVolumeThreshold: requestVolumeThreshold, - Timeout: timeoutVal, - }) - } - return circuitBreakerDisabled + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ + MaxConcurrentRequests: maxConcurrentRequests, + ErrorPercentThreshold: errorPercentThreshold, + SleepWindow: sleepWindowInMilliseconds, + RequestVolumeThreshold: requestVolumeThreshold, + Timeout: timeoutVal, + }) } // HTTPClient returns the underlying HTTP client, should only be @@ -263,7 +265,7 @@ func (c *corgeHTTPClient) EchoString( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "corge-http", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoString", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -366,7 +368,7 @@ func (c *corgeHTTPClient) NoContent( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "corge-http", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "NoContent", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -464,7 +466,7 @@ func (c *corgeHTTPClient) NoContentNoException( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "corge-http", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "NoContentNoException", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -558,7 +560,7 @@ func (c *corgeHTTPClient) CorgeNoContentOnException( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "corge-http", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "CorgeNoContentOnException", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded diff --git a/examples/example-gateway/build/clients/corge/corge.go b/examples/example-gateway/build/clients/corge/corge.go index d77097c28..e1b864709 100644 --- a/examples/example-gateway/build/clients/corge/corge.go +++ b/examples/example-gateway/build/clients/corge/corge.go @@ -113,7 +113,17 @@ func NewClient(deps *module.Dependencies) Client { "Corge::echoString": "EchoString", } - circuitBreakerDisabled := configureCircuitBreaker(deps, timeoutVal) + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.corge.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.corge.circuitBreakerDisabled") + } + + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutVal, methodNames[methodKey]) + } + } client := zanzibar.NewTChannelClientContext( deps.Default.Channel, @@ -172,12 +182,7 @@ func initializeDynamicChannel(deps *module.Dependencies, headerPatterns []string return headerPatterns, re } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.corge.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.corge.circuitBreakerDisabled") - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -201,16 +206,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.corge.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.corge.requestVolumeThreshold")) } - if !circuitBreakerDisabled { - hystrix.ConfigureCommand("corge", hystrix.CommandConfig{ - MaxConcurrentRequests: maxConcurrentRequests, - ErrorPercentThreshold: errorPercentThreshold, - SleepWindow: sleepWindowInMilliseconds, - RequestVolumeThreshold: requestVolumeThreshold, - Timeout: timeoutVal, - }) - } - return circuitBreakerDisabled + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ + MaxConcurrentRequests: maxConcurrentRequests, + ErrorPercentThreshold: errorPercentThreshold, + SleepWindow: sleepWindowInMilliseconds, + RequestVolumeThreshold: requestVolumeThreshold, + Timeout: timeoutVal, + }) } // corgeClient is the TChannel client for downstream service. @@ -246,7 +248,7 @@ func (c *corgeClient) EchoString( "methodName": "EchoString", }) start := time.Now() - err = hystrix.DoC(ctx, "corge", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoString", func(ctx context.Context) error { elapsed := time.Now().Sub(start) scope.Timer("hystrix-timer").Record(elapsed) success, respHeaders, clientErr = c.client.Call( diff --git a/examples/example-gateway/build/clients/echo/echo.go b/examples/example-gateway/build/clients/echo/echo.go index 42c513aa0..e6b57e240 100644 --- a/examples/example-gateway/build/clients/echo/echo.go +++ b/examples/example-gateway/build/clients/echo/echo.go @@ -65,6 +65,18 @@ func NewClient(deps *module.Dependencies) Client { methodNames := map[string]string{ "Echo::Echo": "EchoEcho", } + + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.echo.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.echo.circuitBreakerDisabled") + } + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutInMS, methodNames[methodKey]) + } + } + return &echoClient{ echoClient: gen.NewEchoYARPCClient(oc), opts: zanzibar.NewGRPCClientOpts( @@ -75,21 +87,13 @@ func NewClient(deps *module.Dependencies) Client { "echo", routingKey, requestUUIDHeaderKey, - !configureCircuitBreaker(deps, timeoutInMS), + circuitBreakerDisabled, timeoutInMS, ), } } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.echo.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.echo.circuitBreakerDisabled") - } - if circuitBreakerDisabled { - return false - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -113,15 +117,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.echo.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.echo.requestVolumeThreshold")) } - - hystrix.ConfigureCommand("echo", hystrix.CommandConfig{ + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ MaxConcurrentRequests: maxConcurrentRequests, ErrorPercentThreshold: errorPercentThreshold, SleepWindow: sleepWindowInMilliseconds, RequestVolumeThreshold: requestVolumeThreshold, Timeout: timeoutVal, }) - return true } // EchoEcho is a client RPC call for method Echo::Echo. @@ -152,7 +154,7 @@ func (e *echoClient) EchoEcho( if e.opts.CircuitBreakerDisabled { result, err = runFunc(ctx, request, opts...) } else { - err = hystrix.DoC(ctx, "echo", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoEcho", func(ctx context.Context) error { result, err = runFunc(ctx, request, opts...) return err }, nil) diff --git a/examples/example-gateway/build/clients/google-now/google-now.go b/examples/example-gateway/build/clients/google-now/google-now.go index 342f6eab2..b473da9ae 100644 --- a/examples/example-gateway/build/clients/google-now/google-now.go +++ b/examples/example-gateway/build/clients/google-now/google-now.go @@ -90,17 +90,27 @@ func NewClient(deps *module.Dependencies) Client { followRedirect = deps.Default.Config.MustGetBoolean("clients.google-now.followRedirect") } - circuitBreakerDisabled := configureCircuitBreaker(deps, timeoutVal) + methodNames := map[string]string{ + "AddCredentials": "GoogleNowService::addCredentials", + "CheckCredentials": "GoogleNowService::checkCredentials", + } + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.google-now.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.google-now.circuitBreakerDisabled") + } + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutVal, methodKey) + } + } return &googleNowClient{ clientID: "google-now", httpClient: zanzibar.NewHTTPClientContext( deps.Default.ContextLogger, deps.Default.ContextMetrics, deps.Default.JSONWrapper, "google-now", - map[string]string{ - "AddCredentials": "GoogleNowService::addCredentials", - "CheckCredentials": "GoogleNowService::checkCredentials", - }, + methodNames, baseURL, defaultHeaders, timeout, @@ -112,12 +122,7 @@ func NewClient(deps *module.Dependencies) Client { } } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.google-now.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.google-now.circuitBreakerDisabled") - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -141,16 +146,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.google-now.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.google-now.requestVolumeThreshold")) } - if !circuitBreakerDisabled { - hystrix.ConfigureCommand("google-now", hystrix.CommandConfig{ - MaxConcurrentRequests: maxConcurrentRequests, - ErrorPercentThreshold: errorPercentThreshold, - SleepWindow: sleepWindowInMilliseconds, - RequestVolumeThreshold: requestVolumeThreshold, - Timeout: timeoutVal, - }) - } - return circuitBreakerDisabled + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ + MaxConcurrentRequests: maxConcurrentRequests, + ErrorPercentThreshold: errorPercentThreshold, + SleepWindow: sleepWindowInMilliseconds, + RequestVolumeThreshold: requestVolumeThreshold, + Timeout: timeoutVal, + }) } // HTTPClient returns the underlying HTTP client, should only be @@ -197,7 +199,7 @@ func (c *googleNowClient) AddCredentials( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "google-now", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "AddCredentials", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -279,7 +281,7 @@ func (c *googleNowClient) CheckCredentials( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "google-now", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "CheckCredentials", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded diff --git a/examples/example-gateway/build/clients/multi/multi.go b/examples/example-gateway/build/clients/multi/multi.go index 58d5eb702..c75356383 100644 --- a/examples/example-gateway/build/clients/multi/multi.go +++ b/examples/example-gateway/build/clients/multi/multi.go @@ -88,17 +88,27 @@ func NewClient(deps *module.Dependencies) Client { followRedirect = deps.Default.Config.MustGetBoolean("clients.multi.followRedirect") } - circuitBreakerDisabled := configureCircuitBreaker(deps, timeoutVal) + methodNames := map[string]string{ + "HelloA": "ServiceABack::hello", + "HelloB": "ServiceBBack::hello", + } + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.multi.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.multi.circuitBreakerDisabled") + } + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutVal, methodKey) + } + } return &multiClient{ clientID: "multi", httpClient: zanzibar.NewHTTPClientContext( deps.Default.ContextLogger, deps.Default.ContextMetrics, deps.Default.JSONWrapper, "multi", - map[string]string{ - "HelloA": "ServiceABack::hello", - "HelloB": "ServiceBBack::hello", - }, + methodNames, baseURL, defaultHeaders, timeout, @@ -110,12 +120,7 @@ func NewClient(deps *module.Dependencies) Client { } } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.multi.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.multi.circuitBreakerDisabled") - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -139,16 +144,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.multi.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.multi.requestVolumeThreshold")) } - if !circuitBreakerDisabled { - hystrix.ConfigureCommand("multi", hystrix.CommandConfig{ - MaxConcurrentRequests: maxConcurrentRequests, - ErrorPercentThreshold: errorPercentThreshold, - SleepWindow: sleepWindowInMilliseconds, - RequestVolumeThreshold: requestVolumeThreshold, - Timeout: timeoutVal, - }) - } - return circuitBreakerDisabled + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ + MaxConcurrentRequests: maxConcurrentRequests, + ErrorPercentThreshold: errorPercentThreshold, + SleepWindow: sleepWindowInMilliseconds, + RequestVolumeThreshold: requestVolumeThreshold, + Timeout: timeoutVal, + }) } // HTTPClient returns the underlying HTTP client, should only be @@ -190,7 +192,7 @@ func (c *multiClient) HelloA( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "multi", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "HelloA", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded @@ -273,7 +275,7 @@ func (c *multiClient) HelloB( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "multi", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "HelloB", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded diff --git a/examples/example-gateway/build/clients/withexceptions/withexceptions.go b/examples/example-gateway/build/clients/withexceptions/withexceptions.go index 767abc70f..806fb90c0 100644 --- a/examples/example-gateway/build/clients/withexceptions/withexceptions.go +++ b/examples/example-gateway/build/clients/withexceptions/withexceptions.go @@ -85,16 +85,26 @@ func NewClient(deps *module.Dependencies) Client { followRedirect = deps.Default.Config.MustGetBoolean("clients.withexceptions.followRedirect") } - circuitBreakerDisabled := configureCircuitBreaker(deps, timeoutVal) + methodNames := map[string]string{ + "Func1": "WithExceptions::Func1", + } + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.withexceptions.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.withexceptions.circuitBreakerDisabled") + } + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutVal, methodKey) + } + } return &withexceptionsClient{ clientID: "withexceptions", httpClient: zanzibar.NewHTTPClientContext( deps.Default.ContextLogger, deps.Default.ContextMetrics, deps.Default.JSONWrapper, "withexceptions", - map[string]string{ - "Func1": "WithExceptions::Func1", - }, + methodNames, baseURL, defaultHeaders, timeout, @@ -106,12 +116,7 @@ func NewClient(deps *module.Dependencies) Client { } } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.withexceptions.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.withexceptions.circuitBreakerDisabled") - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -135,16 +140,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.withexceptions.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.withexceptions.requestVolumeThreshold")) } - if !circuitBreakerDisabled { - hystrix.ConfigureCommand("withexceptions", hystrix.CommandConfig{ - MaxConcurrentRequests: maxConcurrentRequests, - ErrorPercentThreshold: errorPercentThreshold, - SleepWindow: sleepWindowInMilliseconds, - RequestVolumeThreshold: requestVolumeThreshold, - Timeout: timeoutVal, - }) - } - return circuitBreakerDisabled + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ + MaxConcurrentRequests: maxConcurrentRequests, + ErrorPercentThreshold: errorPercentThreshold, + SleepWindow: sleepWindowInMilliseconds, + RequestVolumeThreshold: requestVolumeThreshold, + Timeout: timeoutVal, + }) } // HTTPClient returns the underlying HTTP client, should only be @@ -186,7 +188,7 @@ func (c *withexceptionsClient) Func1( } else { // We want hystrix ckt-breaker to count errors only for system issues var clientErr error - err = hystrix.DoC(ctx, "withexceptions", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "Func1", func(ctx context.Context) error { res, clientErr = req.Do() if res != nil { // This is not a system error/issue. Downstream responded diff --git a/examples/selective-gateway/build/clients/echo/echo.go b/examples/selective-gateway/build/clients/echo/echo.go index e4fef7573..5693936ef 100644 --- a/examples/selective-gateway/build/clients/echo/echo.go +++ b/examples/selective-gateway/build/clients/echo/echo.go @@ -65,6 +65,18 @@ func NewClient(deps *module.Dependencies) Client { methodNames := map[string]string{ "Echo::Echo": "EchoEcho", } + + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.echo.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.echo.circuitBreakerDisabled") + } + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutInMS, methodNames[methodKey]) + } + } + return &echoClient{ echoClient: gen.NewEchoYARPCClient(oc), opts: zanzibar.NewGRPCClientOpts( @@ -75,21 +87,13 @@ func NewClient(deps *module.Dependencies) Client { "echo", routingKey, requestUUIDHeaderKey, - !configureCircuitBreaker(deps, timeoutInMS), + circuitBreakerDisabled, timeoutInMS, ), } } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.echo.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.echo.circuitBreakerDisabled") - } - if circuitBreakerDisabled { - return false - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -113,15 +117,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.echo.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.echo.requestVolumeThreshold")) } - - hystrix.ConfigureCommand("echo", hystrix.CommandConfig{ + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ MaxConcurrentRequests: maxConcurrentRequests, ErrorPercentThreshold: errorPercentThreshold, SleepWindow: sleepWindowInMilliseconds, RequestVolumeThreshold: requestVolumeThreshold, Timeout: timeoutVal, }) - return true } // EchoEcho is a client RPC call for method Echo::Echo. @@ -152,7 +154,7 @@ func (e *echoClient) EchoEcho( if e.opts.CircuitBreakerDisabled { result, err = runFunc(ctx, request, opts...) } else { - err = hystrix.DoC(ctx, "echo", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "EchoEcho", func(ctx context.Context) error { result, err = runFunc(ctx, request, opts...) return err }, nil) diff --git a/examples/selective-gateway/build/clients/mirror/mirror.go b/examples/selective-gateway/build/clients/mirror/mirror.go index 175b0ce52..7acb971f4 100644 --- a/examples/selective-gateway/build/clients/mirror/mirror.go +++ b/examples/selective-gateway/build/clients/mirror/mirror.go @@ -73,6 +73,18 @@ func NewClient(deps *module.Dependencies) Client { "Mirror::Mirror": "MirrorMirror", "MirrorInternal::Mirror": "MirrorInternalMirror", } + + // circuitBreakerDisabled sets whether circuit-breaker should be disabled + circuitBreakerDisabled := false + if deps.Default.Config.ContainsKey("clients.mirror.circuitBreakerDisabled") { + circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.mirror.circuitBreakerDisabled") + } + if !circuitBreakerDisabled { + for methodKey := range methodNames { + configureCircuitBreaker(deps, timeoutInMS, methodNames[methodKey]) + } + } + return &mirrorClient{ mirrorClient: gen.NewMirrorYARPCClient(oc), mirrorInternalClient: gen.NewMirrorInternalYARPCClient(oc), @@ -84,21 +96,13 @@ func NewClient(deps *module.Dependencies) Client { "mirror", routingKey, requestUUIDHeaderKey, - !configureCircuitBreaker(deps, timeoutInMS), + circuitBreakerDisabled, timeoutInMS, ), } } -func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { - // circuitBreakerDisabled sets whether circuit-breaker should be disabled - circuitBreakerDisabled := false - if deps.Default.Config.ContainsKey("clients.mirror.circuitBreakerDisabled") { - circuitBreakerDisabled = deps.Default.Config.MustGetBoolean("clients.mirror.circuitBreakerDisabled") - } - if circuitBreakerDisabled { - return false - } +func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int, method string) { // sleepWindowInMilliseconds sets the amount of time, after tripping the circuit, // to reject requests before allowing attempts again to determine if the circuit should again be closed sleepWindowInMilliseconds := 5000 @@ -122,15 +126,13 @@ func configureCircuitBreaker(deps *module.Dependencies, timeoutVal int) bool { if deps.Default.Config.ContainsKey("clients.mirror.requestVolumeThreshold") { requestVolumeThreshold = int(deps.Default.Config.MustGetInt("clients.mirror.requestVolumeThreshold")) } - - hystrix.ConfigureCommand("mirror", hystrix.CommandConfig{ + hystrix.ConfigureCommand(method, hystrix.CommandConfig{ MaxConcurrentRequests: maxConcurrentRequests, ErrorPercentThreshold: errorPercentThreshold, SleepWindow: sleepWindowInMilliseconds, RequestVolumeThreshold: requestVolumeThreshold, Timeout: timeoutVal, }) - return true } // MirrorMirror is a client RPC call for method Mirror::Mirror. @@ -161,7 +163,7 @@ func (e *mirrorClient) MirrorMirror( if e.opts.CircuitBreakerDisabled { result, err = runFunc(ctx, request, opts...) } else { - err = hystrix.DoC(ctx, "mirror", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "MirrorMirror", func(ctx context.Context) error { result, err = runFunc(ctx, request, opts...) return err }, nil) @@ -199,7 +201,7 @@ func (e *mirrorClient) MirrorInternalMirror( if e.opts.CircuitBreakerDisabled { result, err = runFunc(ctx, request, opts...) } else { - err = hystrix.DoC(ctx, "mirror", func(ctx context.Context) error { + err = hystrix.DoC(ctx, "MirrorInternalMirror", func(ctx context.Context) error { result, err = runFunc(ctx, request, opts...) return err }, nil)