Support streaming responses from functions

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alex@openfaas.com>
This commit is contained in:
Alex Ellis (OpenFaaS Ltd)
2024-01-11 10:40:15 +00:00
committed by Alex Ellis
parent 02205b8b19
commit 4679f27804
8 changed files with 121 additions and 19 deletions

View File

@ -7,12 +7,17 @@ import (
)
func NewHttpWriteInterceptor(w http.ResponseWriter) *HttpWriteInterceptor {
return &HttpWriteInterceptor{w, 0}
return &HttpWriteInterceptor{
ResponseWriter: w,
statusCode: 0,
bytesWritten: 0,
}
}
type HttpWriteInterceptor struct {
http.ResponseWriter
statusCode int
statusCode int
bytesWritten int64
}
func (c *HttpWriteInterceptor) Status() int {
@ -22,6 +27,10 @@ func (c *HttpWriteInterceptor) Status() int {
return c.statusCode
}
func (c *HttpWriteInterceptor) BytesWritten() int64 {
return c.bytesWritten
}
func (c *HttpWriteInterceptor) Header() http.Header {
return c.ResponseWriter.Header()
}
@ -30,6 +39,9 @@ func (c *HttpWriteInterceptor) Write(data []byte) (int, error) {
if c.statusCode == 0 {
c.WriteHeader(http.StatusOK)
}
c.bytesWritten += int64(len(data))
return c.ResponseWriter.Write(data)
}

View File

@ -0,0 +1,52 @@
package types
import "time"
const (
TypeFunctionUsage = "function_usage"
TypeAPIAccess = "api_access"
)
type Event interface {
EventType() string
}
type FunctionUsageEvent struct {
Namespace string `json:"namespace"`
FunctionName string `json:"function_name"`
Started time.Time `json:"started"`
Duration time.Duration `json:"duration"`
MemoryBytes int64 `json:"memory_bytes"`
}
func (e FunctionUsageEvent) EventType() string {
return TypeFunctionUsage
}
type APIAccessEvent struct {
Actor *Actor `json:"actor,omitempty"`
Path string `json:"path"`
Method string `json:"method"`
Actions []string `json:"actions"`
ResponseCode int `json:"response_code"`
CustomMessage string `json:"custom_message,omitempty"`
Namespace string `json:"namespace,omitempty"`
Time time.Time `json:"time"`
}
func (e APIAccessEvent) EventType() string {
return TypeAPIAccess
}
// Actor is the user that triggered an event.
// Get from OIDC claims, we can add any of the default OIDC profile or email claim fields if desired.
type Actor struct {
// OIDC subject, a unique identifier of the user.
Sub string `json:"sub"`
// Full name of the subject, can be the name of a user of OpenFaaS component.
Name string `json:"name,omitempty"`
// OpenFaaS issuer
Issuer string `json:"issuer,omitempty"`
// Federated issuer
FedIssuer string `json:"fed_issuer,omitempty"`
}