| 36 | const shutdownTimeout = 5 * time.Second |
| 37 | |
| 38 | func (rd *ready) onStartup() error { |
| 39 | ln, err := reuseport.Listen("tcp", rd.Addr) |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
| 43 | |
| 44 | rd.Lock() |
| 45 | rd.ln = ln |
| 46 | rd.mux = http.NewServeMux() |
| 47 | rd.done = true |
| 48 | rd.Unlock() |
| 49 | |
| 50 | rd.mux.HandleFunc("/ready", func(w http.ResponseWriter, _ *http.Request) { |
| 51 | rd.Lock() |
| 52 | defer rd.Unlock() |
| 53 | if !rd.done { |
| 54 | w.WriteHeader(http.StatusServiceUnavailable) |
| 55 | io.WriteString(w, "Shutting down") |
| 56 | return |
| 57 | } |
| 58 | ready, notReadyPlugins := plugins.Ready() |
| 59 | if ready { |
| 60 | w.WriteHeader(http.StatusOK) |
| 61 | io.WriteString(w, http.StatusText(http.StatusOK)) |
| 62 | return |
| 63 | } |
| 64 | log.Infof("Plugins not ready: %q", notReadyPlugins) |
| 65 | w.WriteHeader(http.StatusServiceUnavailable) |
| 66 | io.WriteString(w, notReadyPlugins) |
| 67 | }) |
| 68 | |
| 69 | rd.srv = &http.Server{ |
| 70 | Handler: rd.mux, |
| 71 | ReadTimeout: 5 * time.Second, |
| 72 | WriteTimeout: 5 * time.Second, |
| 73 | IdleTimeout: 5 * time.Second, |
| 74 | } |
| 75 | |
| 76 | go func() { rd.srv.Serve(rd.ln) }() |
| 77 | |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | func (rd *ready) onFinalShutdown() error { |
| 82 | rd.Lock() |