From e4d3331823b4d465b21600840681bdf2b2563573 Mon Sep 17 00:00:00 2001 From: blek Date: Sun, 5 Nov 2023 15:25:21 +1000 Subject: [PATCH] add option to configure min size to cache proxy --- resource/go.mod | 1 + resource/go.sum | 2 ++ resource/main.go | 16 +++++++++++++--- resource/resourced.toml.example | 5 +++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/resource/go.mod b/resource/go.mod index 0ee4b39..4333b70 100644 --- a/resource/go.mod +++ b/resource/go.mod @@ -5,6 +5,7 @@ go 1.21.3 require ( github.com/BurntSushi/toml v1.3.2 // indirect github.com/andybalholm/brotli v1.0.5 // indirect + github.com/dustin/go-humanize v1.0.1 // indirect github.com/gofiber/fiber/v2 v2.50.0 // indirect github.com/google/uuid v1.3.1 // indirect github.com/klauspost/compress v1.16.7 // indirect diff --git a/resource/go.sum b/resource/go.sum index 76c49cb..c9e3a79 100644 --- a/resource/go.sum +++ b/resource/go.sum @@ -2,6 +2,8 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8 github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/gofiber/fiber/v2 v2.50.0 h1:ia0JaB+uw3GpNSCR5nvC5dsaxXjRU5OEu36aytx+zGw= github.com/gofiber/fiber/v2 v2.50.0/go.mod h1:21eytvay9Is7S6z+OgPi7c7n4++tnClWmhpimVHMimw= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= diff --git a/resource/main.go b/resource/main.go index 513c349..2822d34 100644 --- a/resource/main.go +++ b/resource/main.go @@ -12,6 +12,7 @@ import ( import ( "github.com/BurntSushi/toml" "github.com/gofiber/fiber/v2" + "github.com/dustin/go-humanize" ) type Resource struct { @@ -24,8 +25,9 @@ func (self Resource) Get() ([]byte, error) { } type ResourceDConfig struct { - Enabled bool `toml:"enabled"` - ListenURL string `toml:"listen_url"` + Enabled bool `toml:"enabled"` + ListenURL string `toml:"listen_url"` + ProxyCacheMinSize string `toml:"proxy_cache_min_size",default:5MB` } type Config struct { @@ -58,12 +60,16 @@ func (self Resource) GetProxied() ([]byte, error) { buf, err := ioutil.ReadAll(resp.Body) if err != nil { return make([]byte, 0, 0), err } - ProxyResourceCache[self.Url] = buf + // cache only those that are less than 5 mb + if len(buf) > ProxyCacheMinSize { + ProxyResourceCache[self.Url] = buf + } return buf, nil } var ProxyResourceCache map[string][]byte = make(map[string][]byte) +var ProxyCacheMinSize int func main() { var conf Config @@ -75,6 +81,10 @@ func main() { if err != nil { panic(err) } _ = a + cache_min, err := humanize.ParseBytes(conf.ResourceD.ProxyCacheMinSize) + if err != nil { panic(err) } + ProxyCacheMinSize = int(cache_min) + conf.Validate() app := fiber.New(fiber.Config { diff --git a/resource/resourced.toml.example b/resource/resourced.toml.example index e8fca0e..043888b 100644 --- a/resource/resourced.toml.example +++ b/resource/resourced.toml.example @@ -11,6 +11,11 @@ Enabled=true # URL to listen on ListenUrl="0.0.0.0:80" +# Minibal size of a file to be cached +# File size is parsed via this library: +# https://github.com/dustin/go-humanize +proxy_cache_min_size="5MB" + # Resource ID must be like a java package name # At least one X.X. is required #