diff --git a/pkg/instance/instance.go b/pkg/instance/instance.go index 7366a03..5e5dc27 100644 --- a/pkg/instance/instance.go +++ b/pkg/instance/instance.go @@ -5,7 +5,7 @@ import ( "fmt" "llamactl/pkg/config" "log" - "net/http/httputil" + "net/http" "time" ) @@ -182,15 +182,6 @@ func (i *Instance) GetPort() int { return i.options.GetPort() } -// GetProxy returns the reverse proxy for this instance -func (i *Instance) GetProxy() (*httputil.ReverseProxy, error) { - if i.proxy == nil { - return nil, fmt.Errorf("instance %s has no proxy component", i.Name) - } - - return i.proxy.get() -} - func (i *Instance) IsRemote() bool { opts := i.GetOptions() if opts == nil { @@ -242,6 +233,22 @@ func (i *Instance) ShouldTimeout() bool { return i.proxy.shouldTimeout() } +// GetInflightRequests returns the current number of inflight requests +func (i *Instance) GetInflightRequests() int32 { + if i.proxy == nil { + return 0 + } + return i.proxy.getInflightRequests() +} + +// ServeHTTP serves HTTP requests through the proxy with request tracking and shutdown handling +func (i *Instance) ServeHTTP(w http.ResponseWriter, r *http.Request) error { + if i.proxy == nil { + return fmt.Errorf("instance %s has no proxy component", i.Name) + } + return i.proxy.serveHTTP(w, r) +} + func (i *Instance) getCommand() string { opts := i.GetOptions() if opts == nil { diff --git a/pkg/instance/instance_test.go b/pkg/instance/instance_test.go index a843845..122cc96 100644 --- a/pkg/instance/instance_test.go +++ b/pkg/instance/instance_test.go @@ -171,64 +171,6 @@ func TestSetOptions(t *testing.T) { } } -func TestGetProxy(t *testing.T) { - globalConfig := &config.AppConfig{ - Backends: config.BackendConfig{ - LlamaCpp: config.BackendSettings{ - Command: "llama-server", - Args: []string{}, - }, - MLX: config.BackendSettings{ - Command: "mlx_lm.server", - Args: []string{}, - }, - VLLM: config.BackendSettings{ - Command: "vllm", - Args: []string{"serve"}, - }, - }, - Instances: config.InstancesConfig{ - LogsDir: "/tmp/test", - }, - Nodes: map[string]config.NodeConfig{}, - LocalNode: "main", - } - - options := &instance.Options{ - Nodes: map[string]struct{}{"main": {}}, - BackendOptions: backends.Options{ - BackendType: backends.BackendTypeLlamaCpp, - LlamaServerOptions: &backends.LlamaServerOptions{ - Host: "localhost", - Port: 8080, - }, - }, - } - - // Mock onStatusChange function - mockOnStatusChange := func(oldStatus, newStatus instance.Status) {} - - inst := instance.New("test-instance", globalConfig, options, mockOnStatusChange) - - // Get proxy for the first time - proxy1, err := inst.GetProxy() - if err != nil { - t.Fatalf("GetProxy failed: %v", err) - } - if proxy1 == nil { - t.Error("Expected proxy to be created") - } - - // Get proxy again - should return cached version - proxy2, err := inst.GetProxy() - if err != nil { - t.Fatalf("GetProxy failed: %v", err) - } - if proxy1 != proxy2 { - t.Error("Expected cached proxy to be returned") - } -} - func TestMarshalJSON(t *testing.T) { globalConfig := &config.AppConfig{ Backends: config.BackendConfig{ @@ -613,11 +555,6 @@ func TestRemoteInstanceOperations(t *testing.T) { t.Error("Expected error when restarting remote instance") } - // GetProxy should fail for remote instance - if _, err := inst.GetProxy(); err != nil { - t.Error("Expected no error when getting proxy for remote instance") - } - // GetLogs should fail for remote instance if _, err := inst.GetLogs(10); err == nil { t.Error("Expected error when getting logs for remote instance")