diff --git a/pkg/instance/instance.go b/pkg/instance/instance.go index 7d5d7c5..b167fec 100644 --- a/pkg/instance/instance.go +++ b/pkg/instance/instance.go @@ -13,6 +13,7 @@ import ( "net/url" "os/exec" "sync" + "sync/atomic" "time" ) @@ -86,6 +87,9 @@ type Process struct { // Restart control restartCancel context.CancelFunc `json:"-"` // Cancel function for pending restarts monitorDone chan struct{} `json:"-"` // Channel to signal monitor goroutine completion + + // Timeout management + lastRequestTime atomic.Int64 // Unix timestamp of last request } // validateAndCopyOptions validates and creates a deep copy of the provided options diff --git a/pkg/instance/lifecycle.go b/pkg/instance/lifecycle.go index 1442b6e..58b52eb 100644 --- a/pkg/instance/lifecycle.go +++ b/pkg/instance/lifecycle.go @@ -140,6 +140,15 @@ func (i *Process) Stop() error { return nil } +// UpdateLastRequestTime updates the last request access time for the instance via proxy +func (i *Process) UpdateLastRequestTime() { + i.mu.Lock() + defer i.mu.Unlock() + + lastRequestTime := time.Now().Unix() + i.lastRequestTime.Store(lastRequestTime) +} + func (i *Process) monitorProcess() { defer func() { i.mu.Lock() diff --git a/pkg/instance/timeout.go b/pkg/instance/timeout.go deleted file mode 100644 index 1807170..0000000 --- a/pkg/instance/timeout.go +++ /dev/null @@ -1 +0,0 @@ -package instance diff --git a/pkg/server/handlers.go b/pkg/server/handlers.go index 0bb5285..0d473e7 100644 --- a/pkg/server/handlers.go +++ b/pkg/server/handlers.go @@ -472,6 +472,9 @@ func (h *Handler) ProxyToInstance() http.HandlerFunc { proxyPath = "/" + proxyPath } + // Update the last request time for the instance + inst.UpdateLastRequestTime() + // Modify the request to remove the proxy prefix originalPath := r.URL.Path r.URL.Path = proxyPath @@ -582,6 +585,9 @@ func (h *Handler) OpenAIProxy() http.HandlerFunc { return } + // Update last request time for the instance + inst.UpdateLastRequestTime() + // Recreate the request body from the bytes we read r.Body = io.NopCloser(bytes.NewReader(bodyBytes)) r.ContentLength = int64(len(bodyBytes))