diff --git a/transport/http/http.go b/transport/http/http.go index 218ea55..73aebab 100644 --- a/transport/http/http.go +++ b/transport/http/http.go @@ -4,12 +4,13 @@ import ( "bytes" "encoding/json" "flag" - "fmt" "math" "net/http" "sync" "time" + log "github.com/sirupsen/logrus" + "github.com/netsampler/goflow2/v2/transport" ) @@ -59,38 +60,41 @@ func (d *HTTPDriver) Send(key, data []byte) error { maxRetries := 3 delay := time.Millisecond * 500 // initial delay - for i := 0; i < maxRetries; i++ { - req, err := http.NewRequest("POST", d.httpDestination, bytes.NewBuffer(jsonData)) - if err != nil { - return err - } - req.Header.Set("Content-Type", "application/json") - req.Header.Set(d.httpAuthHeader, d.httpAuthCredentials) + var wg sync.WaitGroup + wg.Add(1) - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - if i == maxRetries-1 { - return err + go func() { + defer wg.Done() + + for i := 0; i < maxRetries; i++ { + req, err := http.NewRequest("POST", d.httpDestination, bytes.NewBuffer(jsonData)) + if err != nil { + return } - continue - } - defer resp.Body.Close() - // If the status code is not in the 2xx range, retry - if resp.StatusCode < 200 || resp.StatusCode >= 300 { - if i == maxRetries-1 { - return fmt.Errorf("failed to send data, status code: %d", resp.StatusCode) + req.Header.Set("Content-Type", "application/json") + req.Header.Set(d.httpAuthHeader, d.httpAuthCredentials) + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil || (resp.StatusCode < 200 || resp.StatusCode >= 300) { + if i == maxRetries-1 { + log.Error(err) + return + } + time.Sleep(delay * time.Duration(math.Pow(2, float64(i)))) // exponential backoff + continue } - time.Sleep(delay * time.Duration(math.Pow(2, float64(i)))) // exponential backoff - continue - } + defer resp.Body.Close() - // reset batchData - d.batchData = d.batchData[:0] - break - } + // reset batchData + d.batchData = d.batchData[:0] + break + } + }() + + wg.Wait() } return nil