Remove remote instance proxy handling from handlers

This commit is contained in:
2025-10-25 14:07:11 +02:00
parent 6a973fae2d
commit ff719f3ef9
6 changed files with 13 additions and 167 deletions

View File

@@ -188,14 +188,6 @@ func (i *Instance) GetProxy() (*httputil.ReverseProxy, error) {
return nil, fmt.Errorf("instance %s has no proxy component", i.Name) return nil, fmt.Errorf("instance %s has no proxy component", i.Name)
} }
// Remote instances should not use local proxy - they are handled by RemoteInstanceProxy
opts := i.GetOptions()
if opts != nil && len(opts.Nodes) > 0 {
if _, isLocal := opts.Nodes[i.localNodeName]; !isLocal {
return nil, fmt.Errorf("instance %s is a remote instance and should not use local proxy", i.Name)
}
}
return i.proxy.get() return i.proxy.get()
} }

View File

@@ -577,7 +577,9 @@ func TestRemoteInstanceOperations(t *testing.T) {
LlamaCpp: config.BackendSettings{Command: "llama-server"}, LlamaCpp: config.BackendSettings{Command: "llama-server"},
}, },
Instances: config.InstancesConfig{LogsDir: "/tmp/test"}, Instances: config.InstancesConfig{LogsDir: "/tmp/test"},
Nodes: map[string]config.NodeConfig{}, Nodes: map[string]config.NodeConfig{
"remote-node": {Address: "http://remote-node:8080"},
},
LocalNode: "main", LocalNode: "main",
} }
options := &instance.Options{ options := &instance.Options{
@@ -612,8 +614,8 @@ func TestRemoteInstanceOperations(t *testing.T) {
} }
// GetProxy should fail for remote instance // GetProxy should fail for remote instance
if _, err := inst.GetProxy(); err == nil { if _, err := inst.GetProxy(); err != nil {
t.Error("Expected error when getting proxy for remote instance") t.Error("Expected no error when getting proxy for remote instance")
} }
// GetLogs should fail for remote instance // GetLogs should fail for remote instance

View File

@@ -4,8 +4,6 @@ import (
"llamactl/pkg/config" "llamactl/pkg/config"
"llamactl/pkg/manager" "llamactl/pkg/manager"
"net/http" "net/http"
"net/http/httputil"
"sync"
"time" "time"
) )
@@ -13,8 +11,6 @@ type Handler struct {
InstanceManager manager.InstanceManager InstanceManager manager.InstanceManager
cfg config.AppConfig cfg config.AppConfig
httpClient *http.Client httpClient *http.Client
remoteProxies map[string]*httputil.ReverseProxy // Cache of remote proxies by instance name
remoteProxiesMu sync.RWMutex
} }
func NewHandler(im manager.InstanceManager, cfg config.AppConfig) *Handler { func NewHandler(im manager.InstanceManager, cfg config.AppConfig) *Handler {
@@ -24,6 +20,5 @@ func NewHandler(im manager.InstanceManager, cfg config.AppConfig) *Handler {
httpClient: &http.Client{ httpClient: &http.Client{
Timeout: 30 * time.Second, Timeout: 30 * time.Second,
}, },
remoteProxies: make(map[string]*httputil.ReverseProxy),
} }
} }

View File

