Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@

v1.0.0-webhookfn / 2019-10-01
=============================

* update message
* hide sensitive payload request. support PrintErrors using Config

v0.0.2-webhookfn / 2019-08-30
=============================

* support Source config

v0.0.1-webhookfn / 2019-08-30
=============================

Expand Down
29 changes: 17 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Config struct {
MaxBatchBytes int
MaxBatchCount int
MaxBatchInterval time.Duration

PrintErrors bool
}

type Client struct {
Expand Down Expand Up @@ -122,7 +124,10 @@ func (c *Client) flush(b *buffer) {
Objects: rm,
}

c.makeRequest(batchRequest)
err := c.makeRequest(batchRequest)
if c.PrintErrors {
log.Printf("[ERROR] Batch failed making request: %v", err)
}
})
b.reset()
}
Expand All @@ -140,7 +145,9 @@ func (c *Client) buffer(b *buffer) {
})
x, err := json.Marshal(req)
if err != nil {
log.Printf("[Error] Message `%s` excluded from batch: %v", req.ID, err)
if c.PrintErrors {
log.Printf("[Error] Message `%s` excluded from batch: %v", req.ID, err)
}
continue
}
if b.size()+len(x) >= c.MaxBatchBytes || b.count()+1 >= c.MaxBatchCount {
Expand All @@ -156,7 +163,9 @@ func (c *Client) buffer(b *buffer) {
})
x, err := json.Marshal(req)
if err != nil {
log.Printf("[Error] Message `%s` excluded from batch: %v", req.ID, err)
if c.PrintErrors {
log.Printf("[Error] Exiting: Message `%s` excluded from batch: %v", req.ID, err)
}
continue
}
if b.size()+len(x) >= c.MaxBatchBytes || b.count()+1 >= c.MaxBatchCount {
Expand Down Expand Up @@ -201,11 +210,10 @@ func (c *Client) Set(v *Object) error {
return nil
}

func (c *Client) makeRequest(request *batch) {
func (c *Client) makeRequest(request *batch) error {
payload, err := json.Marshal(request)
if err != nil {
log.Printf("[Error] Batch failed to marshal: %v - %v", request, err)
return
return err
}

b := backoff.NewExponentialBackOff()
Expand All @@ -223,15 +231,12 @@ func (c *Client) makeRequest(request *batch) {
dec.Decode(&response)

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP Post Request Failed, Status Code %d. \nResponse: %v \nRequest payload: %v",
resp.StatusCode, response, string(payload))
return fmt.Errorf("HTTP Post Request Failed, Status Code %d. \nResponse: %v",
resp.StatusCode, response)
}

return nil
}, b)

if err != nil {
log.Printf("[Error] %v", err)
return
}
return err
}