Skip to content

Commit

Permalink
fix(chore): probably memory leak solved
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Oct 12, 2023
1 parent 2817723 commit 33eace3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 deletions.
1 change: 1 addition & 0 deletions pkg/api/souin.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (s *SouinAPI) HandleRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
case http.MethodPost:
var invalidator invalidation
defer r.Body.Close()
err := json.NewDecoder(r.Body).Decode(&invalidator)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
Expand Down
6 changes: 3 additions & 3 deletions plugins/caddy/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// SouinApp contains the whole Souin necessary items
type SouinApp struct {
*DefaultCache
DefaultCache
// The provider to use.
Storers []storage.Storer
// Surrogate storage to support th econfiguration reload without surrogate-key data loss.
Expand All @@ -29,15 +29,15 @@ func init() {
}

// Provision implements caddy.Provisioner
func (s *SouinApp) Provision(_ caddy.Context) error {
func (s SouinApp) Provision(_ caddy.Context) error {
return nil
}

// Start will start the App
func (s SouinApp) Start() error {
_, _ = up.Delete(stored_providers_key)
_, _ = up.LoadOrStore(stored_providers_key, newStorageProvider())
if s.DefaultCache != nil && s.DefaultCache.GetTTL() == 0 {
if s.DefaultCache.GetTTL() == 0 {
return errors.New("Invalid/Incomplete default cache declaration")
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions plugins/caddy/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (d *DefaultCache) GetDefaultCacheControl() string {
// Configuration holder
type Configuration struct {
// Default cache to fallback on when none are redefined.
DefaultCache *DefaultCache
DefaultCache DefaultCache
// API endpoints enablers.
API configurationtypes.API
// Cache keys configuration.
Expand All @@ -164,7 +164,7 @@ func (c *Configuration) GetUrls() map[string]configurationtypes.URL {

// GetDefaultCache get the default cache
func (c *Configuration) GetDefaultCache() configurationtypes.DefaultCacheInterface {
return c.DefaultCache
return &c.DefaultCache
}

// GetAPI get the default cache
Expand Down
48 changes: 22 additions & 26 deletions plugins/caddy/httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func init() {
type SouinCaddyMiddleware struct {
*middleware.SouinBaseHandler
logger *zap.Logger
Configuration *Configuration
cacheKeys configurationtypes.CacheKeys
Configuration Configuration
// Logger level, fallback on caddy's one when not redefined.
LogLevel string `json:"log_level,omitempty"`
// Allowed HTTP verbs to be cached by the system.
Expand Down Expand Up @@ -87,10 +86,7 @@ func (s *SouinCaddyMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request
}

func (s *SouinCaddyMiddleware) configurationPropertyMapper() error {
if s.Configuration != nil {
return nil
}
defaultCache := &DefaultCache{
defaultCache := DefaultCache{
Badger: s.Badger,
Nuts: s.Nuts,
Key: s.Key,
Expand All @@ -106,33 +102,33 @@ func (s *SouinCaddyMiddleware) configurationPropertyMapper() error {
Stale: s.Stale,
Storers: s.Storers,
}
if s.Configuration == nil {
s.Configuration = &Configuration{
CacheKeys: s.cacheKeys,
DefaultCache: defaultCache,
LogLevel: s.LogLevel,
}
}
// if s.Configuration == nil {
// s.Configuration = Configuration{
// CacheKeys: s.cacheKeys,
// DefaultCache: defaultCache,
// LogLevel: s.LogLevel,
// }
// }
s.Configuration.DefaultCache = defaultCache
return nil
}

// FromApp to initialize configuration from App structure.
func (s *SouinCaddyMiddleware) FromApp(app *SouinApp) error {
if s.Configuration == nil {
s.Configuration = &Configuration{
if s.Configuration.GetDefaultCache() == nil {
s.Configuration = Configuration{
URLs: make(map[string]configurationtypes.URL),
}
}

if app.DefaultCache == nil {
if app.DefaultCache.GetTTL() == 0 {
return nil
}

s.Configuration.API = app.API

if s.Configuration.DefaultCache == nil {
s.Configuration.DefaultCache = &DefaultCache{
if s.Configuration.GetDefaultCache() == nil {
s.Configuration.DefaultCache = DefaultCache{
AllowedHTTPVerbs: app.DefaultCache.AllowedHTTPVerbs,
Headers: app.Headers,
Key: app.Key,
Expand Down Expand Up @@ -235,7 +231,7 @@ func (s *SouinCaddyMiddleware) Provision(ctx caddy.Context) error {
return err
}

bh := middleware.NewHTTPCacheHandler(s.Configuration)
bh := middleware.NewHTTPCacheHandler(&s.Configuration)
surrogates, ok := up.LoadOrStore(surrogate_key, bh.SurrogateKeyStorer)
if ok {
bh.SurrogateKeyStorer = surrogates.(surrogates_providers.SurrogateInterface)
Expand All @@ -261,7 +257,7 @@ func (s *SouinCaddyMiddleware) Provision(ctx caddy.Context) error {
if eo.GetDM() == nil {
v, l, e := up.LoadOrNew(key, func() (caddy.Destructor, error) {
s.logger.Sugar().Debug("Create a new olric instance.")
eo, err := storage.EmbeddedOlricConnectionFactory(s.Configuration)
eo, err := storage.EmbeddedOlricConnectionFactory(&s.Configuration)
if eo != nil {
return eo.(*storage.EmbeddedOlric), err
}
Expand Down Expand Up @@ -294,8 +290,8 @@ func (s *SouinCaddyMiddleware) Provision(ctx caddy.Context) error {

func parseCaddyfileGlobalOption(h *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
souinApp := new(SouinApp)
cfg := &Configuration{
DefaultCache: &DefaultCache{
cfg := Configuration{
DefaultCache: DefaultCache{
AllowedHTTPVerbs: make([]string, 0),
Distributed: false,
Headers: []string{},
Expand All @@ -308,7 +304,7 @@ func parseCaddyfileGlobalOption(h *caddyfile.Dispenser, _ interface{}) (interfac
URLs: make(map[string]configurationtypes.URL),
}

err := parseConfiguration(cfg, h, true)
err := parseConfiguration(&cfg, h, true)

souinApp.DefaultCache = cfg.DefaultCache
souinApp.API = cfg.API
Expand All @@ -328,10 +324,10 @@ func (s *SouinCaddyMiddleware) UnmarshalCaddyfile(h *caddyfile.Dispenser) error
dc := DefaultCache{
AllowedHTTPVerbs: make([]string, 0),
}
s.Configuration = &Configuration{
DefaultCache: &dc,
s.Configuration = Configuration{
DefaultCache: dc,
}
return parseConfiguration(s.Configuration, h, false)
return parseConfiguration(&s.Configuration, h, false)
}

// Interface guards
Expand Down

0 comments on commit 33eace3

Please sign in to comment.