Skip to content

Commit

Permalink
ep level timeout and retry support
Browse files Browse the repository at this point in the history
  • Loading branch information
bishnuag committed Sep 21, 2022
1 parent 63e79be commit 86a6b90
Show file tree
Hide file tree
Showing 99 changed files with 1,826 additions and 593 deletions.
61 changes: 43 additions & 18 deletions codegen/template_bundle/template_files.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion codegen/templates/http_client.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Client interface {
{{if ne .RequestType "" -}}
args {{.RequestType}},
{{end -}}
timeoutAndRetryOps *zanzibar.TimeoutAndRetryOptions,
) (context.Context, {{- if ne .ResponseType "" -}} {{.ResponseType}}, {{- end -}}map[string]string, error)
{{- end -}}
{{- end -}}
Expand Down Expand Up @@ -258,6 +259,7 @@ func (c *{{$clientName}}) {{$methodName}}(
{{if ne .RequestType "" -}}
r {{.RequestType}},
{{end -}}
timeoutAndRetryOps *zanzibar.TimeoutAndRetryOptions,
) (context.Context, {{- if ne .ResponseType "" -}} {{.ResponseType}}, {{- end -}}map[string]string, error) {
reqUUID := zanzibar.RequestUUIDFromCtx(ctx)
if headers == nil {
Expand All @@ -273,7 +275,7 @@ func (c *{{$clientName}}) {{$methodName}}(
{{if .ResponseType -}}
var defaultRes {{.ResponseType}}
{{end -}}
req := zanzibar.NewClientHTTPRequest(ctx, c.clientID, "{{$methodName}}", "{{$serviceMethod}}", c.httpClient)
req := zanzibar.NewClientHTTPRequest(ctx, c.clientID, "{{$methodName}}", "{{$serviceMethod}}", c.httpClient,timeoutAndRetryOps)

{{if .ReqHeaderGoStatements }}
{{range $index, $line := .ReqClientHeaderGoStatements -}}
Expand Down
4 changes: 3 additions & 1 deletion codegen/templates/service_mock.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type MockService interface {
method string,
headers map[string]string,
req, resp zanzibar.RWTStruct,
timeoutAndRetryOps *zanzibar.TimeoutAndRetryOptions,
) (bool, map[string]string, error)
{{$mock}}() *{{$mockType}}
Server() *zanzibar.Gateway
Expand Down Expand Up @@ -188,12 +189,13 @@ func (m *mockService) MakeTChannelRequest(
method string,
headers map[string]string,
req, res zanzibar.RWTStruct,
timeoutAndRetryOps *zanzibar.TimeoutAndRetryOptions,
) (bool, map[string]string, error) {
if !m.started {
return false, nil, errors.New("mock server is not started")
}

sc := m.server.ServerTChannel.GetSubChannel(m.server.ServiceName)
sc.Peers().Add(m.server.RealTChannelAddr)
return m.tChannelClient.Call(ctx, thriftService, method, headers, req, res)
return m.tChannelClient.Call(ctx, thriftService, method, headers, req, res, timeoutAndRetryOps)
}
6 changes: 4 additions & 2 deletions codegen/templates/tchannel_client.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Client interface {
{{if ne .RequestType "" -}}
args {{.RequestType}},
{{end -}}
timeoutAndRetryOps *zanzibar.TimeoutAndRetryOptions,
) (context.Context, {{- if ne .ResponseType "" -}} {{.ResponseType}}, {{- end -}}map[string]string, error)
{{- end -}}
{{- end -}}
Expand Down Expand Up @@ -329,6 +330,7 @@ type {{$clientName}} struct {
{{if ne .RequestType "" -}}
args {{.RequestType}},
{{end -}}
timeoutAndRetryOps *zanzibar.TimeoutAndRetryOptions,
) (context.Context, {{- if ne .ResponseType "" -}} {{.ResponseType}}, {{- end -}}map[string]string, error) {
var result {{.GenCodePkgName}}.{{title $svc.Name}}_{{title .Name}}_Result
{{if .ResponseType -}}
Expand All @@ -345,7 +347,7 @@ type {{$clientName}} struct {
var err error
if (c.circuitBreakerDisabled) {
success, respHeaders, err = c.client.Call(
ctx, "{{$svc.Name}}", "{{.Name}}", reqHeaders, args, &result,
ctx, "{{$svc.Name}}", "{{.Name}}", reqHeaders, args, &result, timeoutAndRetryOps,
)
} else {
// We want hystrix ckt-breaker to count errors only for system issues
Expand All @@ -360,7 +362,7 @@ type {{$clientName}} struct {
elapsed := time.Now().Sub(start)
scope.Timer("hystrix-timer").Record(elapsed)
success, respHeaders, clientErr = c.client.Call(
ctx, "{{$svc.Name}}", "{{.Name}}", reqHeaders, args, &result,
ctx, "{{$svc.Name}}", "{{.Name}}", reqHeaders, args, &result, timeoutAndRetryOps,
)
if _, isSysErr := clientErr.(tchannel.SystemError); !isSysErr {
// Declare ok if it is not a system-error
Expand Down
10 changes: 9 additions & 1 deletion codegen/templates/tchannel_endpoint.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ func (h *{{$handlerName}}) redirectToDeputy(
h.Deps.Default.Config.MustGetInt("tchannel.deputy.timeoutPerAttempt"),
)

maxAttempts := int(h.Deps.Default.Config.MustGetInt("clients.{{$clientID}}.retryCount"))

methodNames := map[string]string{
"{{.ThriftService}}::{{.Name}}": "{{$methodName}}",
}
Expand All @@ -266,7 +268,13 @@ func (h *{{$handlerName}}) redirectToDeputy(
},
)

success, respHeaders, err := client.Call(ctx, "{{.ThriftService}}", "{{$methodName}}", reqHeaders, req, res)
timeoutAndRetryOps := zanzibar.TimeoutAndRetryOptions{
OverallTimeoutInMs: (timeoutPerAttempt + zanzibar.DefaultBackOffTimeAcrossRetries) * time.Duration(maxAttempts),
RequestTimeoutPerAttemptInMs: timeoutPerAttempt,
MaxAttempts: maxAttempts,
BackOffTimeAcrossRetriesInMs: zanzibar.DefaultBackOffTimeAcrossRetries,
}
success, respHeaders, err := client.Call(ctx, "{{.ThriftService}}", "{{$methodName}}", reqHeaders, req, res, &timeoutAndRetryOps)
return ctx, success, res, respHeaders, err
}
{{end -}}
Expand Down
Loading

0 comments on commit 86a6b90

Please sign in to comment.