X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/8595030d0314e6f88a245e66f90cce0a306b6867..443a0b96316ed46600dc5035193adae6ac4d1f74:/services/ws/router.go diff --git a/services/ws/router.go b/services/ws/router.go index 3f9800f04c..a408b58bdd 100644 --- a/services/ws/router.go +++ b/services/ws/router.go @@ -1,7 +1,10 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + package main import ( - "database/sql" "encoding/json" "io" "net/http" @@ -10,7 +13,9 @@ import ( "sync/atomic" "time" - "github.com/Sirupsen/logrus" + "git.curoverse.com/arvados.git/sdk/go/ctxlog" + "git.curoverse.com/arvados.git/sdk/go/health" + "github.com/sirupsen/logrus" "golang.org/x/net/websocket" ) @@ -22,7 +27,7 @@ type wsConn interface { } type router struct { - Config *Config + Config *wsConfig eventSource eventSource newPermChecker func() permChecker @@ -41,22 +46,33 @@ type routerDebugStatus struct { ReqsActive int64 } -type DebugStatuser interface { +type debugStatuser interface { DebugStatus() interface{} } -type sessionFactory func(wsConn, chan<- interface{}, *sql.DB, permChecker) (session, error) - func (rtr *router) setup() { rtr.handler = &handler{ PingTimeout: rtr.Config.PingTimeout.Duration(), QueueSize: rtr.Config.ClientEventQueue, } rtr.mux = http.NewServeMux() - rtr.mux.Handle("/websocket", rtr.makeServer(NewSessionV0)) - rtr.mux.Handle("/arvados/v1/events.ws", rtr.makeServer(NewSessionV1)) - rtr.mux.HandleFunc("/debug.json", jsonHandler(rtr.DebugStatus)) - rtr.mux.HandleFunc("/status.json", jsonHandler(rtr.Status)) + rtr.mux.Handle("/websocket", rtr.makeServer(newSessionV0)) + rtr.mux.Handle("/arvados/v1/events.ws", rtr.makeServer(newSessionV1)) + rtr.mux.Handle("/debug.json", rtr.jsonHandler(rtr.DebugStatus)) + rtr.mux.Handle("/status.json", rtr.jsonHandler(rtr.Status)) + + rtr.mux.Handle("/_health/", &health.Handler{ + Token: rtr.Config.ManagementToken, + Prefix: "/_health/", + Routes: health.Routes{ + "db": rtr.eventSource.DBHealth, + }, + Log: func(r *http.Request, err error) { + if err != nil { + logger(r.Context()).WithError(err).Error("error") + } + }, + }) } func (rtr *router) makeServer(newSession sessionFactory) *websocket.Server { @@ -71,12 +87,12 @@ func (rtr *router) makeServer(newSession sessionFactory) *websocket.Server { stats := rtr.handler.Handle(ws, rtr.eventSource, func(ws wsConn, sendq chan<- interface{}) (session, error) { - return newSession(ws, sendq, rtr.eventSource.DB(), rtr.newPermChecker()) + return newSession(ws, sendq, rtr.eventSource.DB(), rtr.newPermChecker(), &rtr.Config.Client) }) log.WithFields(logrus.Fields{ - "Elapsed": time.Now().Sub(t0).Seconds(), - "Stats": stats, + "elapsed": time.Now().Sub(t0).Seconds(), + "stats": stats, }).Info("disconnect") ws.Close() }), @@ -98,7 +114,7 @@ func (rtr *router) DebugStatus() interface{} { "HTTP": rtr.status, "Outgoing": rtr.handler.DebugStatus(), } - if es, ok := rtr.eventSource.(DebugStatuser); ok { + if es, ok := rtr.eventSource.(debugStatuser); ok { s["EventSource"] = es.DebugStatus() } return s @@ -107,6 +123,7 @@ func (rtr *router) DebugStatus() interface{} { func (rtr *router) Status() interface{} { return map[string]interface{}{ "Clients": atomic.LoadInt64(&rtr.status.ReqsActive), + "Version": version, } } @@ -118,24 +135,25 @@ func (rtr *router) ServeHTTP(resp http.ResponseWriter, req *http.Request) { logger := logger(req.Context()). WithField("RequestID", rtr.newReqID()) - ctx := contextWithLogger(req.Context(), logger) + ctx := ctxlog.Context(req.Context(), logger) req = req.WithContext(ctx) logger.WithFields(logrus.Fields{ - "RemoteAddr": req.RemoteAddr, - "X-Forwarded-For": req.Header.Get("X-Forwarded-For"), + "remoteAddr": req.RemoteAddr, + "reqForwardedFor": req.Header.Get("X-Forwarded-For"), }).Info("accept request") rtr.mux.ServeHTTP(resp, req) } -func jsonHandler(fn func() interface{}) http.HandlerFunc { - return func(resp http.ResponseWriter, req *http.Request) { - logger := logger(req.Context()) - enc := json.NewEncoder(resp) +func (rtr *router) jsonHandler(fn func() interface{}) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + logger := logger(r.Context()) + w.Header().Set("Content-Type", "application/json") + enc := json.NewEncoder(w) err := enc.Encode(fn()) if err != nil { msg := "encode failed" logger.WithError(err).Error(msg) - http.Error(resp, msg, http.StatusInternalServerError) + http.Error(w, msg, http.StatusInternalServerError) } - } + }) }