From c73a53dbe32261a6fdc6f9045ceb664ce8d161f4 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 22 Aug 2020 22:50:49 -0700 Subject: [PATCH] Replace cache with single int64 --- server/filesystem.go | 20 ++++++++++++-------- server/loader.go | 2 -- server/server.go | 5 ----- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/server/filesystem.go b/server/filesystem.go index 9368293..ad0a452 100644 --- a/server/filesystem.go +++ b/server/filesystem.go @@ -40,8 +40,12 @@ func IsPathResolutionError(err error) bool { } type Filesystem struct { - Server *Server - cacheDiskMu sync.Mutex + mu sync.RWMutex + + lastLookupTime time.Time + diskUsage int64 + + Server *Server } // Returns the root path that contains all of a server's data. @@ -241,11 +245,11 @@ func (fs *Filesystem) getCachedDiskUsage() (int64, error) { // // This effectively the same speed as running this call in parallel since this cache will return // instantly on the second call. - fs.cacheDiskMu.Lock() - defer fs.cacheDiskMu.Unlock() + fs.mu.Lock() + defer fs.mu.Unlock() - if x, exists := fs.Server.cache.Get("disk_used"); exists { - return x.(int64), nil + if fs.lastLookupTime.After(time.Now().Add(time.Second * -60)) { + return fs.diskUsage, nil } // If there is no size its either because there is no data (in which case running this function @@ -257,7 +261,8 @@ func (fs *Filesystem) getCachedDiskUsage() (int64, error) { // Always cache the size, even if there is an error. We want to always return that value // so that we don't cause an endless loop of determining the disk size if there is a temporary // error encountered. - fs.Server.cache.Set("disk_used", size, time.Second*60) + fs.lastLookupTime = time.Now() + atomic.StoreInt64(&fs.diskUsage, size) return size, err } @@ -416,7 +421,6 @@ func (fs *Filesystem) unsafeStat(p string) (*Stat, error) { } } - st := &Stat{ Info: s, Mimetype: "inode/directory", diff --git a/server/loader.go b/server/loader.go index 92283ee..79b2cca 100644 --- a/server/loader.go +++ b/server/loader.go @@ -5,7 +5,6 @@ import ( "github.com/apex/log" "github.com/creasty/defaults" "github.com/gammazero/workerpool" - "github.com/patrickmn/go-cache" "github.com/pkg/errors" "github.com/pterodactyl/wings/api" "github.com/pterodactyl/wings/environment" @@ -98,7 +97,6 @@ func FromConfiguration(data *api.ServerConfigurationResponse) (*Server, error) { return nil, err } - s.cache = cache.New(time.Minute*10, time.Minute*15) s.Archiver = Archiver{Server: s} s.Filesystem = Filesystem{Server: s} diff --git a/server/server.go b/server/server.go index cf7c27a..b55399d 100644 --- a/server/server.go +++ b/server/server.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "github.com/apex/log" - "github.com/patrickmn/go-cache" "github.com/pkg/errors" "github.com/pterodactyl/wings/api" "github.com/pterodactyl/wings/environment" @@ -37,10 +36,6 @@ type Server struct { Environment environment.ProcessEnvironment `json:"-"` Filesystem Filesystem `json:"-"` - // Server cache used to store frequently requested information in memory and make - // certain long operations return faster. For example, FS disk space usage. - cache *cache.Cache - // Events emitted by the server instance. emitter *events.EventBus