2020-04-08 07:56:30 +02:00
|
|
|
package inputreader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"github.com/gomodule/redigo/redis"
|
2020-05-27 16:52:19 +02:00
|
|
|
"io"
|
2020-04-08 07:56:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// RedisLPOPReader is a abstraction of LPOP list
|
|
|
|
// and behaves like a reader
|
|
|
|
type RedisLPOPReader struct {
|
|
|
|
// D4 redis connection
|
|
|
|
r *redis.Conn
|
|
|
|
// D4 redis database
|
|
|
|
d int
|
|
|
|
// D4 Queue storing
|
|
|
|
q string
|
|
|
|
// Current buffer
|
|
|
|
buf []byte
|
|
|
|
}
|
|
|
|
|
2020-04-08 16:22:16 +02:00
|
|
|
// NewLPOPReader creates a new RedisLPOPReader
|
2020-05-27 16:52:19 +02:00
|
|
|
func NewLPOPReader(rc *redis.Conn, db int, queue string) (*RedisLPOPReader, error) {
|
2020-04-08 07:56:30 +02:00
|
|
|
rr := *rc
|
|
|
|
|
|
|
|
if _, err := rr.Do("SELECT", db); err != nil {
|
|
|
|
rr.Close()
|
2020-04-08 16:22:16 +02:00
|
|
|
return nil, err
|
2020-04-08 07:56:30 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 16:22:16 +02:00
|
|
|
r := &RedisLPOPReader{
|
2020-05-27 16:52:19 +02:00
|
|
|
r: rc,
|
|
|
|
d: db,
|
|
|
|
q: queue,
|
2020-04-08 07:56:30 +02:00
|
|
|
}
|
2020-04-08 16:22:16 +02:00
|
|
|
|
|
|
|
return r, nil
|
2020-04-08 07:56:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read LPOP the redis queue and use a bytes reader to copy
|
|
|
|
// the resulting data in p
|
|
|
|
func (rl *RedisLPOPReader) Read(p []byte) (n int, err error) {
|
|
|
|
rr := *rl.r
|
|
|
|
|
|
|
|
buf, err := redis.Bytes(rr.Do("LPOP", rl.q))
|
|
|
|
// If redis return empty: EOF (user should not stop)
|
|
|
|
if err == redis.ErrNil {
|
|
|
|
return 0, io.EOF
|
|
|
|
} else if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-05-25 16:48:18 +02:00
|
|
|
buf = append(buf, "\n"...)
|
2020-04-08 07:56:30 +02:00
|
|
|
rreader := bytes.NewReader(buf)
|
|
|
|
n, err = rreader.Read(p)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Teardown is called on error to close the redis connection
|
|
|
|
func (rl *RedisLPOPReader) Teardown() {
|
|
|
|
(*rl.r).Close()
|
|
|
|
}
|