"encoding/json"
"errors"
"sync"
+ "sync/atomic"
"time"
"git.curoverse.com/arvados.git/sdk/go/arvados"
- log "github.com/Sirupsen/logrus"
+ "github.com/Sirupsen/logrus"
)
var (
errQueueFull = errors.New("client queue full")
errFrameTooBig = errors.New("frame too big")
- sendObjectAttributes = []string{"state", "name"}
+ sendObjectAttributes = []string{"state", "name", "owner_uuid", "portable_data_hash"}
v0subscribeOK = []byte(`{"status":200}`)
v0subscribeFail = []byte(`{"status":400}`)
)
type v0session struct {
+ ac *arvados.Client
ws wsConn
+ sendq chan<- interface{}
db *sql.DB
permChecker permChecker
subscriptions []v0subscribe
- log *log.Entry
+ lastMsgID uint64
+ log *logrus.Entry
mtx sync.Mutex
setupOnce sync.Once
}
-func NewSessionV0(ws wsConn, ac arvados.Client, db *sql.DB) (session, error) {
+// newSessionV0 returns a v0 session: a partial port of the Rails/puma
+// implementation, with just enough functionality to support Workbench
+// and arv-mount.
+func newSessionV0(ws wsConn, sendq chan<- interface{}, db *sql.DB, pc permChecker, ac *arvados.Client) (session, error) {
sess := &v0session{
+ sendq: sendq,
ws: ws,
db: db,
- permChecker: NewPermChecker(ac),
+ ac: ac,
+ permChecker: pc,
log: logger(ws.Request().Context()),
}
return sess, nil
}
-func (sess *v0session) Receive(msg map[string]interface{}, buf []byte) [][]byte {
- sess.log.WithField("data", msg).Debug("received message")
+func (sess *v0session) Receive(buf []byte) error {
var sub v0subscribe
if err := json.Unmarshal(buf, &sub); err != nil {
- sess.log.WithError(err).Info("ignored invalid request")
- return nil
- }
- if sub.Method == "subscribe" {
+ sess.log.WithError(err).Info("invalid message from client")
+ } else if sub.Method == "subscribe" {
sub.prepare(sess)
sess.log.WithField("sub", sub).Debug("sub prepared")
+ sess.sendq <- v0subscribeOK
sess.mtx.Lock()
sess.subscriptions = append(sess.subscriptions, sub)
sess.mtx.Unlock()
-
- return append([][]byte{v0subscribeOK}, sub.getOldEvents(sess)...)
+ sub.sendOldEvents(sess)
+ return nil
+ } else {
+ sess.log.WithField("Method", sub.Method).Info("unknown method")
}
- return [][]byte{v0subscribeFail}
+ sess.sendq <- v0subscribeFail
+ return nil
}
func (sess *v0session) EventMessage(e *event) ([]byte, error) {
return nil, err
}
+ kind, _ := sess.ac.KindForUUID(detail.ObjectUUID)
msg := map[string]interface{}{
- "msgID": e.Serial,
+ "msgID": atomic.AddUint64(&sess.lastMsgID, 1),
"id": detail.ID,
"uuid": detail.UUID,
"object_uuid": detail.ObjectUUID,
"object_owner_uuid": detail.ObjectOwnerUUID,
+ "object_kind": kind,
"event_type": detail.EventType,
+ "event_at": detail.EventAt,
}
if detail.Properties != nil && detail.Properties["text"] != nil {
msg["properties"] = detail.Properties
return false
}
-func (sub *v0subscribe) getOldEvents(sess *v0session) (msgs [][]byte) {
+func (sub *v0subscribe) sendOldEvents(sess *v0session) {
if sub.LastLogID == 0 {
return
}
sess.log.WithError(err).Error("row Scan failed")
continue
}
+ for len(sess.sendq)*2 > cap(sess.sendq) {
+ // Ugly... but if we fill up the whole client
+ // queue with a backlog of old events, a
+ // single new event will overflow it and
+ // terminate the connection, and then the
+ // client will probably reconnect and do the
+ // same thing all over again.
+ time.Sleep(100 * time.Millisecond)
+ }
+ now := time.Now()
e := &event{
LogID: id,
- Received: time.Now(),
+ Received: now,
+ Ready: now,
db: sess.db,
}
- if !sub.match(sess, e) {
- sess.log.WithField("event", e).Debug("skip old event")
- continue
- }
- msg, err := sess.EventMessage(e)
- if err != nil {
- sess.log.WithError(err).Error("event marshal failed")
- continue
+ if sub.match(sess, e) {
+ select {
+ case sess.sendq <- e:
+ case <-sess.ws.Request().Context().Done():
+ return
+ }
}
- sess.log.WithField("data", msg).Debug("will queue old event")
- msgs = append(msgs, msg)
}
if err := rows.Err(); err != nil {
sess.log.WithError(err).Error("db.Query failed")
}
- return
}
type v0subscribe struct {