Skip to content

Commit

Permalink
fix: add timeouts and fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
notwedtm committed Jan 1, 2024
1 parent 05ff9f2 commit 4952aa4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cmd/sol-shotty/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func proxy(w http.ResponseWriter, req *http.Request) {
return
}

defer response.Result.Body.Close()

fmt.Printf("Successful response from %s in %dms\n", response.Endpoint, response.RTT)
}

Expand Down
11 changes: 7 additions & 4 deletions pkg/shotgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ type SuccessResponse struct {
}

func Shotgun(endpoints []string, mainRequest *http.Request) (SuccessResponse, error) {
successCh := make(chan SuccessResponse, 1) // Make successCh buffered with capacity 1
successCh := make(chan SuccessResponse)

defer close(successCh)

for _, endpoint := range endpoints {
go makeRequest(endpoint, mainRequest, successCh)
}

successValue := <-successCh
return successValue, nil
return <-successCh, nil
}

func makeRequest(endpoint string, req *http.Request, successCh chan SuccessResponse) {
Expand All @@ -39,7 +40,9 @@ func makeRequest(endpoint string, req *http.Request, successCh chan SuccessRespo
}

// Send request
client := &http.Client{}
client := &http.Client{
Timeout: 30 * time.Second, //TODO: Make this configurable
}

startTime := time.Now()
resp, err := client.Do(newReq)
Expand Down

0 comments on commit 4952aa4

Please sign in to comment.