-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoffsetManager.go
68 lines (58 loc) · 1.72 KB
/
offsetManager.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
65
66
67
68
package kago
import (
"github.com/Shopify/sarama"
"log"
)
type PartitionOffsetManager struct {
client sarama.Client
om sarama.OffsetManager
pom sarama.PartitionOffsetManager
}
func (pom *PartitionOffsetManager) MarkOffset(topic string, partition int32, offset int64, groupId string, ifExactOnce bool) {
if ifExactOnce {
fileOffset(topic, partition, offset, groupId)
}
pom.pom.MarkOffset(offset, "")
}
func (pom *PartitionOffsetManager) ResetOffset(topic string, partition int32, offset int64, groupId string, ifExactOnce bool) {
if ifExactOnce {
fileOffset(topic, partition, offset, groupId)
}
pom.pom.ResetOffset(offset, "")
}
func (pom *PartitionOffsetManager) NextOffset() (offset int64) {
offset, _ = pom.pom.NextOffset()
return offset
}
func (pom *PartitionOffsetManager) Close() (error, error) {
pom.pom.AsyncClose()
err := pom.om.Close()
err2 := pom.client.Close()
return err, err2
}
func (pom *PartitionOffsetManager) Errors() <-chan *ConsumerError {
return pom.pom.Errors()
}
func InitPartitionOffsetManager(addr []string, topic, groupId string, partition int32, conf *Config) (*PartitionOffsetManager, error) {
client, err := sarama.NewClient(addr, &conf.Config.Config)
if err != nil {
log.Println("client create error")
return nil, err
}
offsetManager, err := sarama.NewOffsetManagerFromClient(groupId, client)
if err != nil {
log.Println("offsetManager create error")
return nil, err
}
partitionOffsetManager, err := offsetManager.ManagePartition(topic, partition)
if err != nil {
log.Println("partitionOffsetManager create error")
return nil, err
}
var pom = PartitionOffsetManager{
client: client,
om: offsetManager,
pom: partitionOffsetManager,
}
return &pom, nil
}