tile38/internal/controller/live.go

199 lines
4.0 KiB
Go
Raw Normal View History

2016-03-05 02:08:16 +03:00
package controller
import (
"bytes"
"errors"
2016-04-01 22:46:39 +03:00
"fmt"
2016-03-05 02:08:16 +03:00
"io"
"net"
"sync"
"github.com/tidwall/tile38/internal/log"
"github.com/tidwall/tile38/internal/server"
2016-03-05 02:08:16 +03:00
)
type liveBuffer struct {
key string
glob string
fence *liveFenceSwitches
details []*commandDetailsT
cond *sync.Cond
}
func (c *Controller) processLives() {
for {
c.lcond.L.Lock()
for len(c.lstack) > 0 {
item := c.lstack[0]
c.lstack = c.lstack[1:]
if len(c.lstack) == 0 {
c.lstack = nil
}
for lb := range c.lives {
lb.cond.L.Lock()
if lb.key != "" && lb.key == item.key {
lb.details = append(lb.details, item)
lb.cond.Broadcast()
}
lb.cond.L.Unlock()
}
}
c.lcond.Wait()
c.lcond.L.Unlock()
}
}
2018-08-14 03:05:30 +03:00
func writeLiveMessage(
conn net.Conn,
message []byte,
wrapRESP bool,
connType server.Type,
websocket bool,
) error {
2016-04-01 22:46:39 +03:00
if len(message) == 0 {
return nil
}
2016-03-05 02:08:16 +03:00
if websocket {
2016-04-01 22:46:39 +03:00
return server.WriteWebSocketMessage(conn, message)
}
var err error
switch connType {
case server.RESP:
if wrapRESP {
_, err = fmt.Fprintf(conn, "$%d\r\n%s\r\n", len(message), string(message))
} else {
_, err = conn.Write(message)
}
case server.Native:
2016-04-05 17:15:13 +03:00
_, err = fmt.Fprintf(conn, "$%d %s\r\n", len(message), string(message))
2016-03-05 02:08:16 +03:00
}
2016-04-01 22:46:39 +03:00
return err
2016-03-05 02:08:16 +03:00
}
func (c *Controller) goLive(inerr error, conn net.Conn, rd *server.PipelineReader, msg *server.Message, websocket bool) error {
2016-03-05 02:08:16 +03:00
addr := conn.RemoteAddr().String()
log.Info("live " + addr)
defer func() {
log.Info("not live " + addr)
}()
2018-08-14 03:05:30 +03:00
switch s := inerr.(type) {
default:
return errors.New("invalid live type switches")
case liveAOFSwitches:
2016-04-01 02:26:36 +03:00
return c.liveAOF(s.pos, conn, rd, msg)
2018-08-14 03:05:30 +03:00
case liveSubscriptionSwitches:
return c.liveSubscription(conn, rd, msg, websocket)
case liveFenceSwitches:
// fallthrough
2016-03-05 02:08:16 +03:00
}
2018-08-14 03:05:30 +03:00
// everything below is for live geofences
2016-03-05 02:08:16 +03:00
lb := &liveBuffer{
cond: sync.NewCond(&sync.Mutex{}),
}
var err error
var sw *scanWriter
var wr bytes.Buffer
2018-08-14 03:05:30 +03:00
s := inerr.(liveFenceSwitches)
lb.glob = s.glob
lb.key = s.key
lb.fence = &s
c.mu.RLock()
sw, err = c.newScanWriter(
&wr, msg, s.key, s.output, s.precision, s.glob, false,
s.cursor, s.limit, s.wheres, s.whereins, s.whereevals, s.nofields)
c.mu.RUnlock()
2016-03-05 02:08:16 +03:00
// everything below if for live SCAN, NEARBY, WITHIN, INTERSECTS
if err != nil {
return err
}
c.lcond.L.Lock()
c.lives[lb] = true
c.lcond.L.Unlock()
defer func() {
c.lcond.L.Lock()
delete(c.lives, lb)
c.lcond.L.Unlock()
conn.Close()
}()
var mustQuit bool
go func() {
defer func() {
lb.cond.L.Lock()
mustQuit = true
lb.cond.Broadcast()
lb.cond.L.Unlock()
conn.Close()
}()
for {
vs, err := rd.ReadMessages()
2016-03-05 02:08:16 +03:00
if err != nil {
2017-01-13 00:22:14 +03:00
if err != io.EOF && !(websocket && err == io.ErrUnexpectedEOF) {
2016-03-05 02:08:16 +03:00
log.Error(err)
}
return
}
for _, v := range vs {
if v == nil {
continue
}
switch v.Command {
default:
log.Error("received a live command that was not QUIT")
return
case "quit", "":
return
}
2016-03-05 02:08:16 +03:00
}
}
}()
2016-04-01 22:46:39 +03:00
outputType := msg.OutputType
connType := msg.ConnType
if websocket {
outputType = server.JSON
}
var livemsg []byte
switch outputType {
case server.JSON:
2016-04-03 00:13:20 +03:00
livemsg = []byte(`{"ok":true,"live":true}`)
2016-04-01 22:46:39 +03:00
case server.RESP:
livemsg = []byte("+OK\r\n")
}
2018-08-14 03:05:30 +03:00
if err := writeLiveMessage(conn, livemsg, false, connType, websocket); err != nil {
2016-03-05 02:08:16 +03:00
return nil // nil return is fine here
}
for {
lb.cond.L.Lock()
if mustQuit {
lb.cond.L.Unlock()
return nil
}
for len(lb.details) > 0 {
details := lb.details[0]
lb.details = lb.details[1:]
if len(lb.details) == 0 {
lb.details = nil
}
fence := lb.fence
lb.cond.L.Unlock()
2018-08-14 06:27:22 +03:00
var msgs []string
func() {
// safely lock the fence because we are outside the main loop
c.mu.RLock()
defer c.mu.RUnlock()
msgs = FenceMatch("", sw, fence, nil, details)
}()
2016-03-19 17:16:19 +03:00
for _, msg := range msgs {
2018-08-14 03:05:30 +03:00
if err := writeLiveMessage(conn, []byte(msg), true, connType, websocket); err != nil {
2016-03-19 17:16:19 +03:00
return nil // nil return is fine here
2016-03-05 02:08:16 +03:00
}
}
lb.cond.L.Lock()
}
lb.cond.Wait()
lb.cond.L.Unlock()
}
}