2018-10-29 01:49:45 +03:00
|
|
|
package server
|
2018-08-14 03:05:30 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/tidwall/match"
|
|
|
|
"github.com/tidwall/redcon"
|
|
|
|
"github.com/tidwall/resp"
|
2018-10-11 00:25:40 +03:00
|
|
|
"github.com/tidwall/tile38/internal/log"
|
2018-08-14 03:05:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
pubsubChannel = iota
|
|
|
|
pubsubPattern
|
|
|
|
)
|
|
|
|
|
|
|
|
type pubsub struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
hubs [2]map[string]*subhub
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPubsub() *pubsub {
|
|
|
|
return &pubsub{
|
|
|
|
hubs: [2]map[string]*subhub{
|
|
|
|
make(map[string]*subhub),
|
|
|
|
make(map[string]*subhub),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish a message to subscribers
|
2018-10-29 01:49:45 +03:00
|
|
|
func (c *Server) Publish(channel string, message ...string) int {
|
2018-08-14 03:05:30 +03:00
|
|
|
var msgs []submsg
|
|
|
|
c.pubsub.mu.RLock()
|
|
|
|
if hub := c.pubsub.hubs[pubsubChannel][channel]; hub != nil {
|
|
|
|
for target := range hub.targets {
|
|
|
|
for _, message := range message {
|
|
|
|
msgs = append(msgs, submsg{
|
|
|
|
kind: pubsubChannel,
|
|
|
|
target: target,
|
|
|
|
channel: channel,
|
|
|
|
message: message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for pattern, hub := range c.pubsub.hubs[pubsubPattern] {
|
|
|
|
if match.Match(channel, pattern) {
|
|
|
|
for target := range hub.targets {
|
|
|
|
for _, message := range message {
|
|
|
|
msgs = append(msgs, submsg{
|
|
|
|
kind: pubsubPattern,
|
|
|
|
target: target,
|
|
|
|
channel: channel,
|
|
|
|
pattern: pattern,
|
|
|
|
message: message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.pubsub.mu.RUnlock()
|
|
|
|
|
|
|
|
for _, msg := range msgs {
|
|
|
|
msg.target.cond.L.Lock()
|
|
|
|
msg.target.msgs = append(msg.target.msgs, msg)
|
|
|
|
msg.target.cond.Broadcast()
|
|
|
|
msg.target.cond.L.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(msgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *pubsub) register(kind int, channel string, target *subtarget) {
|
|
|
|
ps.mu.Lock()
|
|
|
|
hub, ok := ps.hubs[kind][channel]
|
|
|
|
if !ok {
|
|
|
|
hub = newSubhub()
|
|
|
|
ps.hubs[kind][channel] = hub
|
|
|
|
}
|
|
|
|
hub.targets[target] = true
|
|
|
|
ps.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *pubsub) unregister(kind int, channel string, target *subtarget) {
|
|
|
|
ps.mu.Lock()
|
|
|
|
hub, ok := ps.hubs[kind][channel]
|
|
|
|
if ok {
|
|
|
|
delete(hub.targets, target)
|
|
|
|
if len(hub.targets) == 0 {
|
|
|
|
delete(ps.hubs[kind], channel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ps.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
type submsg struct {
|
|
|
|
kind byte
|
|
|
|
target *subtarget
|
|
|
|
pattern string
|
|
|
|
channel string
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
type subtarget struct {
|
|
|
|
cond *sync.Cond
|
|
|
|
msgs []submsg
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSubtarget() *subtarget {
|
|
|
|
target := new(subtarget)
|
|
|
|
target.cond = sync.NewCond(&sync.Mutex{})
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
|
|
|
|
type subhub struct {
|
|
|
|
targets map[*subtarget]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSubhub() *subhub {
|
|
|
|
hub := new(subhub)
|
|
|
|
hub.targets = make(map[*subtarget]bool)
|
|
|
|
return hub
|
|
|
|
}
|
|
|
|
|
|
|
|
type liveSubscriptionSwitches struct {
|
2018-10-29 01:49:45 +03:00
|
|
|
// no fields. everything is managed through the Message
|
2018-08-14 03:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sub liveSubscriptionSwitches) Error() string {
|
|
|
|
return goingLive
|
|
|
|
}
|
|
|
|
|
2018-10-29 01:49:45 +03:00
|
|
|
func (c *Server) cmdSubscribe(msg *Message) (resp.Value, error) {
|
|
|
|
if len(msg.Args) < 2 {
|
2018-08-14 03:05:30 +03:00
|
|
|
return resp.Value{}, errInvalidNumberOfArguments
|
|
|
|
}
|
2018-10-29 01:49:45 +03:00
|
|
|
return NOMessage, liveSubscriptionSwitches{}
|
2018-08-14 03:05:30 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 01:49:45 +03:00
|
|
|
func (c *Server) cmdPsubscribe(msg *Message) (resp.Value, error) {
|
|
|
|
if len(msg.Args) < 2 {
|
2018-08-14 03:05:30 +03:00
|
|
|
return resp.Value{}, errInvalidNumberOfArguments
|
|
|
|
}
|
2018-10-29 01:49:45 +03:00
|
|
|
return NOMessage, liveSubscriptionSwitches{}
|
2018-08-14 03:05:30 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 01:49:45 +03:00
|
|
|
func (c *Server) cmdPublish(msg *Message) (resp.Value, error) {
|
2018-08-14 03:05:30 +03:00
|
|
|
start := time.Now()
|
2018-10-29 01:49:45 +03:00
|
|
|
if len(msg.Args) != 3 {
|
2018-08-14 03:05:30 +03:00
|
|
|
return resp.Value{}, errInvalidNumberOfArguments
|
|
|
|
}
|
|
|
|
|
2018-10-29 01:49:45 +03:00
|
|
|
channel := msg.Args[1]
|
|
|
|
message := msg.Args[2]
|
2018-08-14 03:05:30 +03:00
|
|
|
//geofence := gjson.Valid(message) && gjson.Get(message, "fence").Bool()
|
|
|
|
n := c.Publish(channel, message) //, geofence)
|
|
|
|
var res resp.Value
|
|
|
|
switch msg.OutputType {
|
2018-10-29 01:49:45 +03:00
|
|
|
case JSON:
|
2018-08-14 03:05:30 +03:00
|
|
|
res = resp.StringValue(`{"ok":true` +
|
|
|
|
`,"published":` + strconv.FormatInt(int64(n), 10) +
|
|
|
|
`,"elapsed":"` + time.Now().Sub(start).String() + `"}`)
|
2018-10-29 01:49:45 +03:00
|
|
|
case RESP:
|
2018-08-14 03:05:30 +03:00
|
|
|
res = resp.IntegerValue(n)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2018-10-29 01:49:45 +03:00
|
|
|
func (c *Server) liveSubscription(
|
2018-08-14 03:05:30 +03:00
|
|
|
conn net.Conn,
|
2018-10-29 01:49:45 +03:00
|
|
|
rd *PipelineReader,
|
|
|
|
msg *Message,
|
2018-08-14 03:05:30 +03:00
|
|
|
websocket bool,
|
|
|
|
) error {
|
|
|
|
defer conn.Close() // close connection when we are done
|
|
|
|
|
|
|
|
outputType := msg.OutputType
|
|
|
|
connType := msg.ConnType
|
|
|
|
if websocket {
|
2018-10-29 01:49:45 +03:00
|
|
|
outputType = JSON
|
2018-08-14 03:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var start time.Time
|
|
|
|
|
|
|
|
// write helpers
|
|
|
|
var writeLock sync.Mutex
|
|
|
|
write := func(data []byte) {
|
|
|
|
writeLock.Lock()
|
|
|
|
defer writeLock.Unlock()
|
|
|
|
writeLiveMessage(conn, data, false, connType, websocket)
|
|
|
|
}
|
|
|
|
writeOK := func() {
|
|
|
|
switch outputType {
|
2018-10-29 01:49:45 +03:00
|
|
|
case JSON:
|
2018-08-14 03:05:30 +03:00
|
|
|
write([]byte(`{"ok":true` +
|
|
|
|
`,"elapsed":"` + time.Now().Sub(start).String() + `"}`))
|
2018-10-29 01:49:45 +03:00
|
|
|
case RESP:
|
2019-06-04 19:19:12 +03:00
|
|
|
write([]byte("+OK\r\n"))
|
2018-08-14 03:05:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
writeWrongNumberOfArgsErr := func(command string) {
|
|
|
|
switch outputType {
|
2018-10-29 01:49:45 +03:00
|
|
|
case JSON:
|
2018-08-14 03:05:30 +03:00
|
|
|
write([]byte(`{"ok":false,"err":"invalid number of arguments"` +
|
|
|
|
`,"elapsed":"` + time.Now().Sub(start).String() + `"}`))
|
2018-10-29 01:49:45 +03:00
|
|
|
case RESP:
|
2019-06-04 19:19:12 +03:00
|
|
|
write([]byte("-ERR wrong number of arguments " +
|
|
|
|
"for '" + command + "' command\r\n"))
|
2018-08-14 03:05:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
writeOnlyPubsubErr := func() {
|
|
|
|
switch outputType {
|
2018-10-29 01:49:45 +03:00
|
|
|
case JSON:
|
2018-08-14 03:05:30 +03:00
|
|
|
write([]byte(`{"ok":false` +
|
|
|
|
`,"err":"only (P)SUBSCRIBE / (P)UNSUBSCRIBE / ` +
|
|
|
|
`PING / QUIT allowed in this context"` +
|
|
|
|
`,"elapsed":"` + time.Now().Sub(start).String() + `"}`))
|
2018-10-29 01:49:45 +03:00
|
|
|
case RESP:
|
2018-08-14 03:05:30 +03:00
|
|
|
write([]byte("-ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / " +
|
|
|
|
"PING / QUIT allowed in this context\r\n"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeSubscribe := func(command, channel string, num int) {
|
|
|
|
switch outputType {
|
2018-10-29 01:49:45 +03:00
|
|
|
case JSON:
|
2018-08-14 03:05:30 +03:00
|
|
|
write([]byte(`{"ok":true` +
|
|
|
|
`,"command":` + jsonString(command) +
|
|
|
|
`,"channel":` + jsonString(channel) +
|
|
|
|
`,"num":` + strconv.FormatInt(int64(num), 10) +
|
|
|
|
`,"elapsed":"` + time.Now().Sub(start).String() + `"}`))
|
2018-10-29 01:49:45 +03:00
|
|
|
case RESP:
|
2018-08-14 03:05:30 +03:00
|
|
|
b := redcon.AppendArray(nil, 3)
|
|
|
|
b = redcon.AppendBulkString(b, command)
|
|
|
|
b = redcon.AppendBulkString(b, channel)
|
|
|
|
b = redcon.AppendInt(b, int64(num))
|
|
|
|
write(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeMessage := func(msg submsg) {
|
|
|
|
if msg.kind == pubsubChannel {
|
|
|
|
switch outputType {
|
2018-10-29 01:49:45 +03:00
|
|
|
case JSON:
|
2018-08-14 03:05:30 +03:00
|
|
|
var data []byte
|
|
|
|
if !gjson.Valid(msg.message) {
|
|
|
|
data = appendJSONString(nil, msg.message)
|
|
|
|
} else {
|
|
|
|
data = []byte(msg.message)
|
|
|
|
}
|
|
|
|
write(data)
|
2018-10-29 01:49:45 +03:00
|
|
|
case RESP:
|
2018-08-14 03:05:30 +03:00
|
|
|
b := redcon.AppendArray(nil, 3)
|
|
|
|
b = redcon.AppendBulkString(b, "message")
|
|
|
|
b = redcon.AppendBulkString(b, msg.channel)
|
|
|
|
b = redcon.AppendBulkString(b, msg.message)
|
|
|
|
write(b)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch outputType {
|
2018-10-29 01:49:45 +03:00
|
|
|
case JSON:
|
2018-08-14 03:05:30 +03:00
|
|
|
var data []byte
|
|
|
|
if !gjson.Valid(msg.message) {
|
|
|
|
data = appendJSONString(nil, msg.message)
|
|
|
|
} else {
|
|
|
|
data = []byte(msg.message)
|
|
|
|
}
|
|
|
|
write(data)
|
2018-10-29 01:49:45 +03:00
|
|
|
case RESP:
|
2018-08-14 03:05:30 +03:00
|
|
|
b := redcon.AppendArray(nil, 4)
|
|
|
|
b = redcon.AppendBulkString(b, "pmessage")
|
|
|
|
b = redcon.AppendBulkString(b, msg.pattern)
|
|
|
|
b = redcon.AppendBulkString(b, msg.channel)
|
|
|
|
b = redcon.AppendBulkString(b, msg.message)
|
|
|
|
write(b)
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 21:23:23 +03:00
|
|
|
c.statsTotalMsgsSent.add(1)
|
2018-08-14 03:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
m := [2]map[string]bool{
|
2019-06-04 19:19:12 +03:00
|
|
|
make(map[string]bool), // pubsubChannel
|
|
|
|
make(map[string]bool), // pubsubPattern
|
2018-08-14 03:05:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
target := newSubtarget()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
for channel := range m[i] {
|
|
|
|
c.pubsub.unregister(i, channel, target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
target.cond.L.Lock()
|
|
|
|
target.closed = true
|
|
|
|
target.cond.Broadcast()
|
|
|
|
target.cond.L.Unlock()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
log.Debugf("pubsub open")
|
|
|
|
defer log.Debugf("pubsub closed")
|
|
|
|
for {
|
|
|
|
var msgs []submsg
|
|
|
|
target.cond.L.Lock()
|
|
|
|
if len(target.msgs) > 0 {
|
|
|
|
msgs = target.msgs
|
|
|
|
target.msgs = nil
|
|
|
|
}
|
|
|
|
target.cond.L.Unlock()
|
|
|
|
for _, msg := range msgs {
|
|
|
|
writeMessage(msg)
|
|
|
|
}
|
|
|
|
target.cond.L.Lock()
|
|
|
|
if target.closed {
|
|
|
|
target.cond.L.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
target.cond.Wait()
|
|
|
|
target.cond.L.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-10-29 01:49:45 +03:00
|
|
|
msgs := []*Message{msg}
|
2018-08-14 03:05:30 +03:00
|
|
|
for {
|
|
|
|
for _, msg := range msgs {
|
|
|
|
start = time.Now()
|
|
|
|
var kind int
|
2019-06-04 19:19:12 +03:00
|
|
|
var un bool
|
2018-10-29 01:49:45 +03:00
|
|
|
switch msg.Command() {
|
2018-08-14 03:05:30 +03:00
|
|
|
case "quit":
|
|
|
|
writeOK()
|
|
|
|
return nil
|
|
|
|
case "psubscribe":
|
2019-06-04 19:19:12 +03:00
|
|
|
kind, un = pubsubPattern, false
|
|
|
|
case "punsubscribe":
|
|
|
|
kind, un = pubsubPattern, true
|
2018-08-14 03:05:30 +03:00
|
|
|
case "subscribe":
|
2019-06-04 19:19:12 +03:00
|
|
|
kind, un = pubsubChannel, false
|
|
|
|
case "unsubscribe":
|
|
|
|
kind, un = pubsubChannel, true
|
2018-08-14 03:05:30 +03:00
|
|
|
default:
|
|
|
|
writeOnlyPubsubErr()
|
2019-06-04 19:19:12 +03:00
|
|
|
continue
|
2018-08-14 03:05:30 +03:00
|
|
|
}
|
2018-10-29 01:49:45 +03:00
|
|
|
if len(msg.Args) < 2 {
|
|
|
|
writeWrongNumberOfArgsErr(msg.Command())
|
2018-08-14 03:05:30 +03:00
|
|
|
}
|
2018-10-29 01:49:45 +03:00
|
|
|
for i := 1; i < len(msg.Args); i++ {
|
|
|
|
channel := msg.Args[i]
|
2019-06-04 19:19:12 +03:00
|
|
|
if un {
|
|
|
|
delete(m[kind], channel)
|
|
|
|
c.pubsub.unregister(kind, channel, target)
|
|
|
|
} else {
|
|
|
|
m[kind][channel] = true
|
|
|
|
c.pubsub.register(kind, channel, target)
|
|
|
|
}
|
2018-10-29 01:49:45 +03:00
|
|
|
writeSubscribe(msg.Command(), channel, len(m[0])+len(m[1]))
|
2018-08-14 03:05:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
msgs, err = rd.ReadMessages()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|