Created
July 8, 2020 12:46
-
-
Save bschaeffer/a975e3b9cd741acc8b8eb752119910f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (h *httpClient) Publish(batch publisher.Batch) error { | |
events := batch.Events() | |
s.stats.NewBatch(len(events)) | |
entries := make([]logEntry, 0, len(events)) | |
for _, event := range events { | |
entries = append(entries, buildLogEntry(event)) | |
} | |
var buf bytes.Buffer | |
enc := json.NewEncoder(&buf) | |
if err := enc.Encode(entries); err != nil { | |
h.stats.Dropped(len(entries)) | |
batch.Drop() | |
return nil | |
} | |
req := http.NewRequest(“POST”, h.endpoint, &buf) | |
req.Header.Set(“content-type”, “application/json”) | |
resp, err := h.client.Do(req) | |
if err != nil { | |
batch.RetryEvents(events) | |
} else { | |
h.stats.Acked(len(events)) | |
batch.ACK() | |
} | |
// Request/Response cleanup | |
defer resp.Body.Close() | |
_, _ = ioutil.ReadAll(resp.Body) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment