forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 110
/
use_ststoken_demo.go
54 lines (49 loc) · 1.9 KB
/
use_ststoken_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
package main
import (
"fmt"
"os"
"os/signal"
"sync"
"time"
"github.com/aliyun/aliyun-log-go-sdk/producer"
)
func main() {
producerConfig := producer.GetDefaultProducerConfig()
producerConfig.Endpoint = os.Getenv("Endpoint")
producerConfig.AccessKeyID = os.Getenv("AccessKeyID")
producerConfig.AccessKeySecret = os.Getenv("AccessKeySecret")
//When the producer is closed, if the StsTokenShutDown parameter is not set to nil, it will actively call the close method to close the channel.
producerConfig.StsTokenShutDown = make(chan struct{})
producerConfig.UpdateStsToken = updateStsToken
producerInstance := producer.InitProducer(producerConfig)
ch := make(chan os.Signal)
signal.Notify(ch, os.Kill, os.Interrupt)
producerInstance.Start()
var m sync.WaitGroup
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.SendLog("project", "logstrore", "topic", "127.0.0.1", log)
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)
}
}
func updateStsToken() (accessKeyID, accessKeySecret, securityToken string, expireTime time.Time, err error) {
// 写入自己的获取的 ststoken和过期时间逻辑代码,producer会自动在ststoken到达过期时间的时候,重新执行该函数去获取最新的ststoken以及其过期时间。
// TODO 此处填入自己的获取ststoken 的逻辑
return accessKeyID, accessKeySecret, securityToken, expireTime, nil
}