Replace cache with single int64

This commit is contained in:
Dane Everitt 2020-08-22 22:50:49 -07:00
parent 138080d5e2
commit c73a53dbe3
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 12 additions and 15 deletions

View File

@ -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",

View File

@ -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}

View File

@ -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