@@ -49,7 +49,7 @@ func (h *Handler) LlamaCppProxy(onDemandStart bool) http.HandlerFunc {
return return
} }
if !inst.IsRunning() { if !inst.IsRemote() && !inst.IsRunning() {
if !(onDemandStart && options.OnDemandStart != nil && *options.OnDemandStart) { if !(onDemandStart && options.OnDemandStart != nil && *options.OnDemandStart) {
http.Error(w, "Instance is not running", http.StatusServiceUnavailable) http.Error(w, "Instance is not running", http.StatusServiceUnavailable)

View File

@@ -7,8 +7,6 @@ import (
"llamactl/pkg/manager" "llamactl/pkg/manager"
"llamactl/pkg/validation" "llamactl/pkg/validation"
"net/http" "net/http"
"net/http/httputil"
"net/url"
"strconv" "strconv"
"strings" "strings"
@@ -375,12 +373,6 @@ func (h *Handler) ProxyToInstance() http.HandlerFunc {
return return
} }
// Check if this is a remote instance
if inst.IsRemote() {
h.RemoteInstanceProxy(w, r, validatedName, inst)
return
}
if !inst.IsRunning() { if !inst.IsRunning() {
http.Error(w, "Instance is not running", http.StatusServiceUnavailable) http.Error(w, "Instance is not running", http.StatusServiceUnavailable)
return return
@@ -393,9 +385,12 @@ func (h *Handler) ProxyToInstance() http.HandlerFunc {
return return
} }
// Check if this is a remote instance
if !inst.IsRemote() {
// Strip the "/api/v1/instances/<name>/proxy" prefix from the request URL // Strip the "/api/v1/instances/<name>/proxy" prefix from the request URL
prefix := fmt.Sprintf("/api/v1/instances/%s/proxy", validatedName) prefix := fmt.Sprintf("/api/v1/instances/%s/proxy", validatedName)
r.URL.Path = strings.TrimPrefix(r.URL.Path, prefix) r.URL.Path = strings.TrimPrefix(r.URL.Path, prefix)
}
// Update the last request time for the instance // Update the last request time for the instance
inst.UpdateLastRequestTime() inst.UpdateLastRequestTime()
@@ -408,66 +403,3 @@ func (h *Handler) ProxyToInstance() http.HandlerFunc {
proxy.ServeHTTP(w, r) proxy.ServeHTTP(w, r)
} }
} }
// RemoteInstanceProxy proxies requests to a remote instance
func (h *Handler) RemoteInstanceProxy(w http.ResponseWriter, r *http.Request, name string, inst *instance.Instance) {
// Get the node name from instance options
options := inst.GetOptions()
if options == nil {
http.Error(w, "Instance has no options configured", http.StatusInternalServerError)
return
}
// Get the first node from the set
var nodeName string
for node := range options.Nodes {
nodeName = node
break
}
if nodeName == "" {
http.Error(w, "Instance has no node configured", http.StatusInternalServerError)
return
}
// Check if we have a cached proxy for this node
h.remoteProxiesMu.RLock()
proxy, exists := h.remoteProxies[nodeName]
h.remoteProxiesMu.RUnlock()
if !exists {
// Find node configuration
nodeConfig, exists := h.cfg.Nodes[nodeName]
if !exists {
http.Error(w, fmt.Sprintf("Node %s not found", nodeName), http.StatusInternalServerError)
return
}
// Create reverse proxy to remote node
targetURL, err := url.Parse(nodeConfig.Address)
if err != nil {
http.Error(w, "Failed to parse node address: "+err.Error(), http.StatusInternalServerError)
return
}
proxy = httputil.NewSingleHostReverseProxy(targetURL)
// Modify request before forwarding
originalDirector := proxy.Director
apiKey := nodeConfig.APIKey // Capture for closure
proxy.Director = func(req *http.Request) {
originalDirector(req)
// Add API key if configured
if apiKey != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
}
}
// Cache the proxy by node name
h.remoteProxiesMu.Lock()
h.remoteProxies[nodeName] = proxy
h.remoteProxiesMu.Unlock()
}
// Forward the request using the cached proxy
proxy.ServeHTTP(w, r)
}

View File

@@ -3,13 +3,9 @@ package server
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"llamactl/pkg/instance"
"llamactl/pkg/validation" "llamactl/pkg/validation"
"net/http" "net/http"
"net/http/httputil"
"net/url"
) )
// OpenAIListInstances godoc // OpenAIListInstances godoc
@@ -100,15 +96,7 @@ func (h *Handler) OpenAIProxy() http.HandlerFunc {
return return
} }
// Check if this is a remote instance if !inst.IsRemote() && !inst.IsRunning() {
if inst.IsRemote() {
// Restore the body for the remote proxy
r.Body = io.NopCloser(bytes.NewReader(bodyBytes))
h.RemoteOpenAIProxy(w, r, validatedName, inst)
return
}
if !inst.IsRunning() {
options := inst.GetOptions() options := inst.GetOptions()
allowOnDemand := options != nil && options.OnDemandStart != nil && *options.OnDemandStart allowOnDemand := options != nil && options.OnDemandStart != nil && *options.OnDemandStart
if !allowOnDemand { if !allowOnDemand {
@@ -158,66 +146,3 @@ func (h *Handler) OpenAIProxy() http.HandlerFunc {
proxy.ServeHTTP(w, r) proxy.ServeHTTP(w, r)
} }
} }
// RemoteOpenAIProxy proxies OpenAI-compatible requests to a remote instance
func (h *Handler) RemoteOpenAIProxy(w http.ResponseWriter, r *http.Request, modelName string, inst *instance.Instance) {
// Get the node name from instance options
options := inst.GetOptions()
if options == nil {
http.Error(w, "Instance has no options configured", http.StatusInternalServerError)
return
}
// Get the first node from the set
var nodeName string
for node := range options.Nodes {
nodeName = node
break
}
if nodeName == "" {
http.Error(w, "Instance has no node configured", http.StatusInternalServerError)
return
}
// Check if we have a cached proxy for this node
h.remoteProxiesMu.RLock()
proxy, exists := h.remoteProxies[nodeName]
h.remoteProxiesMu.RUnlock()
if !exists {
// Find node configuration
nodeConfig, exists := h.cfg.Nodes[nodeName]
if !exists {
http.Error(w, fmt.Sprintf("Node %s not found", nodeName), http.StatusInternalServerError)
return
}
// Create reverse proxy to remote node
targetURL, err := url.Parse(nodeConfig.Address)
if err != nil {
http.Error(w, "Failed to parse node address: "+err.Error(), http.StatusInternalServerError)
return
}
proxy = httputil.NewSingleHostReverseProxy(targetURL)
// Modify request before forwarding
originalDirector := proxy.Director
apiKey := nodeConfig.APIKey // Capture for closure
proxy.Director = func(req *http.Request) {
originalDirector(req)
// Add API key if configured
if apiKey != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
}
}
// Cache the proxy
h.remoteProxiesMu.Lock()
h.remoteProxies[nodeName] = proxy
h.remoteProxiesMu.Unlock()
}
// Forward the request using the cached proxy
proxy.ServeHTTP(w, r)
}