-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafka-blocker.go
71 lines (65 loc) · 1.44 KB
/
kafka-blocker.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
69
70
71
package main
import (
"context"
"log"
"sync"
"github.com/google/uuid"
kafka "github.com/segmentio/kafka-go"
kafkaProtocol "github.com/segmentio/kafka-go/protocol"
)
type KafkaBlocker struct {
*kafka.Reader
mutex *sync.RWMutex
blockMap map[uuid.UUID]chan struct{}
}
func NewKafkaBlocker(reader *kafka.Reader) *KafkaBlocker {
return &KafkaBlocker{
Reader: reader,
mutex: &sync.RWMutex{},
blockMap: make(map[uuid.UUID]chan struct{}),
}
}
func (kb *KafkaBlocker) Block(uuid uuid.UUID) <-chan struct{} {
kb.mutex.Lock()
ch := make(chan struct{})
kb.blockMap[uuid] = ch
kb.mutex.Unlock()
return ch
}
func findHeader(key string, headers []kafkaProtocol.Header) (kafkaProtocol.Header, bool) {
for _, h := range headers {
if h.Key == key {
return h, true
}
}
return kafka.Header{}, false
}
func (kb *KafkaBlocker) Run(ctx context.Context) {
for {
m, err := kb.Reader.ReadMessage(ctx)
if err != nil {
continue
}
log.Println("got message")
header, ok := findHeader("request-id", m.Headers)
if !ok {
continue
}
log.Println("found header", header)
bytes := header.Value
messageUUID, err := uuid.ParseBytes(bytes)
if err != nil {
continue
}
log.Println("got bytes", messageUUID)
kb.mutex.RLock()
log.Println("map", kb.blockMap[messageUUID])
kb.mutex.RUnlock()
if ch, ok := kb.blockMap[messageUUID]; ok {
close(ch)
kb.mutex.Lock()
delete(kb.blockMap, messageUUID)
kb.mutex.Unlock()
}
}
}