From 90e34589465556045d9fe482edf8399634209047 Mon Sep 17 00:00:00 2001 From: Dave Shanley Date: Tue, 6 Jun 2023 16:43:54 -0400 Subject: [PATCH] Moved API call to sync process (already running in thread) Signed-off-by: Dave Shanley --- daemon/api.go | 9 +++------ daemon/handle_request.go | 19 +------------------ 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/daemon/api.go b/daemon/api.go index eb88daa..a16d704 100644 --- a/daemon/api.go +++ b/daemon/api.go @@ -35,7 +35,7 @@ func (c *wiretapTransport) RoundTrip(r *http.Request) (*http.Response, error) { return resp, err } -func (ws *WiretapService) callAPI(req *http.Request, responseChan chan *http.Response, errorChan chan error) { +func (ws *WiretapService) callAPI(req *http.Request) (*http.Response, error) { tr := newWiretapTransport() client := &http.Client{Transport: tr} @@ -61,8 +61,7 @@ func (ws *WiretapService) callAPI(req *http.Request, responseChan chan *http.Res resp, err := client.Do(newReq) if err != nil { - errorChan <- err - close(errorChan) + return nil, err } fmt.Print(tr.capturedCookieHeaders) @@ -72,7 +71,5 @@ func (ws *WiretapService) callAPI(req *http.Request, responseChan chan *http.Res resp.Header.Set("Set-Cookie", tr.capturedCookieHeaders[0]) } } - - responseChan <- resp - close(responseChan) + return resp, nil } diff --git a/daemon/handle_request.go b/daemon/handle_request.go index d337a30..b47e6d4 100644 --- a/daemon/handle_request.go +++ b/daemon/handle_request.go @@ -18,8 +18,6 @@ import ( func (ws *WiretapService) handleHttpRequest(request *model.Request) { - lowResponseChan := make(chan *http.Response) - lowErrorChan := make(chan error) var returnedResponse *http.Response var returnedError error @@ -32,21 +30,7 @@ func (ws *WiretapService) handleHttpRequest(request *model.Request) { go ws.validateRequest(request, requestValidator, paramValidator, responseValidator) // call the API being requested. - go ws.callAPI(request.HttpRequest, lowResponseChan, lowErrorChan) - -doneWaitingForResponse: - for { - select { - case resp, ok := <-lowResponseChan: - if ok { - returnedResponse = resp - } - break doneWaitingForResponse - case err := <-lowErrorChan: - returnedError = err - break doneWaitingForResponse - } - } + returnedResponse, returnedError = ws.callAPI(request.HttpRequest) if returnedResponse == nil && returnedError != nil { utils.Log.Infof("[wiretap] request %s: Failed (%d)", request.HttpRequest.URL.String(), 500) @@ -61,7 +45,6 @@ doneWaitingForResponse: } // send response back to client. - //go func() { config := ws.controlsStore.GetValue(shared.ConfigKey).(*shared.WiretapConfiguration) if config.GlobalAPIDelay > 0 { time.Sleep(time.Duration(config.GlobalAPIDelay) * time.Millisecond) // simulate a slow response.