platform: modularize api/gui, add docs-tests-web foundation, and refresh root config
This commit is contained in:
114
selective-vpn-api/app/transport_handlers_health_refresh.go
Normal file
114
selective-vpn-api/app/transport_handlers_health_refresh.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func handleTransportHealthRefresh(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var body TransportHealthRefreshRequest
|
||||
if r.Body != nil {
|
||||
defer r.Body.Close()
|
||||
if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body); err != nil && err != io.EOF {
|
||||
http.Error(w, "bad json", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
force := bool(body.Force)
|
||||
|
||||
transportMu.Lock()
|
||||
st := loadTransportClientsState()
|
||||
transportHealthSWR.syncKnownClients(st.Items)
|
||||
transportMu.Unlock()
|
||||
|
||||
items := make([]TransportHealthRefreshItem, 0, len(st.Items))
|
||||
queued := 0
|
||||
skipped := 0
|
||||
seen := map[string]struct{}{}
|
||||
|
||||
appendItem := func(it TransportClient, reason string, wasQueued bool) {
|
||||
id := strings.TrimSpace(it.ID)
|
||||
if id == "" {
|
||||
return
|
||||
}
|
||||
if _, ok := seen[id]; ok {
|
||||
return
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
items = append(items, TransportHealthRefreshItem{
|
||||
ClientID: id,
|
||||
Status: it.Status,
|
||||
Queued: wasQueued,
|
||||
Reason: strings.TrimSpace(reason),
|
||||
})
|
||||
if wasQueued {
|
||||
queued++
|
||||
} else {
|
||||
skipped++
|
||||
}
|
||||
}
|
||||
|
||||
if len(body.ClientIDs) == 0 {
|
||||
for _, it := range st.Items {
|
||||
if !force && !transportHealthCandidate(it) {
|
||||
appendItem(it, "not eligible for background probe", false)
|
||||
continue
|
||||
}
|
||||
if transportQueueHealthProbe(it, force) {
|
||||
appendItem(it, "queued", true)
|
||||
} else {
|
||||
appendItem(it, "throttled or already fresh", false)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for _, raw := range body.ClientIDs {
|
||||
id := sanitizeID(raw)
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
idx := findTransportClientIndex(st.Items, id)
|
||||
if idx < 0 {
|
||||
items = append(items, TransportHealthRefreshItem{
|
||||
ClientID: id,
|
||||
Queued: false,
|
||||
Reason: "not found",
|
||||
})
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
it := st.Items[idx]
|
||||
if !force && !transportHealthCandidate(it) {
|
||||
appendItem(it, "not eligible for background probe", false)
|
||||
continue
|
||||
}
|
||||
if transportQueueHealthProbe(it, force) {
|
||||
appendItem(it, "queued", true)
|
||||
} else {
|
||||
appendItem(it, "throttled or already fresh", false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resp := TransportHealthRefreshResponse{
|
||||
OK: true,
|
||||
Message: "health refresh queued",
|
||||
Count: len(items),
|
||||
Queued: queued,
|
||||
Skipped: skipped,
|
||||
Items: items,
|
||||
}
|
||||
if resp.Count == 0 {
|
||||
resp.OK = false
|
||||
resp.Code = "TRANSPORT_HEALTH_REFRESH_NO_TARGETS"
|
||||
resp.Message = "no target clients"
|
||||
}
|
||||
writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
Reference in New Issue
Block a user