-
Notifications
You must be signed in to change notification settings - Fork 172
/
emitter.go
130 lines (107 loc) · 3.24 KB
/
emitter.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package goka
import (
"errors"
"fmt"
"sync"
)
var (
// ErrEmitterAlreadyClosed is returned when Emit is called after the emitter has been finished.
ErrEmitterAlreadyClosed error = errors.New("emitter already closed")
)
// Emitter emits messages into a specific Kafka topic, first encoding the message with the given codec.
type Emitter struct {
codec Codec
producer Producer
topic string
defaultHeaders Headers
wg sync.WaitGroup
mu sync.RWMutex
done chan struct{}
}
// NewEmitter creates a new emitter using passed brokers, topic, codec and possibly options.
func NewEmitter(brokers []string, topic Stream, codec Codec, options ...EmitterOption) (*Emitter, error) {
options = append(
// default options comes first
[]EmitterOption{
WithEmitterClientID(fmt.Sprintf("goka-emitter-%s", topic)),
},
// user-defined options (may overwrite default ones)
options...,
)
opts := new(eoptions)
opts.applyOptions(topic, codec, options...)
prod, err := opts.builders.producer(brokers, opts.clientID, opts.hasher)
if err != nil {
return nil, fmt.Errorf(errBuildProducer, err)
}
return &Emitter{
codec: codec,
producer: prod,
topic: string(topic),
defaultHeaders: opts.defaultHeaders,
done: make(chan struct{}),
}, nil
}
func (e *Emitter) emitDone(err error) { e.wg.Done() }
// EmitWithHeaders sends a message with the given headers for the passed key using the emitter's codec.
func (e *Emitter) EmitWithHeaders(key string, msg interface{}, headers Headers) (*Promise, error) {
var (
err error
data []byte
)
if msg != nil {
data, err = e.codec.Encode(msg)
if err != nil {
return nil, fmt.Errorf("Error encoding value for key %s in topic %s: %v", key, e.topic, err)
}
}
// protect e.done channel and e.wg WaitGroup together to reject all new emits after calling e.Finish
// wg.Add must not be called after wg.Wait finished
e.mu.RLock()
select {
case <-e.done:
e.mu.RUnlock()
return NewPromise().finish(nil, ErrEmitterAlreadyClosed), nil
default:
e.wg.Add(1)
e.mu.RUnlock()
}
if headers == nil && e.defaultHeaders == nil {
return e.producer.Emit(e.topic, key, data).Then(e.emitDone), nil
}
return e.producer.EmitWithHeaders(e.topic, key, data, e.defaultHeaders.Merged(headers)).Then(e.emitDone), nil
}
// Emit sends a message for passed key using the emitter's codec.
func (e *Emitter) Emit(key string, msg interface{}) (*Promise, error) {
return e.EmitWithHeaders(key, msg, nil)
}
// EmitSyncWithHeaders sends a message with the given headers to passed topic and key.
func (e *Emitter) EmitSyncWithHeaders(key string, msg interface{}, headers Headers) error {
var (
err error
promise *Promise
)
promise, err = e.EmitWithHeaders(key, msg, headers)
if err != nil {
return err
}
done := make(chan struct{})
promise.Then(func(asyncErr error) {
err = asyncErr
close(done)
})
<-done
return err
}
// EmitSync sends a message to passed topic and key.
func (e *Emitter) EmitSync(key string, msg interface{}) error {
return e.EmitSyncWithHeaders(key, msg, nil)
}
// Finish waits until the emitter is finished producing all pending messages.
func (e *Emitter) Finish() error {
e.mu.Lock()
close(e.done)
e.mu.Unlock()
e.wg.Wait()
return e.producer.Close()
}