Start launches the HTTP server and a background goroutine that computes per-second rates.
(port int)
| 42 | // Start launches the HTTP server and a background goroutine that computes |
| 43 | // per-second rates. |
| 44 | func (s *JSONStats) Start(port int) { |
| 45 | go s.computeRates() |
| 46 | |
| 47 | mux := http.NewServeMux() |
| 48 | mux.HandleFunc("/", s.handleRequest) |
| 49 | addr := fmt.Sprintf(":%d", port) |
| 50 | slog.Info("starting JSON monitoring server", "addr", addr) |
| 51 | srv := &http.Server{ |
| 52 | Addr: addr, |
| 53 | Handler: mux, |
| 54 | ReadTimeout: 5 * time.Second, |
| 55 | WriteTimeout: 5 * time.Second, |
| 56 | } |
| 57 | if err := srv.ListenAndServe(); err != nil { |
| 58 | slog.Error("monitoring server failed", "error", err) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | func (s *JSONStats) computeRates() { |
| 63 | ticker := time.NewTicker(time.Second) |