feat(storage): improve caching in cache driver
All checks were successful
arcad/edge/pipeline/head This commit looks good

ref #20
This commit is contained in:
2023-11-30 19:09:51 +01:00
parent 870db072e0
commit 32f04af138
16 changed files with 312 additions and 103 deletions

View File

@ -6,7 +6,6 @@ import (
"net/http"
"forge.cadoles.com/arcad/edge/pkg/bus"
"github.com/pkg/errors"
)
type contextKey string
@ -15,15 +14,16 @@ var (
contextKeyBus contextKey = "bus"
contextKeyHTTPRequest contextKey = "httpRequest"
contextKeyHTTPClient contextKey = "httpClient"
contextKeySessionID contextKey = "sessionId"
)
func (h *Handler) contextMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(ctx, contextKeyBus, h.bus)
ctx = context.WithValue(ctx, contextKeyHTTPRequest, r)
ctx = context.WithValue(ctx, contextKeyHTTPClient, h.httpClient)
ctx = WithContextBus(ctx, h.bus)
ctx = WithContextHTTPRequest(ctx, r)
ctx = WithContextHTTPClient(ctx, h.httpClient)
r = r.WithContext(ctx)
@ -33,23 +33,43 @@ func (h *Handler) contextMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(fn)
}
func ContextBus(ctx context.Context) bus.Bus {
func ContextBus(ctx context.Context) (bus.Bus, bool) {
return contextValue[bus.Bus](ctx, contextKeyBus)
}
func ContextHTTPRequest(ctx context.Context) *http.Request {
func WithContextBus(parent context.Context, bus bus.Bus) context.Context {
return context.WithValue(parent, contextKeyBus, bus)
}
func ContextHTTPRequest(ctx context.Context) (*http.Request, bool) {
return contextValue[*http.Request](ctx, contextKeyHTTPRequest)
}
func ContextHTTPClient(ctx context.Context) *http.Client {
func WithContextHTTPRequest(parent context.Context, request *http.Request) context.Context {
return context.WithValue(parent, contextKeyHTTPRequest, request)
}
func ContextHTTPClient(ctx context.Context) (*http.Client, bool) {
return contextValue[*http.Client](ctx, contextKeyHTTPClient)
}
func contextValue[T any](ctx context.Context, key any) T {
func WithContextHTTPClient(parent context.Context, client *http.Client) context.Context {
return context.WithValue(parent, contextKeyHTTPClient, client)
}
func ContextSessionID(ctx context.Context) (string, bool) {
return contextValue[string](ctx, contextKeySessionID)
}
func WithContextSessionID(parent context.Context, sessionID string) context.Context {
return context.WithValue(parent, contextKeySessionID, sessionID)
}
func contextValue[T any](ctx context.Context, key any) (T, bool) {
value, ok := ctx.Value(key).(T)
if !ok {
panic(errors.Errorf("could not find key '%v' on context", key))
return *new(T), false
}
return value
return value, true
}

View File

@ -102,14 +102,12 @@ func NewHandler(funcs ...HandlerOptionFunc) *Handler {
r.Get("/client.js.map", handler.handleSDKClientMap)
})
r.Group(func(r chi.Router) {
r.Use(handler.contextMiddleware)
for _, fn := range opts.HTTPMounts {
r.Group(func(r chi.Router) {
fn(r)
})
}
})
for _, fn := range opts.HTTPMounts {
r.Group(func(r chi.Router) {
r.Use(handler.contextMiddleware)
fn(r)
})
}
r.HandleFunc("/sock/*", handler.handleSockJS)
})

View File

@ -5,7 +5,6 @@ import (
"encoding/json"
"net/http"
"forge.cadoles.com/arcad/edge/pkg/module"
"github.com/igm/sockjs-go/v3/sockjs"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
@ -15,11 +14,6 @@ const (
statusChannelClosed = iota
)
const (
ContextKeySessionID module.ContextKey = "sessionId"
ContextKeyOriginRequest module.ContextKey = "originRequest"
)
func (h *Handler) handleSockJS(w http.ResponseWriter, r *http.Request) {
h.mutex.RLock()
defer h.mutex.RUnlock()
@ -181,10 +175,8 @@ func (h *Handler) handleIncomingMessages(ctx context.Context, sess sockjs.Sessio
}
ctx := logger.With(ctx, logger.F("payload", payload))
ctx = module.WithContext(ctx, map[module.ContextKey]any{
ContextKeySessionID: sess.ID(),
ContextKeyOriginRequest: sess.Request(),
})
ctx = WithContextHTTPRequest(ctx, sess.Request())
ctx = WithContextSessionID(ctx, sess.ID())
incomingMessage := NewIncomingMessageEnvelope(ctx, payload)