Moonshark/core/runner/Response.go
2025-04-10 13:01:17 -05:00

57 lines
1.3 KiB
Go

package runner
import (
"sync"
"github.com/valyala/fasthttp"
)
// Response represents a unified response from script execution
type Response struct {
// Basic properties
Body any // Body content (any type)
Metadata map[string]any // Additional metadata
// HTTP specific properties
Status int // HTTP status code
Headers map[string]string // HTTP headers
Cookies []*fasthttp.Cookie // HTTP cookies
// Session information
SessionData map[string]any
}
// Response pool to reduce allocations
var responsePool = sync.Pool{
New: func() any {
return &Response{
Status: 200,
Headers: make(map[string]string, 8),
Metadata: make(map[string]any, 8),
Cookies: make([]*fasthttp.Cookie, 0, 4),
SessionData: make(map[string]any, 8),
}
},
}
// NewResponse creates a new response object from the pool
func NewResponse() *Response {
return responsePool.Get().(*Response)
}
// Release returns a response to the pool after cleaning it
func ReleaseResponse(resp *Response) {
if resp == nil {
return
}
resp.Body = nil
resp.Status = 200
resp.Headers = make(map[string]string, 8)
resp.Metadata = make(map[string]any, 8)
resp.Cookies = resp.Cookies[:0]
resp.SessionData = make(map[string]any, 8)
responsePool.Put(resp)
}