From 28e2ad0d4c39e435a698472843e868cf45141b4b Mon Sep 17 00:00:00 2001 From: Nick Turner Date: Mon, 6 Jan 2020 18:15:01 -0800 Subject: [PATCH] update deputy middleware to properly close channel between connections --- codegen/template_bundle/template_files.go | 16 ++++++++-------- codegen/templates/tchannel_endpoint.tmpl | 14 +++++++------- .../bounce_bounce_method_bounce_tchannel.go | 14 +++++++------- .../baz_simpleservice_method_call_tchannel.go | 14 +++++++------- .../baz_simpleservice_method_echo_tchannel.go | 14 +++++++------- .../echo/echo_echo_method_echo_tchannel.go | 14 +++++++------- ..._simpleservice_method_anothercall_tchannel.go | 14 +++++++------- ...x_simpleservice_method_echostring_tchannel.go | 14 +++++++------- 8 files changed, 57 insertions(+), 57 deletions(-) diff --git a/codegen/template_bundle/template_files.go b/codegen/template_bundle/template_files.go index 53e6a8847..b5eb2257e 100644 --- a/codegen/template_bundle/template_files.go +++ b/codegen/template_bundle/template_files.go @@ -3354,10 +3354,14 @@ func (h *{{$handlerName}}) redirectToDeputy( "{{.ThriftService}}::{{.Name}}": "{{$methodName}}", } - sub := h.Deps.Default.Channel.GetSubChannel(serviceName, tchannel.Isolated) - sub.Peers().Add(hostPort) + deputyChannel, err := tchannel.NewChannel(serviceName, nil) + if err != nil { + h.Deps.Default.ContextLogger.Error(ctx, "Deputy Failure", zap.Error(err)) + } + defer deputyChannel.Close() + deputyChannel.Peers().Add(hostPort) client := zanzibar.NewTChannelClientContext( - h.Deps.Default.Channel, + deputyChannel, h.Deps.Default.Logger, h.Deps.Default.ContextMetrics, h.Deps.Default.ContextExtractor, @@ -3372,10 +3376,6 @@ func (h *{{$handlerName}}) redirectToDeputy( ) success, respHeaders, err := client.Call(ctx, "{{.ThriftService}}", "{{$methodName}}", reqHeaders, req, res) - // hostPort is added above, so there should not be any error returned for the - // following line - // nolint - _ = sub.Peers().Remove(hostPort) return success, res, respHeaders, err } {{end -}} @@ -3393,7 +3393,7 @@ func tchannel_endpointTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "tchannel_endpoint.tmpl", size: 8817, mode: os.FileMode(420), modTime: time.Unix(1, 0)} + info := bindataFileInfo{name: "tchannel_endpoint.tmpl", size: 8784, mode: os.FileMode(420), modTime: time.Unix(1, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/codegen/templates/tchannel_endpoint.tmpl b/codegen/templates/tchannel_endpoint.tmpl index c4a170560..84fb68273 100644 --- a/codegen/templates/tchannel_endpoint.tmpl +++ b/codegen/templates/tchannel_endpoint.tmpl @@ -244,10 +244,14 @@ func (h *{{$handlerName}}) redirectToDeputy( "{{.ThriftService}}::{{.Name}}": "{{$methodName}}", } - sub := h.Deps.Default.Channel.GetSubChannel(serviceName, tchannel.Isolated) - sub.Peers().Add(hostPort) + deputyChannel, err := tchannel.NewChannel(serviceName, nil) + if err != nil { + h.Deps.Default.ContextLogger.Error(ctx, "Deputy Failure", zap.Error(err)) + } + defer deputyChannel.Close() + deputyChannel.Peers().Add(hostPort) client := zanzibar.NewTChannelClientContext( - h.Deps.Default.Channel, + deputyChannel, h.Deps.Default.Logger, h.Deps.Default.ContextMetrics, h.Deps.Default.ContextExtractor, @@ -262,10 +266,6 @@ func (h *{{$handlerName}}) redirectToDeputy( ) success, respHeaders, err := client.Call(ctx, "{{.ThriftService}}", "{{$methodName}}", reqHeaders, req, res) - // hostPort is added above, so there should not be any error returned for the - // following line - // nolint - _ = sub.Peers().Remove(hostPort) return success, res, respHeaders, err } {{end -}} diff --git a/examples/example-gateway/build/endpoints/bounce/bounce_bounce_method_bounce_tchannel.go b/examples/example-gateway/build/endpoints/bounce/bounce_bounce_method_bounce_tchannel.go index 3f3bd8dbf..63912a5e1 100644 --- a/examples/example-gateway/build/endpoints/bounce/bounce_bounce_method_bounce_tchannel.go +++ b/examples/example-gateway/build/endpoints/bounce/bounce_bounce_method_bounce_tchannel.go @@ -158,10 +158,14 @@ func (h *BounceBounceHandler) redirectToDeputy( "Bounce::bounce": "bounce", } - sub := h.Deps.Default.Channel.GetSubChannel(serviceName, tchannel.Isolated) - sub.Peers().Add(hostPort) + deputyChannel, err := tchannel.NewChannel(serviceName, nil) + if err != nil { + h.Deps.Default.ContextLogger.Error(ctx, "Deputy Failure", zap.Error(err)) + } + defer deputyChannel.Close() + deputyChannel.Peers().Add(hostPort) client := zanzibar.NewTChannelClientContext( - h.Deps.Default.Channel, + deputyChannel, h.Deps.Default.Logger, h.Deps.Default.ContextMetrics, h.Deps.Default.ContextExtractor, @@ -176,9 +180,5 @@ func (h *BounceBounceHandler) redirectToDeputy( ) success, respHeaders, err := client.Call(ctx, "Bounce", "bounce", reqHeaders, req, res) - // hostPort is added above, so there should not be any error returned for the - // following line - // nolint - _ = sub.Peers().Remove(hostPort) return success, res, respHeaders, err } diff --git a/examples/example-gateway/build/endpoints/tchannel/baz/baz_simpleservice_method_call_tchannel.go b/examples/example-gateway/build/endpoints/tchannel/baz/baz_simpleservice_method_call_tchannel.go index 2139f106c..b863469e7 100644 --- a/examples/example-gateway/build/endpoints/tchannel/baz/baz_simpleservice_method_call_tchannel.go +++ b/examples/example-gateway/build/endpoints/tchannel/baz/baz_simpleservice_method_call_tchannel.go @@ -214,10 +214,14 @@ func (h *SimpleServiceCallHandler) redirectToDeputy( "SimpleService::Call": "Call", } - sub := h.Deps.Default.Channel.GetSubChannel(serviceName, tchannel.Isolated) - sub.Peers().Add(hostPort) + deputyChannel, err := tchannel.NewChannel(serviceName, nil) + if err != nil { + h.Deps.Default.ContextLogger.Error(ctx, "Deputy Failure", zap.Error(err)) + } + defer deputyChannel.Close() + deputyChannel.Peers().Add(hostPort) client := zanzibar.NewTChannelClientContext( - h.Deps.Default.Channel, + deputyChannel, h.Deps.Default.Logger, h.Deps.Default.ContextMetrics, h.Deps.Default.ContextExtractor, @@ -232,9 +236,5 @@ func (h *SimpleServiceCallHandler) redirectToDeputy( ) success, respHeaders, err := client.Call(ctx, "SimpleService", "Call", reqHeaders, req, res) - // hostPort is added above, so there should not be any error returned for the - // following line - // nolint - _ = sub.Peers().Remove(hostPort) return success, res, respHeaders, err } diff --git a/examples/example-gateway/build/endpoints/tchannel/baz/baz_simpleservice_method_echo_tchannel.go b/examples/example-gateway/build/endpoints/tchannel/baz/baz_simpleservice_method_echo_tchannel.go index 8bfa121bd..c1938ac0f 100644 --- a/examples/example-gateway/build/endpoints/tchannel/baz/baz_simpleservice_method_echo_tchannel.go +++ b/examples/example-gateway/build/endpoints/tchannel/baz/baz_simpleservice_method_echo_tchannel.go @@ -158,10 +158,14 @@ func (h *SimpleServiceEchoHandler) redirectToDeputy( "SimpleService::Echo": "Echo", } - sub := h.Deps.Default.Channel.GetSubChannel(serviceName, tchannel.Isolated) - sub.Peers().Add(hostPort) + deputyChannel, err := tchannel.NewChannel(serviceName, nil) + if err != nil { + h.Deps.Default.ContextLogger.Error(ctx, "Deputy Failure", zap.Error(err)) + } + defer deputyChannel.Close() + deputyChannel.Peers().Add(hostPort) client := zanzibar.NewTChannelClientContext( - h.Deps.Default.Channel, + deputyChannel, h.Deps.Default.Logger, h.Deps.Default.ContextMetrics, h.Deps.Default.ContextExtractor, @@ -176,9 +180,5 @@ func (h *SimpleServiceEchoHandler) redirectToDeputy( ) success, respHeaders, err := client.Call(ctx, "SimpleService", "Echo", reqHeaders, req, res) - // hostPort is added above, so there should not be any error returned for the - // following line - // nolint - _ = sub.Peers().Remove(hostPort) return success, res, respHeaders, err } diff --git a/examples/example-gateway/build/endpoints/tchannel/echo/echo_echo_method_echo_tchannel.go b/examples/example-gateway/build/endpoints/tchannel/echo/echo_echo_method_echo_tchannel.go index c9191c158..e4471704c 100644 --- a/examples/example-gateway/build/endpoints/tchannel/echo/echo_echo_method_echo_tchannel.go +++ b/examples/example-gateway/build/endpoints/tchannel/echo/echo_echo_method_echo_tchannel.go @@ -158,10 +158,14 @@ func (h *EchoEchoHandler) redirectToDeputy( "Echo::echo": "echo", } - sub := h.Deps.Default.Channel.GetSubChannel(serviceName, tchannel.Isolated) - sub.Peers().Add(hostPort) + deputyChannel, err := tchannel.NewChannel(serviceName, nil) + if err != nil { + h.Deps.Default.ContextLogger.Error(ctx, "Deputy Failure", zap.Error(err)) + } + defer deputyChannel.Close() + deputyChannel.Peers().Add(hostPort) client := zanzibar.NewTChannelClientContext( - h.Deps.Default.Channel, + deputyChannel, h.Deps.Default.Logger, h.Deps.Default.ContextMetrics, h.Deps.Default.ContextExtractor, @@ -176,9 +180,5 @@ func (h *EchoEchoHandler) redirectToDeputy( ) success, respHeaders, err := client.Call(ctx, "Echo", "echo", reqHeaders, req, res) - // hostPort is added above, so there should not be any error returned for the - // following line - // nolint - _ = sub.Peers().Remove(hostPort) return success, res, respHeaders, err } diff --git a/examples/example-gateway/build/endpoints/tchannel/panic/panic_simpleservice_method_anothercall_tchannel.go b/examples/example-gateway/build/endpoints/tchannel/panic/panic_simpleservice_method_anothercall_tchannel.go index 5f951fa67..0ae5ecf8f 100644 --- a/examples/example-gateway/build/endpoints/tchannel/panic/panic_simpleservice_method_anothercall_tchannel.go +++ b/examples/example-gateway/build/endpoints/tchannel/panic/panic_simpleservice_method_anothercall_tchannel.go @@ -208,10 +208,14 @@ func (h *SimpleServiceAnotherCallHandler) redirectToDeputy( "SimpleService::AnotherCall": "AnotherCall", } - sub := h.Deps.Default.Channel.GetSubChannel(serviceName, tchannel.Isolated) - sub.Peers().Add(hostPort) + deputyChannel, err := tchannel.NewChannel(serviceName, nil) + if err != nil { + h.Deps.Default.ContextLogger.Error(ctx, "Deputy Failure", zap.Error(err)) + } + defer deputyChannel.Close() + deputyChannel.Peers().Add(hostPort) client := zanzibar.NewTChannelClientContext( - h.Deps.Default.Channel, + deputyChannel, h.Deps.Default.Logger, h.Deps.Default.ContextMetrics, h.Deps.Default.ContextExtractor, @@ -226,9 +230,5 @@ func (h *SimpleServiceAnotherCallHandler) redirectToDeputy( ) success, respHeaders, err := client.Call(ctx, "SimpleService", "AnotherCall", reqHeaders, req, res) - // hostPort is added above, so there should not be any error returned for the - // following line - // nolint - _ = sub.Peers().Remove(hostPort) return success, res, respHeaders, err } diff --git a/examples/example-gateway/build/endpoints/tchannel/quux/quux_simpleservice_method_echostring_tchannel.go b/examples/example-gateway/build/endpoints/tchannel/quux/quux_simpleservice_method_echostring_tchannel.go index 6a7aea841..d924ce538 100644 --- a/examples/example-gateway/build/endpoints/tchannel/quux/quux_simpleservice_method_echostring_tchannel.go +++ b/examples/example-gateway/build/endpoints/tchannel/quux/quux_simpleservice_method_echostring_tchannel.go @@ -158,10 +158,14 @@ func (h *SimpleServiceEchoStringHandler) redirectToDeputy( "SimpleService::EchoString": "EchoString", } - sub := h.Deps.Default.Channel.GetSubChannel(serviceName, tchannel.Isolated) - sub.Peers().Add(hostPort) + deputyChannel, err := tchannel.NewChannel(serviceName, nil) + if err != nil { + h.Deps.Default.ContextLogger.Error(ctx, "Deputy Failure", zap.Error(err)) + } + defer deputyChannel.Close() + deputyChannel.Peers().Add(hostPort) client := zanzibar.NewTChannelClientContext( - h.Deps.Default.Channel, + deputyChannel, h.Deps.Default.Logger, h.Deps.Default.ContextMetrics, h.Deps.Default.ContextExtractor, @@ -176,9 +180,5 @@ func (h *SimpleServiceEchoStringHandler) redirectToDeputy( ) success, respHeaders, err := client.Call(ctx, "SimpleService", "EchoString", reqHeaders, req, res) - // hostPort is added above, so there should not be any error returned for the - // following line - // nolint - _ = sub.Peers().Remove(hostPort) return success, res, respHeaders, err }