Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(chore): adapt to storages usage of protobuf #538

Merged
merged 5 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions configurationtypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"time"

"go.uber.org/zap"
"github.com/darkweak/storages/core"
yaml "gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -439,8 +439,8 @@ type AbstractConfigurationInterface interface {
GetDefaultCache() DefaultCacheInterface
GetAPI() API
GetLogLevel() string
GetLogger() *zap.Logger
SetLogger(*zap.Logger)
GetLogger() core.Logger
SetLogger(core.Logger)
GetYkeys() map[string]SurrogateKeys
GetSurrogateKeys() map[string]SurrogateKeys
GetCacheKeys() CacheKeys
Expand Down
2 changes: 1 addition & 1 deletion context/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (cc *cacheContext) SetupContext(c configurationtypes.AbstractConfigurationI
if c.GetDefaultCache().GetCacheName() != "" {
cc.cacheName = c.GetDefaultCache().GetCacheName()
}
c.GetLogger().Sugar().Debugf("Set %s as Cache-Status name", cc.cacheName)
c.GetLogger().Debugf("Set %s as Cache-Status name", cc.cacheName)
}

func (cc *cacheContext) SetContext(req *http.Request) *http.Request {
Expand Down
9 changes: 5 additions & 4 deletions context/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/storages/core"
"go.uber.org/zap"
)

