forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 110
/
simple_callback_demo.go
64 lines (58 loc) · 1.75 KB
/
simple_callback_demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"os"
"os/signal"
"sync"
"time"
"github.com/aliyun/aliyun-log-go-sdk/producer"
)
type Callback struct {
}
func (callback *Callback) Success(result *producer.Result) {
attemptList := result.GetReservedAttempts()
for _, attempt := range attemptList {
fmt.Println(attempt)
}
}
func (callback *Callback) Fail(result *producer.Result) {
fmt.Println(result.IsSuccessful())
fmt.Println(result.GetErrorCode())
fmt.Println(result.GetErrorMessage())
fmt.Println(result.GetReservedAttempts())
fmt.Println(result.GetRequestId())
fmt.Println(result.GetTimeStampMs())
}
func main() {
producerConfig := producer.GetDefaultProducerConfig()
producerConfig.Endpoint = os.Getenv("Endpoint")
producerConfig.AccessKeyID = os.Getenv("AccessKeyID")
producerConfig.AccessKeySecret = os.Getenv("AccessKeySecret")
producerInstance := producer.InitProducer(producerConfig)
ch := make(chan os.Signal)
signal.Notify(ch, os.Kill, os.Interrupt)
producerInstance.Start()
var m sync.WaitGroup
callBack := &Callback{}
for i := 0; i < 10; i++ {
m.Add(1)
go func() {
defer m.Done()
for i := 0; i < 1000; i++ {
// GenerateLog is producer's function for generating SLS format logs
// GenerateLog has low performance, and native Log interface is the best choice for high performance.
log := producer.GenerateLog(uint32(time.Now().Unix()), map[string]string{"content": "test", "content2": fmt.Sprintf("%v", i)})
err := producerInstance.SendLogWithCallBack("project", "logstrore", "topic", "127.0.0.1", log, callBack)
if err != nil {
fmt.Println(err)
}
}
}()
}
m.Wait()
fmt.Println("Send completion")
if _, ok := <-ch; ok {
fmt.Println("Get the shutdown signal and start to shut down")
producerInstance.Close(60000)
}
}