summaryrefslogtreecommitdiff
path: root/plugins/redis/pubsub.go
blob: 4e41acb52ce76f9aedaf9ee2b7aac091bdf94089 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package redis

import (
	"context"
	"sync"

	"github.com/go-redis/redis/v8"
	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/pkg/pubsub"
	"github.com/spiral/roadrunner/v2/plugins/config"
	"github.com/spiral/roadrunner/v2/plugins/logger"
)

type PubSubDriver struct {
	sync.RWMutex
	cfg *Config `mapstructure:"redis"`

	log             logger.Logger
	channel         *redisChannel
	universalClient redis.UniversalClient
	stopCh          chan struct{}
}

func NewPubSubDriver(log logger.Logger, key string, cfgPlugin config.Configurer, stopCh chan struct{}) (pubsub.PubSub, error) {
	const op = errors.Op("new_pub_sub_driver")
	ps := &PubSubDriver{
		log:    log,
		stopCh: stopCh,
	}

	// will be different for every connected driver
	err := cfgPlugin.UnmarshalKey(key, &ps.cfg)
	if err != nil {
		return nil, errors.E(op, err)
	}

	ps.cfg.InitDefaults()

	ps.universalClient = redis.NewUniversalClient(&redis.UniversalOptions{
		Addrs:              ps.cfg.Addrs,
		DB:                 ps.cfg.DB,
		Username:           ps.cfg.Username,
		Password:           ps.cfg.Password,
		SentinelPassword:   ps.cfg.SentinelPassword,
		MaxRetries:         ps.cfg.MaxRetries,
		MinRetryBackoff:    ps.cfg.MaxRetryBackoff,
		MaxRetryBackoff:    ps.cfg.MaxRetryBackoff,
		DialTimeout:        ps.cfg.DialTimeout,
		ReadTimeout:        ps.cfg.ReadTimeout,
		WriteTimeout:       ps.cfg.WriteTimeout,
		PoolSize:           ps.cfg.PoolSize,
		MinIdleConns:       ps.cfg.MinIdleConns,
		MaxConnAge:         ps.cfg.MaxConnAge,
		PoolTimeout:        ps.cfg.PoolTimeout,
		IdleTimeout:        ps.cfg.IdleTimeout,
		IdleCheckFrequency: ps.cfg.IdleCheckFreq,
		ReadOnly:           ps.cfg.ReadOnly,
		RouteByLatency:     ps.cfg.RouteByLatency,
		RouteRandomly:      ps.cfg.RouteRandomly,
		MasterName:         ps.cfg.MasterName,
	})

	statusCmd := ps.universalClient.Ping(context.Background())
	if statusCmd.Err() != nil {
		return nil, statusCmd.Err()
	}

	ps.channel = newRedisChannel(ps.universalClient, log)

	ps.stop()

	return ps, nil
}

func (p *PubSubDriver) stop() {
	go func() {
		for range p.stopCh {
			_ = p.channel.stop()
			return
		}
	}()
}

func (p *PubSubDriver) Publish(msg *pubsub.Message) error {
	p.Lock()
	defer p.Unlock()

	f := p.universalClient.Publish(context.Background(), msg.Topic, msg.Payload)
	if f.Err() != nil {
		return f.Err()
	}

	return nil
}

func (p *PubSubDriver) PublishAsync(msg *pubsub.Message) {
	go func() {
		p.Lock()
		defer p.Unlock()

		f := p.universalClient.Publish(context.Background(), msg.Topic, msg.Payload)
		if f.Err() != nil {
			p.log.Error("redis publish", "error", f.Err())
		}
	}()
}

func (p *PubSubDriver) Subscribe(connectionID string, topics ...string) error {
	// just add a connection
	for i := 0; i < len(topics); i++ {
		// key - topic
		// value - connectionID
		hset := p.universalClient.SAdd(context.Background(), topics[i], connectionID)
		res, err := hset.Result()
		if err != nil {
			return err
		}
		if res == 0 {
			p.log.Warn("could not subscribe to the provided topic, you might be already subscribed to it", "connectionID", connectionID, "topic", topics[i])
			continue
		}
	}

	// and subscribe after
	return p.channel.sub(topics...)
}

func (p *PubSubDriver) Unsubscribe(connectionID string, topics ...string) error {
	// Remove topics from the storage
	for i := 0; i < len(topics); i++ {
		srem := p.universalClient.SRem(context.Background(), topics[i], connectionID)
		if srem.Err() != nil {
			return srem.Err()
		}
	}

	for i := 0; i < len(topics); i++ {
		// if there are no such topics, we can safely unsubscribe from the redis
		exists := p.universalClient.Exists(context.Background(), topics[i])
		res, err := exists.Result()
		if err != nil {
			return err
		}

		// if we have associated connections - skip
		if res == 1 { // exists means that topic still exists and some other nodes may have connections associated with it
			continue
		}

		// else - unsubscribe
		err = p.channel.unsub(topics[i])
		if err != nil {
			return err
		}
	}

	return nil
}

func (p *PubSubDriver) Connections(topic string, res map[string]struct{}) {
	hget := p.universalClient.SMembersMap(context.Background(), topic)
	r, err := hget.Result()
	if err != nil {
		panic(err)
	}

	// assign connections
	// res expected to be from the sync.Pool
	for k := range r {
		res[k] = struct{}{}
	}
}

// Next return next message
func (p *PubSubDriver) Next() (*pubsub.Message, error) {
	return p.channel.message(), nil
}