Expand All @@ -29,10 +30,10 @@ func (*testConfiguration) GetAPI() configurationtypes.API {
func (*testConfiguration) GetLogLevel() string {
return ""
}
func (*testConfiguration) GetLogger() *zap.Logger {
return zap.NewNop()
func (*testConfiguration) GetLogger() core.Logger {
return zap.NewNop().Sugar()
}
func (*testConfiguration) SetLogger(*zap.Logger) {
func (*testConfiguration) SetLogger(core.Logger) {
}
func (*testConfiguration) GetYkeys() map[string]configurationtypes.SurrogateKeys {
return nil
Expand All @@ -49,7 +50,7 @@ func Test_CacheContext_SetupContext(t *testing.T) {
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
c.SetLogger(zap.NewNop().Sugar())
ctx := cacheContext{}

ctx.SetupContext(&c)
Expand Down
2 changes: 1 addition & 1 deletion context/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Test_GraphQLContext_SetContext(t *testing.T) {
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
c.SetLogger(zap.NewNop().Sugar())
ctx := graphQLContext{custom: true}

req := httptest.NewRequest(http.MethodGet, "http://domain.com", nil)
Expand Down
2 changes: 1 addition & 1 deletion context/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (m *methodContext) SetupContext(c configurationtypes.AbstractConfigurationI
m.allowedVerbs = c.GetDefaultCache().GetAllowedHTTPVerbs()
m.custom = true
}
c.GetLogger().Sugar().Debugf("Allow %d method(s). %v.", len(m.allowedVerbs), m.allowedVerbs)
c.GetLogger().Debugf("Allow %d method(s). %v.", len(m.allowedVerbs), m.allowedVerbs)
}

func (m *methodContext) SetContext(req *http.Request) *http.Request {
Expand Down
4 changes: 2 additions & 2 deletions context/method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Test_MethodContext_SetupContext(t *testing.T) {
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
c.SetLogger(zap.NewNop().Sugar())
ctx := methodContext{}

ctx.SetupContext(&c)
Expand All @@ -34,7 +34,7 @@ func Test_MethodContext_SetContext(t *testing.T) {
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
c.SetLogger(zap.NewNop().Sugar())
ctx := methodContext{}
c.defaultCache.AllowedHTTPVerbs = []string{http.MethodGet, http.MethodHead}
ctx.SetupContext(&c)
Expand Down
2 changes: 1 addition & 1 deletion context/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (mc *ModeContext) SetupContext(c configurationtypes.AbstractConfigurationIn
mc.Bypass_request = mode == "bypass" || mode == "bypass_request"
mc.Bypass_response = mode == "bypass" || mode == "bypass_response"
mc.Strict = !mc.Bypass_request && !mc.Bypass_response
c.GetLogger().Sugar().Debugf("The cache logic will run as %s: %+v", mode, mc)
c.GetLogger().Debugf("The cache logic will run as %s: %+v", mode, mc)
}

func (mc *ModeContext) SetContext(req *http.Request) *http.Request {
Expand Down
2 changes: 1 addition & 1 deletion context/mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Test_ModeContext_SetupContext(t *testing.T) {
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
c.SetLogger(zap.NewNop().Sugar())
ctx := ModeContext{}

ctx.SetupContext(&c)
Expand Down
4 changes: 2 additions & 2 deletions context/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (t *timeoutContext) SetupContext(c configurationtypes.AbstractConfiguration
if c.GetDefaultCache().GetTimeout().Backend.Duration != 0 {
t.timeoutBackend = c.GetDefaultCache().GetTimeout().Backend.Duration
}
c.GetLogger().Sugar().Infof("Set backend timeout to %v", t.timeoutBackend)
c.GetLogger().Sugar().Infof("Set cache timeout to %v", t.timeoutBackend)
c.GetLogger().Infof("Set backend timeout to %v", t.timeoutBackend)
c.GetLogger().Infof("Set cache timeout to %v", t.timeoutBackend)
}

func (t *timeoutContext) SetContext(req *http.Request) *http.Request {
Expand Down
4 changes: 2 additions & 2 deletions context/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Test_TimeoutContext_SetupContext(t *testing.T) {
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
c.SetLogger(zap.NewNop().Sugar())
ctx := timeoutContext{}

ctx.SetupContext(&c)
Expand Down Expand Up @@ -61,7 +61,7 @@ func Test_TimeoutContext_SetContext(t *testing.T) {
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
c.SetLogger(zap.NewNop().Sugar())
ctx := timeoutContext{}
ctx.SetupContext(&c)

Expand Down
6 changes: 3 additions & 3 deletions context/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Test_Context_Init(t *testing.T) {
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
c.SetLogger(zap.NewNop().Sugar())
co := GetContext()

co.Init(&c)
Expand All @@ -31,7 +31,7 @@ func Test_Context_SetContext(t *testing.T) {
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
c.SetLogger(zap.NewNop().Sugar())
co := GetContext()

co.Init(&c)
Expand All @@ -58,7 +58,7 @@ func Test_Context_SetBaseContext(t *testing.T) {
c := testConfiguration{
defaultCache: &dc,
}
c.SetLogger(zap.NewNop())
c.SetLogger(zap.NewNop().Sugar())
co := GetContext()

co.Init(&c)
Expand Down
13 changes: 6 additions & 7 deletions docs/website/content/docs/storages/add-own.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Your `struct` must implement it to be a valid storer and be registered in the st

By convention we declare a `Factory` function that respects this signature:
```go
func(providerConfiguration core.CacheProvider, logger *zap.Logger, stale time.Duration) (core.Storer, error)
func(providerConfiguration core.CacheProvider, logger core.Logger, stale time.Duration) (core.Storer, error)
```

And the `Storer` interface is the following:
Expand Down Expand Up @@ -47,18 +47,17 @@ import (

"github.com/darkweak/souin/pkg/storage/types"
"github.com/darkweak/storages/core"
"go.uber.org/zap"
)

// custom storage provider type
type customStorage struct {
m *sync.Map
stale time.Duration
logger *zap.Logger
logger core.Logger
}

// Factory function create new custom storage instance
func Factory(_ core.CacheProvider, logger *zap.Logger, stale time.Duration) (types.Storer, error) {
func Factory(_ core.CacheProvider, logger core.Logger, stale time.Duration) (types.Storer, error) {
return &customStorage{m: &sync.Map{}, logger: logger, stale: stale}, nil
}
```
Expand Down Expand Up @@ -164,7 +163,7 @@ func (provider *customStorage) SetMultiLevel(baseKey, variedKey string, value []
var e error
compressed := new(bytes.Buffer)
if _, e = lz4.NewWriter(compressed).ReadFrom(bytes.NewReader(value)); e != nil {
provider.logger.Sugar().Errorf("Impossible to compress the key %s into Badger, %v", variedKey, e)
provider.logger.Errorf("Impossible to compress the key %s into Badger, %v", variedKey, e)
return e
}

Expand All @@ -182,7 +181,7 @@ func (provider *customStorage) SetMultiLevel(baseKey, variedKey string, value []
return e
}

provider.logger.Sugar().Debugf("Store the new mapping for the key %s in customStorage", variedKey)
provider.logger.Debugf("Store the new mapping for the key %s in customStorage", variedKey)
provider.m.Store(mappingKey, val)
return nil
}
Expand Down Expand Up @@ -233,7 +232,7 @@ After that you'll be able to register your storage using
// anywhere.go
...
logger, _ := zap.NewProduction()
customStorage, _ := your_package.Factory(core.CacheProvider{}, logger, time.Hour)
customStorage, _ := your_package.Factory(core.CacheProvider{}, logger.Sugar(), time.Hour)
// It will register as `YOUR_CUSTOM_STORAGE-THE_UUID`.
core.RegisterStorage(customStorage)

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ go 1.22.1
require (
github.com/caddyserver/caddy/v2 v2.8.4
github.com/cespare/xxhash/v2 v2.2.0
github.com/darkweak/storages/core v0.0.4
github.com/darkweak/storages/core v0.0.7
github.com/google/uuid v1.6.0
github.com/pierrec/lz4/v4 v4.1.21
github.com/pquerna/cachecontrol v0.2.0
github.com/prometheus/client_golang v1.19.1
github.com/prometheus/client_model v0.5.0
go.uber.org/zap v1.27.0
golang.org/x/sync v0.7.0
google.golang.org/protobuf v1.34.2
gopkg.in/yaml.v3 v3.0.1
)

Expand Down Expand Up @@ -109,7 +110,6 @@ require (
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240506185236-b8a5c65736ae // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6 // indirect
google.golang.org/protobuf v1.34.1 // indirect
howett.net/plist v1.0.0 // indirect
)

Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/darkweak/go-esi v0.0.5 h1:b9LHI8Tz46R+i6p8avKPHAIBRQUCZDebNmKm5w/Zrns=
github.com/darkweak/go-esi v0.0.5/go.mod h1:koCJqwum1u6mslyZuq/Phm6hfG1K3ZK5Y7jrUBTH654=
github.com/darkweak/storages/core v0.0.4 h1:rpFTiN+CHwdlXThS28mRpvsSfuMVqWcOe/xtC5A2AQg=
github.com/darkweak/storages/core v0.0.4/go.mod h1:wLp1cOAB4WUd46BBOtV4Lwot4GD+8fZbtIw6QM7fYuc=
github.com/darkweak/storages/core v0.0.7 h1:QVbiNOjRvqfXtTy3a+SadCghzsATHO+4xazBhmuyPVk=
github.com/darkweak/storages/core v0.0.7/go.mod h1:ajTpB9IFLRIRY0EEFLjM5vtsrcNTh+TJK9yRxgG5/wY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -576,8 +576,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6 h1:
google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
27 changes: 14 additions & 13 deletions pkg/api/souin.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package api

import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -14,6 +12,8 @@ import (
"github.com/darkweak/souin/pkg/storage/types"
"github.com/darkweak/souin/pkg/surrogate/providers"
"github.com/darkweak/storages/core"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)

// SouinAPI object contains informations related to the endpoints
Expand Down Expand Up @@ -71,8 +71,9 @@ func (s *SouinAPI) BulkDelete(key string, purge bool) {
for _, current := range s.storers {
if b := current.Get(core.MappingKeyPrefix + key); len(b) > 0 {
var mapping core.StorageMapper
if e := gob.NewDecoder(bytes.NewBuffer(b)).Decode(&mapping); e == nil {
for k := range mapping.Mapping {

if e := proto.Unmarshal(b, &mapping); e == nil {
for k := range mapping.GetMapping() {
current.Delete(k)
}
}
Expand All @@ -82,7 +83,7 @@ func (s *SouinAPI) BulkDelete(key string, purge bool) {
} else {
newFreshTime := time.Now()
for k, v := range mapping.Mapping {
v.FreshTime = newFreshTime
v.FreshTime = timestamppb.New(newFreshTime)
mapping.Mapping[k] = v
}
}
Expand Down Expand Up @@ -156,29 +157,29 @@ func (s *SouinAPI) purgeMapping() {
infiniteStoreDuration := storageToInfiniteTTLMap[current.Name()]
values := current.MapKeys(core.MappingKeyPrefix)
for k, v := range values {
var mapping core.StorageMapper
e := gob.NewDecoder(bytes.NewBuffer([]byte(v))).Decode(&mapping)
mapping := &core.StorageMapper{}

e := proto.Unmarshal([]byte(v), mapping)
if e != nil {
current.Delete(core.MappingKeyPrefix + k)
continue
}

updated := false
for key, val := range mapping.Mapping {
if now.Sub(val.FreshTime) > 0 && now.Sub(val.StaleTime) > 0 {
delete(mapping.Mapping, key)
for key, val := range mapping.GetMapping() {
if now.Sub(val.FreshTime.AsTime()) > 0 && now.Sub(val.StaleTime.AsTime()) > 0 {
delete(mapping.GetMapping(), key)
updated = true
}
}

if updated {
buf := new(bytes.Buffer)
e = gob.NewEncoder(buf).Encode(mapping)
v, e := proto.Marshal(mapping)
if e != nil {
fmt.Println("Impossible to re-encode the mapping", core.MappingKeyPrefix+k)
current.Delete(core.MappingKeyPrefix + k)
}
_ = current.Set(core.MappingKeyPrefix+k, buf.Bytes(), infiniteStoreDuration)
_ = current.Set(core.MappingKeyPrefix+k, v, infiniteStoreDuration)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/middleware/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package middleware

import (
"github.com/darkweak/souin/configurationtypes"
"go.uber.org/zap"
"github.com/darkweak/storages/core"
)

// BaseConfiguration holder
Expand All @@ -12,7 +12,7 @@ type BaseConfiguration struct {
CacheKeys configurationtypes.CacheKeys `json:"cache_keys" yaml:"cache_keys"`
URLs map[string]configurationtypes.URL `json:"urls" yaml:"urls"`
LogLevel string `json:"log_level" yaml:"log_level"`
Logger *zap.Logger
Logger core.Logger
PluginName string
Ykeys map[string]configurationtypes.SurrogateKeys `json:"ykeys" yaml:"ykeys"`
SurrogateKeys map[string]configurationtypes.SurrogateKeys `json:"surrogate_keys" yaml:"surrogate_keys"`
Expand Down Expand Up @@ -44,12 +44,12 @@ func (c *BaseConfiguration) GetLogLevel() string {
}

// GetLogger get the logger
func (c *BaseConfiguration) GetLogger() *zap.Logger {
func (c *BaseConfiguration) GetLogger() core.Logger {
return c.Logger
}

// SetLogger set the logger
func (c *BaseConfiguration) SetLogger(l *zap.Logger) {
func (c *BaseConfiguration) SetLogger(l core.Logger) {
c.Logger = l
}

Expand Down
Loading
Loading