X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/8a5ef4b1c8086fed020cb5d45552f26ec85f74ab..7563e6276baade41eb3faa3ff167abab1cb0f890:/services/ws/router.go diff --git a/services/ws/router.go b/services/ws/router.go index 5f40143fcd..878c282f8a 100644 --- a/services/ws/router.go +++ b/services/ws/router.go @@ -5,15 +5,12 @@ package ws import ( - "encoding/json" "io" "net/http" - "strconv" "sync" "sync/atomic" "time" - "git.arvados.org/arvados.git/lib/cmd" "git.arvados.org/arvados.git/sdk/go/arvados" "git.arvados.org/arvados.git/sdk/go/ctxlog" "git.arvados.org/arvados.git/sdk/go/health" @@ -40,20 +37,6 @@ type router struct { setupOnce sync.Once done chan struct{} reg *prometheus.Registry - - lastReqID int64 - lastReqMtx sync.Mutex - - status routerDebugStatus -} - -type routerDebugStatus struct { - ReqsReceived int64 - ReqsActive int64 -} - -type debugStatuser interface { - DebugStatus() interface{} } func (rtr *router) setup() { @@ -70,9 +53,6 @@ func (rtr *router) setup() { QueueSize: rtr.cluster.API.WebsocketClientEventQueue, } rtr.mux = http.NewServeMux() - rtr.mux.Handle("/debug.json", rtr.jsonHandler(rtr.DebugStatus)) - rtr.mux.Handle("/status.json", rtr.jsonHandler(rtr.Status)) - rtr.mux.Handle("/websocket", rtr.makeServer(newSessionV0, mSockets.WithLabelValues("0"))) rtr.mux.Handle("/arvados/v1/events.ws", rtr.makeServer(newSessionV1, mSockets.WithLabelValues("1"))) rtr.mux.Handle("/_health/", &health.Handler{ @@ -98,7 +78,6 @@ func (rtr *router) makeServer(newSession sessionFactory, gauge prometheus.Gauge) Handler: websocket.Handler(func(ws *websocket.Conn) { t0 := time.Now() logger := ctxlog.FromContext(ws.Request().Context()) - logger.Info("connected") atomic.AddInt64(&connected, 1) gauge.Set(float64(atomic.LoadInt64(&connected))) @@ -110,7 +89,7 @@ func (rtr *router) makeServer(newSession sessionFactory, gauge prometheus.Gauge) logger.WithFields(logrus.Fields{ "elapsed": time.Now().Sub(t0).Seconds(), "stats": stats, - }).Info("disconnect") + }).Info("client disconnected") ws.Close() atomic.AddInt64(&connected, -1) gauge.Set(float64(atomic.LoadInt64(&connected))) @@ -118,65 +97,11 @@ func (rtr *router) makeServer(newSession sessionFactory, gauge prometheus.Gauge) } } -func (rtr *router) newReqID() string { - rtr.lastReqMtx.Lock() - defer rtr.lastReqMtx.Unlock() - id := time.Now().UnixNano() - if id <= rtr.lastReqID { - id = rtr.lastReqID + 1 - } - return strconv.FormatInt(id, 36) -} - -func (rtr *router) DebugStatus() interface{} { - s := map[string]interface{}{ - "HTTP": rtr.status, - "Outgoing": rtr.handler.DebugStatus(), - } - if es, ok := rtr.eventSource.(debugStatuser); ok { - s["EventSource"] = es.DebugStatus() - } - return s -} - -func (rtr *router) Status() interface{} { - return map[string]interface{}{ - "Clients": atomic.LoadInt64(&rtr.status.ReqsActive), - "Version": cmd.Version.String(), - } -} - func (rtr *router) ServeHTTP(resp http.ResponseWriter, req *http.Request) { rtr.setupOnce.Do(rtr.setup) - atomic.AddInt64(&rtr.status.ReqsReceived, 1) - atomic.AddInt64(&rtr.status.ReqsActive, 1) - defer atomic.AddInt64(&rtr.status.ReqsActive, -1) - - logger := ctxlog.FromContext(req.Context()). - WithField("RequestID", rtr.newReqID()) - ctx := ctxlog.Context(req.Context(), logger) - req = req.WithContext(ctx) - logger.WithFields(logrus.Fields{ - "remoteAddr": req.RemoteAddr, - "reqForwardedFor": req.Header.Get("X-Forwarded-For"), - }).Info("accept request") rtr.mux.ServeHTTP(resp, req) } -func (rtr *router) jsonHandler(fn func() interface{}) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - logger := ctxlog.FromContext(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(w, msg, http.StatusInternalServerError) - } - }) -} - func (rtr *router) CheckHealth() error { rtr.setupOnce.Do(rtr.setup) return rtr.eventSource.DBHealth()