-
Notifications
You must be signed in to change notification settings - Fork 2
/
producer.go
45 lines (37 loc) · 1.11 KB
/
producer.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
package kafkaclient
import (
"time"
"github.com/confluentinc/confluent-kafka-go/kafka"
)
// Producer promotes the producer of kafka client.
type Producer struct {
*kafka.Producer
}
// TimeoutFlush specifies the flush's timeout for the producer.
var TimeoutFlush = 5 * time.Second
// GetOrigin returns the producer origin from kafka.
func (p *Producer) GetOrigin() *kafka.Producer {
return p.Producer
}
// Close close the underlying resources.
func (p *Producer) Close() (err error) {
p.Producer.Flush(int(TimeoutFlush.Milliseconds()))
p.Producer.Close()
return
}
// Publish publishes message synchronously to the kafka's brokers.
func (p *Producer) Publish(msg *kafka.Message) (event kafka.Event, err error) {
eventChan := make(chan kafka.Event, 1)
defer close(eventChan)
err = p.Produce(msg, eventChan)
if err != nil {
return
}
event = <-eventChan
return
}
// PublishAsync publishes message asynchronously to the kafka's brokers.
func (p *Producer) PublishAsync(msg *kafka.Message, eventChan chan kafka.Event) (err error) {
err = p.Produce(msg, eventChan)
return
}