Improve live query performance and simplicity

This commit is contained in:
Tobie Morgan Hitchcock 2018-04-27 00:04:36 +01:00
parent 5d5fdc296f
commit 9b03178dfd
3 changed files with 75 additions and 71 deletions

View file

@ -28,7 +28,7 @@ var sockets sync.Map
func register(fib *fibre.Context, id string) func() { func register(fib *fibre.Context, id string) func() {
return func() { return func() {
sockets.LoadOrStore(id, &socket{ sockets.Store(id, &socket{
fibre: fib, fibre: fib,
items: make(map[string][]interface{}), items: make(map[string][]interface{}),
lives: make(map[string]*sql.LiveStatement), lives: make(map[string]*sql.LiveStatement),

View file

@ -47,10 +47,24 @@ func (d *document) lives(ctx context.Context, when method) (err error) {
return err return err
} }
if len(lvs) > 0 { // Loop over the currently running
// live queries so that we can pass
// change notifications to the socket.
for _, lv := range lvs { for _, lv := range lvs {
// Check whether the change was made by
// the same connection as the live query,
// and if it is then don't notify changes.
if id == lv.FB {
continue
}
// Load the socket which owns the live
// query so that we can check the socket
// permissions, and send the notifications.
if sck, ok := sockets.Load(lv.FB); ok { if sck, ok := sockets.Load(lv.FB); ok {
var out interface{} var out interface{}
@ -61,14 +75,6 @@ func (d *document) lives(ctx context.Context, when method) (err error) {
ctx = sck.(*socket).ctx(d.ns, d.db) ctx = sck.(*socket).ctx(d.ns, d.db)
// Check whether the change was made by
// the same connection as the live query,
// and if it is then don't notify changes.
if id == lv.FB {
continue
}
// Check whether this live query has the // Check whether this live query has the
// necessary permissions to view this // necessary permissions to view this
// document, or continue to the next query. // document, or continue to the next query.
@ -128,8 +134,6 @@ func (d *document) lives(ctx context.Context, when method) (err error) {
} }
}
return return
} }

View file

@ -97,7 +97,7 @@ func (s *socket) clear(id string) (err error) {
s.mutex.Lock() s.mutex.Lock()
defer s.mutex.Unlock() defer s.mutex.Unlock()
s.items[id] = nil delete(s.items, id)
return return
@ -135,7 +135,7 @@ func (s *socket) flush(id string) (err error) {
// pending message notifications // pending message notifications
// for this socket when done. // for this socket when done.
s.items[id] = nil delete(s.items, id)
return return