From 1b78106cd164f3ca7710eb67e1f1040792978620 Mon Sep 17 00:00:00 2001 From: yaron2 Date: Fri, 21 Jun 2019 11:17:48 -0700 Subject: [PATCH] fix local address check, fix test --- pkg/action/action.go | 39 +++++++++++++++++---------------------- pkg/action/action_test.go | 6 +++--- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/pkg/action/action.go b/pkg/action/action.go index 806cefd5..fa9bb29a 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -728,7 +728,6 @@ func (i *Action) OnLocalBatchActivation(c *routing.Context) { } func (i *Action) OnInvokeAction(c *routing.Context) { - _, span, spanCtx := i.TraceSpanFromRoutingContext(c, nil, "OnInvokeAction") if span != nil { defer span.End() @@ -741,9 +740,9 @@ func (i *Action) OnInvokeAction(c *routing.Context) { targetAddress := string(c.Request.Header.Peek(addressHeader)) httpMethod := string(c.Method()) - corId := "" + corID := "" if span != nil { - corId = diag.SerializeSpanContext(*spanCtx) + corID = diag.SerializeSpanContext(*spanCtx) } headers := map[string]string{ @@ -751,7 +750,7 @@ func (i *Action) OnInvokeAction(c *routing.Context) { invokedHTTPVerbHeader: httpMethod, invokedMethodHeader: actionMethod, actionTargetIDHeader: actionID, - diag.CorrelationID: corId, + diag.CorrelationID: corID, } if contextID != "" { @@ -782,12 +781,9 @@ func (i *Action) OnInvokeAction(c *routing.Context) { if address == "" { if !responseDelivered { - diag.SetSpanStatus(span, trace.StatusCodeUnavailable, fmt.Sprintf("could not locate host for: %s/%s", actionID, contextID)) - RespondWithError(c.RequestCtx, 500, fmt.Sprintf("could not locate host for: %s/%s", actionID, contextID)) } - return } } else { @@ -798,16 +794,14 @@ func (i *Action) OnInvokeAction(c *routing.Context) { var resp []byte var err error - if address == fmt.Sprintf("%s:%v", i.IPAddress, gRPCPort) { + if i.isTargetLocal(targetAddress) { resp, err = i.invoke(actionMethod, actionID, contextID, "", httpMethod, string(c.URI().QueryString()), payload, spanCtx) } else { conn, err := i.GetGRPCConnection(address) if err != nil { log.Fatalf("gRPC connection failure: %s", err) if !responseDelivered { - diag.SetSpanStatus(span, trace.StatusCodeInternal, fmt.Sprintf("Delivery to action id %s failed - %s", actionID, err.Error())) - RespondWithError(c.RequestCtx, 500, fmt.Sprintf("delivery to action id %s failed", actionID)) } return @@ -828,21 +822,17 @@ func (i *Action) OnInvokeAction(c *routing.Context) { if err != nil { log.Error(err) - diag.SetSpanStatus(span, trace.StatusCodeInternal, fmt.Sprintf("Delivery to action id %s failed - %s", actionID, err.Error())) - RespondWithError(c.RequestCtx, 500, fmt.Sprintf("delivery to action id %s failed", actionID)) - } else { - if resp != nil && len(resp) > 0 { - - diag.SetSpanStatus(span, trace.StatusCodeOK, fmt.Sprintf("action was called and returned %s", string(resp))) - - RespondWithJSON(c.RequestCtx, 200, resp) - } else { - diag.SetSpanStatus(span, trace.StatusCodeOK, fmt.Sprintf("action was called but returned no value")) + return + } - RespondEmpty(c.RequestCtx, 200) - } + if resp != nil && len(resp) > 0 { + diag.SetSpanStatus(span, trace.StatusCodeOK, fmt.Sprintf("action was called and returned %s", string(resp))) + RespondWithJSON(c.RequestCtx, 200, resp) + } else { + diag.SetSpanStatus(span, trace.StatusCodeOK, fmt.Sprintf("action was called but returned no value")) + RespondEmpty(c.RequestCtx, 200) } } @@ -1551,6 +1541,11 @@ func (i *Action) SetIPAddress() { } } +func (i *Action) isTargetLocal(address string) bool { + return strings.Contains(address, "localhost") || strings.Contains(address, "127.0.0.1") || + address == fmt.Sprintf("%s:%v", i.IPAddress, gRPCPort) +} + func (i *Action) GetApplicationConfig() { if i.Protocol == GRPCProtocol { client := pb.NewAppClient(i.GRPCClient) diff --git a/pkg/action/action_test.go b/pkg/action/action_test.go index 18c2bfbe..9f3e098d 100644 --- a/pkg/action/action_test.go +++ b/pkg/action/action_test.go @@ -16,7 +16,7 @@ func TestInvokeActionGETWithOneParameter(t *testing.T) { is := assert.New(t) req := fasthttp.AcquireRequest() req.SetRequestURI("http://localhost:3505/action/test-action/echo?param=1") - req.Header.Set("actions.action-address", "10.0.75.1:60006") + req.Header.Set("actions.action-address", "localhost:60006") req.Header.SetMethod("GET") resp := fasthttp.AcquireResponse() client := &fasthttp.Client{} @@ -28,7 +28,7 @@ func TestInvokeActionGETWithTwoParameters(t *testing.T) { is := assert.New(t) req := fasthttp.AcquireRequest() req.SetRequestURI("http://localhost:3505/action/test-action/echo?param1=1¶m2=2") - req.Header.Set("actions.action-address", "10.0.75.1:60006") + req.Header.Set("actions.action-address", "localhost:60006") req.Header.SetMethod("GET") resp := fasthttp.AcquireResponse() client := &fasthttp.Client{} @@ -55,7 +55,7 @@ func TestInvokeActionGETWithNoParameters(t *testing.T) { is := assert.New(t) req := fasthttp.AcquireRequest() req.SetRequestURI("http://localhost:3505/action/test-action/echo") - req.Header.Set("actions.action-address", "10.0.75.1:60006") + req.Header.Set("actions.action-address", "localhost:60006") req.Header.SetMethod("GET") resp := fasthttp.AcquireResponse() client := &fasthttp.Client{} -- GitLab