handleUpstreams reports the status of the reverse proxy upstream pool.
(w http.ResponseWriter, r *http.Request)
| 60 | // handleUpstreams reports the status of the reverse proxy |
| 61 | // upstream pool. |
| 62 | func (adminUpstreams) handleUpstreams(w http.ResponseWriter, r *http.Request) error { |
| 63 | if r.Method != http.MethodGet { |
| 64 | return caddy.APIError{ |
| 65 | HTTPStatus: http.StatusMethodNotAllowed, |
| 66 | Err: fmt.Errorf("method not allowed"), |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Prep for a JSON response |
| 71 | w.Header().Set("Content-Type", "application/json") |
| 72 | enc := json.NewEncoder(w) |
| 73 | |
| 74 | // Collect the results to respond with |
| 75 | results := []upstreamStatus{} |
| 76 | knownHosts := make(map[string]struct{}) |
| 77 | |
| 78 | // Iterate over the static upstream pool (needs to be fast) |
| 79 | var rangeErr error |
| 80 | hosts.Range(func(key, val any) bool { |
| 81 | address, ok := key.(string) |
| 82 | if !ok { |
| 83 | rangeErr = caddy.APIError{ |
| 84 | HTTPStatus: http.StatusInternalServerError, |
| 85 | Err: fmt.Errorf("could not type assert upstream address"), |
| 86 | } |
| 87 | return false |
| 88 | } |
| 89 | |
| 90 | upstream, ok := val.(*Host) |
| 91 | if !ok { |
| 92 | rangeErr = caddy.APIError{ |
| 93 | HTTPStatus: http.StatusInternalServerError, |
| 94 | Err: fmt.Errorf("could not type assert upstream struct"), |
| 95 | } |
| 96 | return false |
| 97 | } |
| 98 | |
| 99 | knownHosts[address] = struct{}{} |
| 100 | |
| 101 | results = append(results, upstreamStatus{ |
| 102 | Address: address, |
| 103 | NumRequests: upstream.NumRequests(), |
| 104 | Fails: upstream.Fails(), |
| 105 | }) |
| 106 | return true |
| 107 | }) |
| 108 | |
| 109 | currentInFlight := getInFlightRequests() |
| 110 | for address, count := range currentInFlight { |
| 111 | if _, exists := knownHosts[address]; !exists && count > 0 { |
| 112 | results = append(results, upstreamStatus{ |
| 113 | Address: address, |
| 114 | NumRequests: int(count), |
| 115 | Fails: 0, |
| 116 | }) |
| 117 | } |
| 118 | } |
| 119 |