Skip to content

Commit

Permalink
Release 2.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
周倩 committed May 7, 2022
1 parent def5754 commit 4766de8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions consumers/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package consumers

import (
"encoding/json"
"sync"
"time"

"github.com/sensorsdata/sa-sdk-go/structs"
Expand All @@ -34,6 +35,7 @@ type BatchConsumer struct {
DataBuffer []structs.EventData
ItemBuffer []structs.Item
Timeout time.Duration
lock sync.Mutex
}

func InitBatchConsumer(url string, max, timeout int) (*BatchConsumer, error) {
Expand All @@ -48,6 +50,8 @@ func InitBatchConsumer(url string, max, timeout int) (*BatchConsumer, error) {
}

func (c *BatchConsumer) Send(data structs.EventData) error {
c.lock.Lock()
defer c.lock.Unlock()
c.DataBuffer = append(c.DataBuffer, data)
if (len(c.DataBuffer) + len(c.ItemBuffer)) < c.Max {
return nil
Expand All @@ -65,9 +69,10 @@ func (c *BatchConsumer) Flush() error {
}

err = send(c.Url, string(jdata), c.Timeout, true)

c.DataBuffer = c.DataBuffer[:0]
return err
if err != nil {
return err
}
}

// 刷新 Item 数据
Expand All @@ -80,7 +85,9 @@ func (c *BatchConsumer) Flush() error {
err = send(c.Url, string(itemData), c.Timeout, true)

c.ItemBuffer = c.ItemBuffer[:0]
return err
if err != nil {
return err
}
}
return nil
}
Expand All @@ -90,6 +97,8 @@ func (c *BatchConsumer) Close() error {
}

func (c *BatchConsumer) ItemSend(item structs.Item) error {
c.lock.Lock()
defer c.lock.Unlock()
c.ItemBuffer = append(c.ItemBuffer, item)
if (len(c.DataBuffer) + len(c.ItemBuffer)) < c.Max {
return nil
Expand Down
2 changes: 1 addition & 1 deletion sensorsanalytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (
ITEM_SET = "item_set"
ITEM_DELETE = "item_delete"

SDK_VERSION = "2.0.5"
SDK_VERSION = "2.0.6"
LIB_NAME = "Golang"

MAX_ID_LEN = 255
Expand Down

0 comments on commit 4766de8

Please sign in to comment.