2013-12-08 04:19:20 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2013 The Perkeep Authors
|
2013-12-08 04:19:20 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package search
|
|
|
|
|
|
|
|
import (
|
2013-12-08 06:22:45 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2013-12-08 04:19:20 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2013-12-08 06:22:45 +00:00
|
|
|
"sync"
|
2013-12-08 04:19:20 +00:00
|
|
|
"time"
|
|
|
|
|
2016-04-20 23:43:47 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2013-12-08 04:19:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Time allowed to write a message to the peer.
|
|
|
|
writeWait = 10 * time.Second
|
|
|
|
|
|
|
|
// Time allowed to read the next pong message from the peer.
|
|
|
|
pongWait = 60 * time.Second
|
|
|
|
|
|
|
|
// Send pings to peer with this period. Must be less than pongWait.
|
|
|
|
pingPeriod = (pongWait * 9) / 10
|
|
|
|
|
|
|
|
// Maximum message size allowed from peer.
|
|
|
|
maxMessageSize = 10 << 10
|
|
|
|
)
|
|
|
|
|
|
|
|
type wsHub struct {
|
2013-12-08 06:22:45 +00:00
|
|
|
sh *Handler
|
|
|
|
register chan *wsConn
|
|
|
|
unregister chan *wsConn
|
|
|
|
watchReq chan watchReq
|
|
|
|
newBlobRecv chan string // new blob received. string is camliType.
|
|
|
|
updatedResults chan *watchedQuery
|
2014-08-29 21:11:58 +00:00
|
|
|
statusUpdate chan json.RawMessage
|
|
|
|
|
|
|
|
// Owned by func run:
|
|
|
|
conns map[*wsConn]bool
|
2013-12-08 04:19:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newWebsocketHub(sh *Handler) *wsHub {
|
|
|
|
return &wsHub{
|
2013-12-08 06:22:45 +00:00
|
|
|
sh: sh,
|
2015-01-19 17:29:19 +00:00
|
|
|
register: make(chan *wsConn), // unbuffered; issue 563
|
|
|
|
unregister: make(chan *wsConn), // unbuffered; issue 563
|
2013-12-08 06:22:45 +00:00
|
|
|
conns: make(map[*wsConn]bool),
|
|
|
|
watchReq: make(chan watchReq, buffered),
|
|
|
|
newBlobRecv: make(chan string, buffered),
|
|
|
|
updatedResults: make(chan *watchedQuery, buffered),
|
2014-08-29 21:11:58 +00:00
|
|
|
statusUpdate: make(chan json.RawMessage, buffered),
|
2013-12-08 04:19:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *wsHub) run() {
|
2014-08-29 21:11:58 +00:00
|
|
|
var lastStatusMsg []byte
|
2013-12-08 04:19:20 +00:00
|
|
|
for {
|
|
|
|
select {
|
2014-08-29 21:11:58 +00:00
|
|
|
case st := <-h.statusUpdate:
|
|
|
|
const prefix = `{"tag":"_status","status":`
|
|
|
|
lastStatusMsg = make([]byte, 0, len(prefix)+len(st)+1)
|
|
|
|
lastStatusMsg = append(lastStatusMsg, prefix...)
|
|
|
|
lastStatusMsg = append(lastStatusMsg, st...)
|
|
|
|
lastStatusMsg = append(lastStatusMsg, '}')
|
|
|
|
for c := range h.conns {
|
|
|
|
c.send <- lastStatusMsg
|
|
|
|
}
|
2013-12-08 04:19:20 +00:00
|
|
|
case c := <-h.register:
|
|
|
|
h.conns[c] = true
|
2014-08-29 21:11:58 +00:00
|
|
|
c.send <- lastStatusMsg
|
2013-12-08 04:19:20 +00:00
|
|
|
case c := <-h.unregister:
|
|
|
|
delete(h.conns, c)
|
|
|
|
close(c.send)
|
2013-12-08 06:22:45 +00:00
|
|
|
case camliType := <-h.newBlobRecv:
|
|
|
|
if camliType == "" {
|
|
|
|
// TODO: something smarter. some
|
|
|
|
// queries might care about all blobs.
|
|
|
|
// But for now only re-kick off
|
|
|
|
// queries if schema blobs arrive. We
|
|
|
|
// should track per-WatchdQuery which
|
|
|
|
// blob types the search cares about.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// New blob was received. Kick off standing search queries to see if any changed.
|
|
|
|
for conn := range h.conns {
|
|
|
|
for _, wq := range conn.queries {
|
2015-12-29 21:33:45 +00:00
|
|
|
go h.redoSearch(wq)
|
2013-12-08 06:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case wr := <-h.watchReq:
|
|
|
|
// Unsubscribe
|
|
|
|
if wr.q == nil {
|
|
|
|
delete(wr.conn.queries, wr.tag)
|
|
|
|
log.Printf("Removed subscription for %v, %q", wr.conn, wr.tag)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Very similar type, but semantically
|
|
|
|
// different, so separate for now:
|
|
|
|
wq := &watchedQuery{
|
|
|
|
conn: wr.conn,
|
|
|
|
tag: wr.tag,
|
|
|
|
q: wr.q,
|
|
|
|
}
|
|
|
|
wr.conn.queries[wr.tag] = wq
|
|
|
|
log.Printf("Added/updated search subscription for tag %q", wr.tag)
|
|
|
|
go h.doSearch(wq)
|
|
|
|
|
|
|
|
case wq := <-h.updatedResults:
|
|
|
|
if !h.conns[wq.conn] || wq.conn.queries[wq.tag] == nil {
|
|
|
|
// Client has since disconnected or unsubscribed.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wq.mu.Lock()
|
|
|
|
lastres := wq.lastres
|
|
|
|
wq.mu.Unlock()
|
|
|
|
resb, err := json.Marshal(wsUpdateMessage{
|
|
|
|
Tag: wq.tag,
|
|
|
|
Result: lastres,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
wq.conn.send <- resb
|
2013-12-08 04:19:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-29 21:33:45 +00:00
|
|
|
// redoSearch is called (in its own goroutine) when a new schema blob
|
|
|
|
// arrives to note that wq might now have new results and we should
|
|
|
|
// re-run it. But because a search can take awhile, don't run more
|
|
|
|
// than one refresh at a time.
|
|
|
|
func (h *wsHub) redoSearch(wq *watchedQuery) {
|
|
|
|
wq.mu.Lock()
|
|
|
|
defer wq.mu.Unlock()
|
|
|
|
wq.dirty = true
|
|
|
|
if wq.refreshing {
|
|
|
|
// Somebody else is already refreshing.
|
|
|
|
// One's enough.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for wq.dirty {
|
|
|
|
wq.refreshing = true
|
|
|
|
wq.dirty = false
|
|
|
|
wq.mu.Unlock() // release lock while running query; might become dirty meanwhile
|
|
|
|
h.doSearch(wq)
|
|
|
|
wq.mu.Lock() // before checking wq.dirty
|
|
|
|
}
|
|
|
|
wq.refreshing = false
|
|
|
|
}
|
|
|
|
|
2013-12-08 06:22:45 +00:00
|
|
|
func (h *wsHub) doSearch(wq *watchedQuery) {
|
|
|
|
// Make our own copy, in case
|
|
|
|
q := new(SearchQuery)
|
|
|
|
*q = *wq.q // shallow copy, since Query will mutate its internal state fields
|
|
|
|
if q.Describe != nil {
|
|
|
|
q.Describe = new(DescribeRequest)
|
|
|
|
*q.Describe = *wq.q.Describe
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := h.sh.Query(q)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Query error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resj, _ := json.Marshal(res)
|
|
|
|
|
|
|
|
wq.mu.Lock()
|
|
|
|
eq := bytes.Equal(wq.lastresj, resj)
|
|
|
|
wq.lastres = res
|
|
|
|
wq.lastresj = resj
|
|
|
|
wq.mu.Unlock()
|
|
|
|
if eq {
|
|
|
|
// No change in search. Ignore.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.updatedResults <- wq
|
|
|
|
}
|
|
|
|
|
|
|
|
type wsConn struct {
|
|
|
|
ws *websocket.Conn
|
|
|
|
send chan []byte // Buffered channel of outbound messages.
|
|
|
|
sh *Handler
|
|
|
|
|
2015-01-19 17:29:19 +00:00
|
|
|
// queries is owned by the wsHub.run goroutine.
|
2013-12-08 06:22:45 +00:00
|
|
|
queries map[string]*watchedQuery // tag -> subscription
|
|
|
|
}
|
|
|
|
|
|
|
|
type watchedQuery struct {
|
|
|
|
conn *wsConn
|
|
|
|
tag string
|
|
|
|
q *SearchQuery
|
|
|
|
|
2015-12-29 21:33:45 +00:00
|
|
|
mu sync.Mutex // guards following
|
|
|
|
refreshing bool // search is currently running
|
|
|
|
dirty bool // new schema blob arrived while refreshing; another refresh due
|
|
|
|
lastres *SearchResult
|
|
|
|
lastresj []byte // as JSON
|
2013-12-08 06:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// watchReq is a (un)subscribe request.
|
|
|
|
type watchReq struct {
|
|
|
|
conn *wsConn
|
|
|
|
tag string // required
|
|
|
|
q *SearchQuery // if nil, subscribe
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client->Server subscription message.
|
|
|
|
type wsClientMessage struct {
|
|
|
|
// Tag is required.
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
// Query is required to subscribe. If absent, it means unsubscribe.
|
|
|
|
Query *SearchQuery `json:"query,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type wsUpdateMessage struct {
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
Result *SearchResult `json:"result,omitempty"`
|
|
|
|
}
|
|
|
|
|
2013-12-08 04:19:20 +00:00
|
|
|
// readPump pumps messages from the websocket connection to the hub.
|
|
|
|
func (c *wsConn) readPump() {
|
|
|
|
defer func() {
|
|
|
|
c.sh.wsHub.unregister <- c
|
|
|
|
c.ws.Close()
|
|
|
|
}()
|
|
|
|
c.ws.SetReadLimit(maxMessageSize)
|
|
|
|
c.ws.SetReadDeadline(time.Now().Add(pongWait))
|
|
|
|
c.ws.SetPongHandler(func(string) error { c.ws.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
|
|
|
for {
|
|
|
|
_, message, err := c.ws.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2014-08-29 21:11:58 +00:00
|
|
|
log.Printf("Got websocket message %#q", message)
|
2013-12-08 06:22:45 +00:00
|
|
|
cm := new(wsClientMessage)
|
|
|
|
if err := json.Unmarshal(message, cm); err != nil {
|
|
|
|
log.Printf("Ignoring bogus websocket message. Err: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.sh.wsHub.watchReq <- watchReq{
|
|
|
|
conn: c,
|
|
|
|
tag: cm.Tag,
|
|
|
|
q: cm.Query,
|
|
|
|
}
|
2013-12-08 04:19:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// write writes a message with the given message type and payload.
|
|
|
|
func (c *wsConn) write(mt int, payload []byte) error {
|
|
|
|
c.ws.SetWriteDeadline(time.Now().Add(writeWait))
|
|
|
|
return c.ws.WriteMessage(mt, payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
// writePump pumps messages from the hub to the websocket connection.
|
|
|
|
func (c *wsConn) writePump() {
|
|
|
|
ticker := time.NewTicker(pingPeriod)
|
|
|
|
defer func() {
|
|
|
|
ticker.Stop()
|
|
|
|
c.ws.Close()
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case message, ok := <-c.send:
|
|
|
|
if !ok {
|
|
|
|
c.write(websocket.CloseMessage, []byte{})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := c.write(websocket.TextMessage, message); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-ticker.C:
|
|
|
|
if err := c.write(websocket.PingMessage, []byte{}); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *Handler) serveWebSocket(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
ws, err := websocket.Upgrade(rw, req, nil, 1024, 1024)
|
|
|
|
if _, ok := err.(websocket.HandshakeError); ok {
|
|
|
|
http.Error(rw, "Not a websocket handshake", 400)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c := &wsConn{
|
2013-12-08 06:22:45 +00:00
|
|
|
ws: ws,
|
|
|
|
send: make(chan []byte, 256),
|
|
|
|
sh: sh,
|
|
|
|
queries: make(map[string]*watchedQuery),
|
2013-12-08 04:19:20 +00:00
|
|
|
}
|
|
|
|
sh.wsHub.register <- c
|
|
|
|
go c.writePump()
|
|
|
|
c.readPump()
|
|
|
|
}
|