16217: Exit service command if the service handler fails.
[arvados.git] / services / ws / router.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package ws
6
7 import (
8         "encoding/json"
9         "io"
10         "net/http"
11         "strconv"
12         "sync"
13         "sync/atomic"
14         "time"
15
16         "git.arvados.org/arvados.git/lib/cmd"
17         "git.arvados.org/arvados.git/sdk/go/arvados"
18         "git.arvados.org/arvados.git/sdk/go/ctxlog"
19         "git.arvados.org/arvados.git/sdk/go/health"
20         "github.com/sirupsen/logrus"
21         "golang.org/x/net/websocket"
22 )
23
24 type wsConn interface {
25         io.ReadWriter
26         Request() *http.Request
27         SetReadDeadline(time.Time) error
28         SetWriteDeadline(time.Time) error
29 }
30
31 type router struct {
32         client         *arvados.Client
33         cluster        *arvados.Cluster
34         eventSource    eventSource
35         newPermChecker func() permChecker
36
37         handler   *handler
38         mux       *http.ServeMux
39         setupOnce sync.Once
40         done      chan struct{}
41
42         lastReqID  int64
43         lastReqMtx sync.Mutex
44
45         status routerDebugStatus
46 }
47
48 type routerDebugStatus struct {
49         ReqsReceived int64
50         ReqsActive   int64
51 }
52
53 type debugStatuser interface {
54         DebugStatus() interface{}
55 }
56
57 func (rtr *router) setup() {
58         rtr.handler = &handler{
59                 PingTimeout: time.Duration(rtr.cluster.API.SendTimeout),
60                 QueueSize:   rtr.cluster.API.WebsocketClientEventQueue,
61         }
62         rtr.mux = http.NewServeMux()
63         rtr.mux.Handle("/websocket", rtr.makeServer(newSessionV0))
64         rtr.mux.Handle("/arvados/v1/events.ws", rtr.makeServer(newSessionV1))
65         rtr.mux.Handle("/debug.json", rtr.jsonHandler(rtr.DebugStatus))
66         rtr.mux.Handle("/status.json", rtr.jsonHandler(rtr.Status))
67
68         rtr.mux.Handle("/_health/", &health.Handler{
69                 Token:  rtr.cluster.ManagementToken,
70                 Prefix: "/_health/",
71                 Routes: health.Routes{
72                         "db": rtr.eventSource.DBHealth,
73                 },
74                 Log: func(r *http.Request, err error) {
75                         if err != nil {
76                                 ctxlog.FromContext(r.Context()).WithError(err).Error("error")
77                         }
78                 },
79         })
80 }
81
82 func (rtr *router) makeServer(newSession sessionFactory) *websocket.Server {
83         return &websocket.Server{
84                 Handshake: func(c *websocket.Config, r *http.Request) error {
85                         return nil
86                 },
87                 Handler: websocket.Handler(func(ws *websocket.Conn) {
88                         t0 := time.Now()
89                         logger := ctxlog.FromContext(ws.Request().Context())
90                         logger.Info("connected")
91
92                         stats := rtr.handler.Handle(ws, logger, rtr.eventSource,
93                                 func(ws wsConn, sendq chan<- interface{}) (session, error) {
94                                         return newSession(ws, sendq, rtr.eventSource.DB(), rtr.newPermChecker(), rtr.client)
95                                 })
96
97                         logger.WithFields(logrus.Fields{
98                                 "elapsed": time.Now().Sub(t0).Seconds(),
99                                 "stats":   stats,
100                         }).Info("disconnect")
101                         ws.Close()
102                 }),
103         }
104 }
105
106 func (rtr *router) newReqID() string {
107         rtr.lastReqMtx.Lock()
108         defer rtr.lastReqMtx.Unlock()
109         id := time.Now().UnixNano()
110         if id <= rtr.lastReqID {
111                 id = rtr.lastReqID + 1
112         }
113         return strconv.FormatInt(id, 36)
114 }
115
116 func (rtr *router) DebugStatus() interface{} {
117         s := map[string]interface{}{
118                 "HTTP":     rtr.status,
119                 "Outgoing": rtr.handler.DebugStatus(),
120         }
121         if es, ok := rtr.eventSource.(debugStatuser); ok {
122                 s["EventSource"] = es.DebugStatus()
123         }
124         return s
125 }
126
127 func (rtr *router) Status() interface{} {
128         return map[string]interface{}{
129                 "Clients": atomic.LoadInt64(&rtr.status.ReqsActive),
130                 "Version": cmd.Version.String(),
131         }
132 }
133
134 func (rtr *router) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
135         rtr.setupOnce.Do(rtr.setup)
136         atomic.AddInt64(&rtr.status.ReqsReceived, 1)
137         atomic.AddInt64(&rtr.status.ReqsActive, 1)
138         defer atomic.AddInt64(&rtr.status.ReqsActive, -1)
139
140         logger := ctxlog.FromContext(req.Context()).
141                 WithField("RequestID", rtr.newReqID())
142         ctx := ctxlog.Context(req.Context(), logger)
143         req = req.WithContext(ctx)
144         logger.WithFields(logrus.Fields{
145                 "remoteAddr":      req.RemoteAddr,
146                 "reqForwardedFor": req.Header.Get("X-Forwarded-For"),
147         }).Info("accept request")
148         rtr.mux.ServeHTTP(resp, req)
149 }
150
151 func (rtr *router) jsonHandler(fn func() interface{}) http.Handler {
152         return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
153                 logger := ctxlog.FromContext(r.Context())
154                 w.Header().Set("Content-Type", "application/json")
155                 enc := json.NewEncoder(w)
156                 err := enc.Encode(fn())
157                 if err != nil {
158                         msg := "encode failed"
159                         logger.WithError(err).Error(msg)
160                         http.Error(w, msg, http.StatusInternalServerError)
161                 }
162         })
163 }
164
165 func (rtr *router) CheckHealth() error {
166         rtr.setupOnce.Do(rtr.setup)
167         return rtr.eventSource.DBHealth()
168 }
169
170 func (rtr *router) Done() <-chan struct{} {
171         return rtr.done
172 }