Skip to content

Commit

Permalink
fix: bad pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
euskadi31 committed May 4, 2023
1 parent 6bfe745 commit f77b6fc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
32 changes: 24 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type client struct {
retryInterval time.Duration
retrySize int
httpClient *http.Client
msgs chan *Event
events []*Event
msgs chan Event
events []Event
retries chan *Payload
quit chan struct{}
shutdown chan struct{}
Expand All @@ -67,8 +67,8 @@ func New(key string, opts ...Option) Client {
c.httpClient = &http.Client{
Timeout: c.timeout,
}
c.msgs = make(chan *Event, c.bufferSize)
c.events = make([]*Event, 0, c.bufferSize)
c.msgs = make(chan Event, c.bufferSize)
c.events = make([]Event, 0, c.bufferSize)
c.retries = make(chan *Payload, c.retrySize)

for _, opt := range opts {
Expand Down Expand Up @@ -156,6 +156,16 @@ func (c *client) Close() (err error) {
return
}

func (c *client) processErrorResponse(resp *http.Response) error {
var errorResponse *ErrorResponse

if err := json.NewDecoder(resp.Body).Decode(&errorResponse); err != nil {
return fmt.Errorf("json decode failed: %w", err)
}

return errorResponse
}

func (c *client) sendBatch(payload *Payload) error {
ctx := context.Background()

Expand All @@ -178,10 +188,16 @@ func (c *client) sendBatch(payload *Payload) error {

return fmt.Errorf("http client send request failed: %w", err)
}
defer resp.Body.Close()
defer func() {
if err := resp.Body.Close(); err != nil {
log.Error().Err(err).Msg("http client close response body failed")
}
}()

if resp.StatusCode != http.StatusOK {
log.Error().Msgf("Amplitude send batch failed: status code %d", resp.StatusCode)
err := c.processErrorResponse(resp)

log.Error().Err(err).Msgf("Amplitude send batch failed: status code %d", resp.StatusCode)

return ErrBatchFailed
}
Expand All @@ -204,7 +220,7 @@ func (c *client) flush() error {
end = length
}

var events []*Event
var events []Event

events, c.events = c.events[0:end], c.events[end:]

Expand Down Expand Up @@ -245,7 +261,7 @@ func (c *client) Enqueue(event *Event) (err error) {
}
}()

c.msgs <- event
c.msgs <- *event

if len(c.msgs) == cap(c.msgs) {
go func() {
Expand Down
45 changes: 44 additions & 1 deletion payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

package amplitude

import "fmt"

type RequestPayload struct {
APIKey string `json:"api_key"`
Events []*Event `json:"events"`
Events []Event `json:"events"`
Options *PayloadOptions `json:"options,omitempty"`
}

Expand All @@ -19,3 +21,44 @@ type Payload struct {
Attempts int
Size int
}

/*
*
{
"code": 400,
"error": "Request missing required field",
"missing_field": "api_key",
"events_with_invalid_fields": {
"time": [
3,
4,
7
]
},
"events_with_missing_fields": {
"event_type": [
3,
4,
7
]
}
}
*
*/
type ErrorResponse struct {
Code int `json:"code"`
ErrorMessage string `json:"error"`
MissingField string `json:"missing_field"`
}

func (e *ErrorResponse) Error() string {
msg := fmt.Sprintf("%d: %s", e.Code, e.ErrorMessage)

if e.MissingField != "" {
msg = fmt.Sprintf("%s: missing: %s", msg, e.MissingField)
}

return msg
}

0 comments on commit f77b6fc

Please sign in to comment.