217 lines
5.7 KiB
Go
217 lines
5.7 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type providerStatus struct {
|
|
Name string `json:"name"`
|
|
Configured bool `json:"configured"`
|
|
OK bool `json:"ok"`
|
|
Stale bool `json:"stale,omitempty"`
|
|
URL string `json:"url,omitempty"`
|
|
Model string `json:"model,omitempty"`
|
|
LatencyMS int64 `json:"latency_ms,omitempty"`
|
|
LastOKAt string `json:"last_ok_at,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
type providersStatusResponse struct {
|
|
At time.Time `json:"at"`
|
|
Providers []providerStatus `json:"providers"`
|
|
}
|
|
|
|
type providerHealthSnapshot struct {
|
|
at time.Time
|
|
latencyMS int64
|
|
}
|
|
|
|
const providerStaleOKWindow = 2 * time.Minute
|
|
|
|
func (s *Server) handleProviderStatus(w http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := contextWithTimeout(r, 8*time.Second)
|
|
defer cancel()
|
|
|
|
resp := providersStatusResponse{
|
|
At: time.Now().UTC(),
|
|
Providers: []providerStatus{
|
|
s.checkLLM(ctx),
|
|
s.checkWhisperX(ctx),
|
|
s.checkAudioLLM(ctx, "qwen2-audio", s.cfg.QwenAudioBaseURL, s.cfg.QwenAudioAPIKey, s.cfg.QwenAudioModel, s.cfg.QwenAudioTimeout),
|
|
s.checkAudioLLM(ctx, "voxtral-small", s.cfg.VoxtralBaseURL, s.cfg.VoxtralAPIKey, s.cfg.VoxtralModel, s.cfg.VoxtralTimeout),
|
|
},
|
|
}
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
func (s *Server) checkAudioLLM(ctx context.Context, name, baseURL, apiKey, model string, timeout time.Duration) providerStatus {
|
|
baseURL = strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
|
st := providerStatus{
|
|
Name: name,
|
|
Configured: baseURL != "",
|
|
URL: baseURL,
|
|
Model: model,
|
|
}
|
|
if !st.Configured {
|
|
return st
|
|
}
|
|
if timeout <= 0 {
|
|
timeout = 10 * time.Minute
|
|
}
|
|
start := time.Now()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, st.URL+"/v1/models", nil)
|
|
if err != nil {
|
|
st.Error = err.Error()
|
|
return st
|
|
}
|
|
if apiKey != "" {
|
|
req.Header.Set("Authorization", "Bearer "+apiKey)
|
|
}
|
|
res, err := (&http.Client{Timeout: minDuration(timeout, 3*time.Second)}).Do(req)
|
|
st.LatencyMS = time.Since(start).Milliseconds()
|
|
if err != nil {
|
|
st.Error = err.Error()
|
|
return s.withStaleProviderOK(name, st)
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode >= 300 {
|
|
st.Error = fmt.Sprintf("http %d: %s", res.StatusCode, readSmallBody(res.Body))
|
|
return s.withStaleProviderOK(name, st)
|
|
}
|
|
st.OK = true
|
|
s.rememberProviderOK(name, st.LatencyMS)
|
|
return st
|
|
}
|
|
|
|
func minDuration(a, b time.Duration) time.Duration {
|
|
if a < b {
|
|
return a
|
|
}
|
|
return b
|
|
}
|
|
|
|
func (s *Server) checkLLM(ctx context.Context) providerStatus {
|
|
st := providerStatus{
|
|
Name: "llm",
|
|
Configured: strings.TrimSpace(s.cfg.LLMBaseURL) != "",
|
|
URL: strings.TrimRight(s.cfg.LLMBaseURL, "/"),
|
|
Model: s.cfg.LLMModel,
|
|
}
|
|
if !st.Configured {
|
|
return st
|
|
}
|
|
start := time.Now()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, st.URL+"/v1/models", nil)
|
|
if err != nil {
|
|
st.Error = err.Error()
|
|
return st
|
|
}
|
|
if s.cfg.LLMAPIKey != "" {
|
|
req.Header.Set("Authorization", "Bearer "+s.cfg.LLMAPIKey)
|
|
}
|
|
httpClient := &http.Client{Timeout: s.cfg.LLMTimeout}
|
|
res, err := httpClient.Do(req)
|
|
st.LatencyMS = time.Since(start).Milliseconds()
|
|
if err != nil {
|
|
st.Error = err.Error()
|
|
return s.withStaleProviderOK("llm", st)
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode >= 300 {
|
|
st.Error = fmt.Sprintf("http %d: %s", res.StatusCode, readSmallBody(res.Body))
|
|
return s.withStaleProviderOK("llm", st)
|
|
}
|
|
st.OK = true
|
|
s.rememberProviderOK("llm", st.LatencyMS)
|
|
return st
|
|
}
|
|
|
|
func (s *Server) checkWhisperX(ctx context.Context) providerStatus {
|
|
baseURL := strings.TrimRight(strings.TrimSpace(s.cfg.WhisperXURL), "/")
|
|
st := providerStatus{Name: "whisperx", Configured: baseURL != "", URL: baseURL}
|
|
if !st.Configured {
|
|
return st
|
|
}
|
|
paths := []string{"/health", "/healthz", "/readyz", "/"}
|
|
var lastErr string
|
|
for _, path := range paths {
|
|
cctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
start := time.Now()
|
|
req, err := http.NewRequestWithContext(cctx, http.MethodGet, baseURL+path, nil)
|
|
if err != nil {
|
|
cancel()
|
|
lastErr = err.Error()
|
|
continue
|
|
}
|
|
res, err := (&http.Client{Timeout: 2 * time.Second}).Do(req)
|
|
st.LatencyMS = time.Since(start).Milliseconds()
|
|
cancel()
|
|
if err != nil {
|
|
lastErr = err.Error()
|
|
continue
|
|
}
|
|
body := ""
|
|
if res.StatusCode >= 300 {
|
|
body = readSmallBody(res.Body)
|
|
}
|
|
_ = res.Body.Close()
|
|
if res.StatusCode >= 300 {
|
|
lastErr = fmt.Sprintf("%s http %d: %s", path, res.StatusCode, body)
|
|
continue
|
|
}
|
|
st.OK = true
|
|
s.rememberProviderOK("whisperx", st.LatencyMS)
|
|
return st
|
|
}
|
|
st.Error = lastErr
|
|
return s.withStaleProviderOK("whisperx", st)
|
|
}
|
|
|
|
func (s *Server) rememberProviderOK(name string, latencyMS int64) {
|
|
s.providerMu.Lock()
|
|
defer s.providerMu.Unlock()
|
|
s.providerOKs[name] = providerHealthSnapshot{at: time.Now().UTC(), latencyMS: latencyMS}
|
|
}
|
|
|
|
func (s *Server) withStaleProviderOK(name string, st providerStatus) providerStatus {
|
|
s.providerMu.Lock()
|
|
snap, ok := s.providerOKs[name]
|
|
s.providerMu.Unlock()
|
|
if !ok || time.Since(snap.at) > providerStaleOKWindow {
|
|
return st
|
|
}
|
|
st.OK = true
|
|
st.Stale = true
|
|
if st.LatencyMS == 0 {
|
|
st.LatencyMS = snap.latencyMS
|
|
}
|
|
st.LastOKAt = snap.at.Format(time.RFC3339)
|
|
if st.Error != "" {
|
|
st.Error = "последняя проверка не ответила: " + st.Error
|
|
}
|
|
return st
|
|
}
|
|
|
|
func readSmallBody(r io.Reader) string {
|
|
body, err := io.ReadAll(io.LimitReader(r, 512))
|
|
if err != nil {
|
|
return err.Error()
|
|
}
|
|
var parsed map[string]any
|
|
if err := json.Unmarshal(body, &parsed); err == nil {
|
|
if msg, ok := parsed["error"].(string); ok {
|
|
return msg
|
|
}
|
|
if msg, ok := parsed["detail"].(string); ok {
|
|
return msg
|
|
}
|
|
}
|
|
return strings.TrimSpace(string(body))
|
|
}
|