Clean up API code, use update and new message types for sending subscription events

This commit is contained in:
Daniel 2019-09-20 10:35:13 +02:00
parent d16cb1ebf2
commit e0535421e4
3 changed files with 17 additions and 18 deletions

View file

@ -101,6 +101,7 @@ func authMiddleware(next http.Handler) http.Handler {
Name: cookieName, Name: cookieName,
Value: tokenString, Value: tokenString,
HttpOnly: true, HttpOnly: true,
MaxAge: int(cookieTTL.Seconds()),
}) })
// serve // serve

View file

@ -7,7 +7,6 @@ import (
"github.com/safing/portbase/log" "github.com/safing/portbase/log"
"github.com/gorilla/websocket"
"github.com/tevino/abool" "github.com/tevino/abool"
) )
@ -36,7 +35,6 @@ type Client struct {
operations map[string]*Operation operations map[string]*Operation
nextOpID uint64 nextOpID uint64
wsConn *websocket.Conn
lastError string lastError string
} }
@ -73,12 +71,12 @@ func (c *Client) Connect() error {
func (c *Client) StayConnected() { func (c *Client) StayConnected() {
log.Infof("client: connecting to Portmaster at %s", c.server) log.Infof("client: connecting to Portmaster at %s", c.server)
c.Connect() _ = c.Connect()
for { for {
select { select {
case <-time.After(backOffTimer): case <-time.After(backOffTimer):
log.Infof("client: reconnecting...") log.Infof("client: reconnecting...")
c.Connect() _ = c.Connect()
case <-c.shutdownSignal: case <-c.shutdownSignal:
return return
} }

View file

@ -191,7 +191,7 @@ func (api *DatabaseAPI) writer() {
select { select {
// prioritize direct writes // prioritize direct writes
case data = <-api.sendQueue: case data = <-api.sendQueue:
if data == nil || len(data) == 0 { if len(data) == 0 {
api.shutdown() api.shutdown()
return return
} }
@ -242,8 +242,9 @@ func (api *DatabaseAPI) handleGet(opID []byte, key string) {
r, err := api.db.Get(key) r, err := api.db.Get(key)
if err == nil { if err == nil {
data, err = r.Marshal(r, record.JSON) data, err = r.Marshal(r, record.JSON)
} else { }
api.send(opID, dbMsgTypeError, err.Error(), nil) if err == nil {
api.send(opID, dbMsgTypeError, err.Error(), nil) //nolint:nilness // FIXME: possibly false positive (golangci-lint govet/nilness)
return return
} }
api.send(opID, dbMsgTypeOk, r.Key(), data) api.send(opID, dbMsgTypeOk, r.Key(), data)
@ -357,7 +358,7 @@ func (api *DatabaseAPI) processSub(opID []byte, sub *database.Subscription) {
select { select {
case <-api.shutdownSignal: case <-api.shutdownSignal:
// cancel sub and return // cancel sub and return
sub.Cancel() _ = sub.Cancel()
return return
case r := <-sub.Feed: case r := <-sub.Feed:
// process sub feed // process sub feed
@ -373,17 +374,19 @@ func (api *DatabaseAPI) processSub(opID []byte, sub *database.Subscription) {
// TODO: use upd, new and delete msgTypes // TODO: use upd, new and delete msgTypes
r.Lock() r.Lock()
isDeleted := r.Meta().IsDeleted() isDeleted := r.Meta().IsDeleted()
new := r.Meta().Created == r.Meta().Modified
r.Unlock() r.Unlock()
if isDeleted { switch {
case isDeleted:
api.send(opID, dbMsgTypeDel, r.Key(), nil) api.send(opID, dbMsgTypeDel, r.Key(), nil)
} else { case new:
api.send(opID, dbMsgTypeNew, r.Key(), data)
default:
api.send(opID, dbMsgTypeUpd, r.Key(), data) api.send(opID, dbMsgTypeUpd, r.Key(), data)
} }
} else { } else if sub.Err != nil {
// sub feed ended // sub feed ended
if sub.Err != nil { api.send(opID, dbMsgTypeError, sub.Err.Error(), nil)
api.send(opID, dbMsgTypeError, sub.Err.Error(), nil)
}
} }
} }
} }
@ -489,10 +492,7 @@ func (api *DatabaseAPI) handleInsert(opID []byte, key string, data []byte) {
return false return false
} }
insertError = acc.Set(key.String(), value.Value()) insertError = acc.Set(key.String(), value.Value())
if insertError != nil { return insertError == nil
return false
}
return true
}) })
if insertError != nil { if insertError != nil {