-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproducer.go
78 lines (63 loc) · 1.62 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
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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"sync"
"github.com/Shopify/sarama"
"github.com/cloudfoundry/sonde-go/events"
"golang.org/x/net/context"
)
type NozzleProducer interface {
// Produce produces firehose events
Produce(context.Context, <-chan *events.Envelope)
// Errors returns error channel
Errors() <-chan *sarama.ProducerError
// Success returns sarama.ProducerMessage
Successes() <-chan *sarama.ProducerMessage
// Close shuts down the producer and flushes any messages it may have buffered.
Close() error
}
var defaultLogger = log.New(ioutil.Discard, "", log.LstdFlags)
// LogProducer implements NozzleProducer interfaces.
// This producer is mainly used for debugging reason.
type LogProducer struct {
Logger *log.Logger
once sync.Once
}
func NewLogProducer(logger *log.Logger) NozzleProducer {
return &LogProducer{
Logger: logger,
}
}
// init sets default logger
func (p *LogProducer) init() {
if p.Logger == nil {
p.Logger = defaultLogger
}
}
func (p *LogProducer) Produce(ctx context.Context, eventCh <-chan *events.Envelope) {
p.once.Do(p.init)
for {
select {
case event := <-eventCh:
buf, _ := json.Marshal(event)
p.Logger.Printf("[INFO] %s", string(buf))
case <-ctx.Done():
p.Logger.Printf("[INFO] Stop producer")
return
}
}
}
func (p *LogProducer) Errors() <-chan *sarama.ProducerError {
errCh := make(chan *sarama.ProducerError, 1)
return errCh
}
func (p *LogProducer) Successes() <-chan *sarama.ProducerMessage {
msgCh := make(chan *sarama.ProducerMessage)
return msgCh
}
func (p *LogProducer) Close() error {
// Nothing to close for thi producer
return nil
}