-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add evsender * Add event * Add map to events * Update client.go * Update client.go * Add params * Update log.go * Update client.go * Revert "Update client.go" This reverts commit 01aa335. * Use []Event * Rename evsender => event * Rename to eventer
- Loading branch information
Showing
4 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package eventer | ||
|
||
import ( | ||
"github.com/trustwallet/golibs/client" | ||
"github.com/trustwallet/golibs/network/middleware" | ||
) | ||
|
||
type Client struct { | ||
client.Request | ||
} | ||
|
||
type Status struct { | ||
Status bool `json:"status"` | ||
} | ||
|
||
type Event struct { | ||
Name string `json:"name"` | ||
CreatedAt int64 `json:"created_at"` | ||
Params map[string]string `json:"params"` | ||
} | ||
|
||
var senderClient *Client | ||
var batchLimit = 100 | ||
|
||
func Init(url string, limit int) { | ||
senderClient = &Client{client.InitJSONClient(url, middleware.SentryErrorHandler)} | ||
batchLimit = limit | ||
} | ||
|
||
func (c Client) SendBatch(events []Event) (status Status, err error) { | ||
err = senderClient.Post(&status, "", events) | ||
return | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package eventer | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var events []Event | ||
var eventsMux = sync.RWMutex{} | ||
|
||
func Log(event Event) { | ||
eventsMux.Lock() | ||
defer func() { | ||
eventsMux.Unlock() | ||
}() | ||
if event.CreatedAt == 0 { | ||
event.CreatedAt = time.Now().Unix() | ||
} | ||
events = append(events, event) | ||
|
||
if len(events) >= batchLimit { | ||
go sendEvents(events) | ||
events = nil | ||
} | ||
} | ||
|
||
func sendEvents(events []Event) { | ||
if senderClient == nil { | ||
return | ||
} | ||
_, err := senderClient.SendBatch(events) | ||
if err != nil { | ||
log.Error(err) | ||
return | ||
} | ||
} |
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
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