1 Star 0 Fork 0

qw_1215/glink

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
redis.go 1.08 KB
一键复制 编辑 原始数据 按行查看 历史
qw_1215 提交于 2020-03-12 22:26 +08:00 . 项目初始化
package source
import (
"github.com/go-redis/redis"
streams "gitee.com/qw_1215/glink"
"gitee.com/qw_1215/glink/flow"
)
// RedisSource implements Redis Pub/Sub Source
type RedisSource struct {
redisdb *redis.Client
channel string
out chan interface{}
}
// NewRedisSource returns new RedisSource instance
func NewRedisSource(config *redis.Options, channel string) (*RedisSource, error) {
redisdb := redis.NewClient(config)
pubsub := redisdb.Subscribe(channel)
// Wait for confirmation that subscription is created before publishing anything
if _, err := pubsub.Receive(); err != nil {
return nil, err
}
source := &RedisSource{
redisdb,
channel,
make(chan interface{}),
}
go source.init(pubsub.Channel())
return source, nil
}
func (rs *RedisSource) init(ch <-chan *redis.Message) {
for msg := range ch {
rs.out <- msg
}
}
// Via streams data through given flow
func (rs *RedisSource) Via(_flow streams.Flow) streams.Flow {
flow.DoStream(rs, _flow)
return _flow
}
// Out returns channel for sending data
func (rs *RedisSource) Out() <-chan interface{} {
return rs.out
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/qw_1215/glink.git
git@gitee.com:qw_1215/glink.git
qw_1215
glink
glink
195e12e86392

搜索帮助