| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package database |
| 5 |
|
| 6 |
import ( |
| 7 |
"sync" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/gomodule/redigo/redis" |
| 11 |
"github.com/op/go-logging" |
| 12 |
) |
| 13 |
|
| 14 |
var ( |
| 15 |
log = logging.MustGetLogger("main") |
| 16 |
) |
| 17 |
|
| 18 |
type pubsubEvent string |
| 19 |
|
| 20 |
const ( |
| 21 |
CLUSTER pubsubEvent = "_mirrorbits_cluster" |
| 22 |
FILE_UPDATE pubsubEvent = "_mirrorbits_file_update" |
| 23 |
MIRROR_UPDATE pubsubEvent = "_mirrorbits_mirror_update" |
| 24 |
MIRROR_FILE_UPDATE pubsubEvent = "_mirrorbits_mirror_file_update" |
| 25 |
|
| 26 |
PUBSUB_RECONNECTED pubsubEvent = "_mirrorbits_pubsub_reconnected" |
| 27 |
) |
| 28 |
|
| 29 |
// Pubsub is the internal structure of the publish/subscribe handler |
| 30 |
type Pubsub struct { |
| 31 |
r *Redis |
| 32 |
rconn redis.Conn |
| 33 |
connlock sync.Mutex |
| 34 |
extSubscribers map[string][]chan string |
| 35 |
extSubscribersLock sync.RWMutex |
| 36 |
stop chan bool |
| 37 |
wg sync.WaitGroup |
| 38 |
} |
| 39 |
|
| 40 |
// NewPubsub returns a new instance of the publish/subscribe handler |
| 41 |
func NewPubsub(r *Redis) *Pubsub { |
| 42 |
pubsub := new(Pubsub) |
| 43 |
pubsub.r = r |
| 44 |
pubsub.stop = make(chan bool) |
| 45 |
pubsub.extSubscribers = make(map[string][]chan string) |
| 46 |
go pubsub.updateEvents() |
| 47 |
return pubsub |
| 48 |
} |
| 49 |
|
| 50 |
// Close all the connections to the pubsub server |
| 51 |
func (p *Pubsub) Close() { |
| 52 |
close(p.stop) |
| 53 |
p.connlock.Lock() |
| 54 |
if p.rconn != nil { |
| 55 |
// FIXME Calling p.rconn.Close() here will block indefinitely in redigo |
| 56 |
p.rconn.Send("UNSUBSCRIBE") |
| 57 |
p.rconn.Send("QUIT") |
| 58 |
p.rconn.Flush() |
| 59 |
} |
| 60 |
p.connlock.Unlock() |
| 61 |
p.wg.Wait() |
| 62 |
} |
| 63 |
|
| 64 |
// SubscribeEvent allows subscription to a particular kind of events and receive a |
| 65 |
// notification when an event is dispatched on the given channel. |
| 66 |
func (p *Pubsub) SubscribeEvent(event pubsubEvent, channel chan string) { |
| 67 |
p.extSubscribersLock.Lock() |
| 68 |
defer p.extSubscribersLock.Unlock() |
| 69 |
|
| 70 |
listeners := p.extSubscribers[string(event)] |
| 71 |
listeners = append(listeners, channel) |
| 72 |
p.extSubscribers[string(event)] = listeners |
| 73 |
} |
| 74 |
|
| 75 |
func (p *Pubsub) updateEvents() { |
| 76 |
p.wg.Add(1) |
| 77 |
defer p.wg.Done() |
| 78 |
disconnected := false |
| 79 |
connect: |
| 80 |
for { |
| 81 |
select { |
| 82 |
case <-p.stop: |
| 83 |
return |
| 84 |
default: |
| 85 |
} |
| 86 |
p.connlock.Lock() |
| 87 |
p.rconn = p.r.Get() |
| 88 |
if _, err := p.rconn.Do("PING"); err != nil { |
| 89 |
disconnected = true |
| 90 |
p.rconn.Close() |
| 91 |
p.rconn = nil |
| 92 |
p.connlock.Unlock() |
| 93 |
if RedisIsLoading(err) { |
| 94 |
// Doing a PING after (re-connection) prevents cases where redis |
| 95 |
// is currently loading the dataset and is still not ready. |
| 96 |
log.Warning("Redis is still loading the dataset in memory") |
| 97 |
} |
| 98 |
time.Sleep(500 * time.Millisecond) |
| 99 |
continue |
| 100 |
} |
| 101 |
p.connlock.Unlock() |
| 102 |
log.Debug("Subscribing pubsub") |
| 103 |
psc := redis.PubSubConn{Conn: p.rconn} |
| 104 |
|
| 105 |
psc.Subscribe(CLUSTER) |
| 106 |
psc.Subscribe(FILE_UPDATE) |
| 107 |
psc.Subscribe(MIRROR_UPDATE) |
| 108 |
psc.Subscribe(MIRROR_FILE_UPDATE) |
| 109 |
|
| 110 |
if disconnected == true { |
| 111 |
// This is a way to keep the cache active while disconnected |
| 112 |
// from redis but still clear the cache (possibly outdated) |
| 113 |
// after a successful reconnection. |
| 114 |
disconnected = false |
| 115 |
p.handleMessage(string(PUBSUB_RECONNECTED), nil) |
| 116 |
} |
| 117 |
for { |
| 118 |
switch v := psc.Receive().(type) { |
| 119 |
case redis.Message: |
| 120 |
//log.Debugf("Redis message on channel %s: message: %s", v.Channel, v.Data) |
| 121 |
p.handleMessage(v.Channel, v.Data) |
| 122 |
case redis.Subscription: |
| 123 |
log.Debugf("Redis subscription on channel %s: %s (%d)", v.Channel, v.Kind, v.Count) |
| 124 |
case error: |
| 125 |
select { |
| 126 |
case <-p.stop: |
| 127 |
return |
| 128 |
default: |
| 129 |
} |
| 130 |
log.Errorf("Pubsub disconnected: %s", v) |
| 131 |
psc.Close() |
| 132 |
p.rconn.Close() |
| 133 |
time.Sleep(50 * time.Millisecond) |
| 134 |
disconnected = true |
| 135 |
goto connect |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// Notify subscribers of the new message |
| 142 |
func (p *Pubsub) handleMessage(channel string, data []byte) { |
| 143 |
p.extSubscribersLock.RLock() |
| 144 |
defer p.extSubscribersLock.RUnlock() |
| 145 |
|
| 146 |
listeners := p.extSubscribers[channel] |
| 147 |
for _, listener := range listeners { |
| 148 |
// Block if the listener is not available |
| 149 |
listener <- string(data) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
// Publish a message on the pubsub server |
| 154 |
func Publish(r redis.Conn, event pubsubEvent, message string) error { |
| 155 |
_, err := r.Do("PUBLISH", string(event), message) |
| 156 |
return err |
| 157 |
} |
| 158 |
|
| 159 |
// SendPublish add the message to a transaction |
| 160 |
func SendPublish(r redis.Conn, event pubsubEvent, message string) error { |
| 161 |
err := r.Send("PUBLISH", string(event), message) |
| 162 |
return err |
| 163 |
} |