13 "github.com/Sirupsen/logrus"
14 "golang.org/x/net/websocket"
17 type wsConn interface {
19 Request() *http.Request
20 SetReadDeadline(time.Time) error
21 SetWriteDeadline(time.Time) error
26 eventSource eventSource
27 newPermChecker func() permChecker
36 status routerDebugStatus
39 type routerDebugStatus struct {
44 type DebugStatuser interface {
45 DebugStatus() interface{}
48 type sessionFactory func(wsConn, chan<- interface{}, *sql.DB, permChecker) (session, error)
50 func (rtr *router) setup() {
51 rtr.handler = &handler{
52 PingTimeout: rtr.Config.PingTimeout.Duration(),
53 QueueSize: rtr.Config.ClientEventQueue,
55 rtr.mux = http.NewServeMux()
56 rtr.mux.Handle("/websocket", rtr.makeServer(NewSessionV0))
57 rtr.mux.Handle("/arvados/v1/events.ws", rtr.makeServer(NewSessionV1))
58 rtr.mux.HandleFunc("/debug.json", jsonHandler(rtr.DebugStatus))
59 rtr.mux.HandleFunc("/status.json", jsonHandler(rtr.Status))
62 func (rtr *router) makeServer(newSession sessionFactory) *websocket.Server {
63 return &websocket.Server{
64 Handshake: func(c *websocket.Config, r *http.Request) error {
67 Handler: websocket.Handler(func(ws *websocket.Conn) {
69 log := logger(ws.Request().Context())
72 stats := rtr.handler.Handle(ws, rtr.eventSource,
73 func(ws wsConn, sendq chan<- interface{}) (session, error) {
74 return newSession(ws, sendq, rtr.eventSource.DB(), rtr.newPermChecker())
77 log.WithFields(logrus.Fields{
78 "Elapsed": time.Now().Sub(t0).Seconds(),
86 func (rtr *router) newReqID() string {
88 defer rtr.lastReqMtx.Unlock()
89 id := time.Now().UnixNano()
90 if id <= rtr.lastReqID {
91 id = rtr.lastReqID + 1
93 return strconv.FormatInt(id, 36)
96 func (rtr *router) DebugStatus() interface{} {
97 s := map[string]interface{}{
99 "Outgoing": rtr.handler.DebugStatus(),
101 if es, ok := rtr.eventSource.(DebugStatuser); ok {
102 s["EventSource"] = es.DebugStatus()
107 func (rtr *router) Status() interface{} {
108 return map[string]interface{}{
109 "Clients": atomic.LoadInt64(&rtr.status.ReqsActive),
113 func (rtr *router) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
114 rtr.setupOnce.Do(rtr.setup)
115 atomic.AddInt64(&rtr.status.ReqsReceived, 1)
116 atomic.AddInt64(&rtr.status.ReqsActive, 1)
117 defer atomic.AddInt64(&rtr.status.ReqsActive, -1)
119 logger := logger(req.Context()).
120 WithField("RequestID", rtr.newReqID())
121 ctx := contextWithLogger(req.Context(), logger)
122 req = req.WithContext(ctx)
123 logger.WithFields(logrus.Fields{
124 "RemoteAddr": req.RemoteAddr,
125 "X-Forwarded-For": req.Header.Get("X-Forwarded-For"),
126 }).Info("accept request")
127 rtr.mux.ServeHTTP(resp, req)
130 func jsonHandler(fn func() interface{}) http.HandlerFunc {
131 return func(resp http.ResponseWriter, req *http.Request) {
132 logger := logger(req.Context())
133 enc := json.NewEncoder(resp)
134 err := enc.Encode(fn())
136 msg := "encode failed"
137 logger.WithError(err).Error(msg)
138 http.Error(resp, msg, http.StatusInternalServerError)