-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathredis.go
104 lines (87 loc) · 2.03 KB
/
redis.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
package sink
import (
"fmt"
"os"
"time"
"github.com/garyburd/redigo/redis"
log "github.com/sirupsen/logrus"
)
// RedisSink ...
type RedisSink struct {
pool *redis.Pool
key string
stopCh chan interface{}
putCh chan []byte
}
// NewStdout ...
func NewRedis() (*RedisSink, error) {
redisURL := os.Getenv("SINK_REDIS_URL")
if redisURL == "" {
return nil, fmt.Errorf("[sink/redis] Missing SINK_REDIS_URL (example: redis://[user]:[password]@127.0.0.1[:5672]/0)")
}
redisKey := os.Getenv("SINK_REDIS_KEY")
if redisKey == "" {
return nil, fmt.Errorf("[sink/redis] Missing SINK_REDIS_KEY (example: my-key")
}
redisPool := redis.Pool{
MaxIdle: 2,
MaxActive: 2,
IdleTimeout: time.Minute,
Dial: func() (redis.Conn, error) { return redis.DialURL(redisURL) },
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
return &RedisSink{
pool: &redisPool,
key: redisKey,
stopCh: make(chan interface{}),
putCh: make(chan []byte, 1000),
}, nil
}
// Start ...
func (s *RedisSink) Start() error {
// Stop chan for all tasks to depend on
s.stopCh = make(chan interface{})
go s.write()
// wait forever for a stop signal to happen
for {
select {
case <-s.stopCh:
break
}
break
}
return nil
}
// Stop ...
func (s *RedisSink) Stop() {
log.Debugf("[sink/redis] ensure writer queue is empty (%d messages left)", len(s.putCh))
for len(s.putCh) > 0 {
log.Debugf("[sink/redis] Waiting for queue to drain - (%d messages left)", len(s.putCh))
time.Sleep(1 * time.Second)
}
close(s.stopCh)
defer s.pool.Close()
}
// Put ..
func (s *RedisSink) Put(data []byte) error {
s.putCh <- data
return nil
}
func (s *RedisSink) write() {
log.Infof("[sink/redis] Starting writer to key '%s'", s.key)
for {
select {
case data := <-s.putCh:
conn := s.pool.Get()
if _, err := conn.Do("RPUSH", s.key, data); err != nil {
log.Infof("[sink/redis] %s", err)
} else {
log.Infof("[sink/redis] Published to key '%s'", s.key)
}
conn.Close()
}
}
}