-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtailer.go
60 lines (49 loc) · 1.16 KB
/
tailer.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
package main
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/service/kinesis"
)
func readShard(messages chan string, client *kinesis.Kinesis, streamName string, shardId string) {
println("Shard Id: " + shardId)
iter, err := getShardIterator(client, streamName, shardId)
if err != nil {
println(err)
panic(err)
}
for {
getParams := kinesis.GetRecordsInput{
ShardIterator: iter,
}
resp, err := client.GetRecords(&getParams)
if err != nil {
fmt.Println(err.Error())
return
}
for _, r := range resp.Records {
messages <- fmt.Sprintf("[%s] %s: %s", shardId, *r.SequenceNumber, r.Data)
}
iter = resp.NextShardIterator
if len(resp.Records) == 0 {
time.Sleep(1000 * time.Millisecond)
}
}
}
// TailStream tails the given stream
// FORMAT: SEQUENCE_ID: DATA
func TailStream(streamName string) {
println("FORMAT -> SEQUENCE_ID: DATA")
client := newClient(newSession())
shardIds, err := getShards(client, streamName)
if err != nil {
println("Err")
panic(err)
}
messages := make(chan string, 1000)
for _, shardId := range shardIds {
go readShard(messages, client, streamName, shardId)
}
for {
fmt.Println(<-messages)
}
}