Skip to content

Commit

Permalink
Moved API call to sync process (already running in thread)
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Shanley <dave@quobix.com>
  • Loading branch information
daveshanley committed Jun 14, 2023
1 parent a78c829 commit 90e3458
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 24 deletions.
9 changes: 3 additions & 6 deletions daemon/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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)
Expand All @@ -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
}
19 changes: 1 addition & 18 deletions daemon/handle_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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.
Expand Down

0 comments on commit 90e3458

Please sign in to comment.