Add env on startup (#283)

* Add READ_BUFFER_SIZE, CONCURRENCY,DISABLE_KEEPALIVE to ENV

* Add TestGuessSupportedFormat
This commit is contained in:
Nova Kwok 2023-10-17 22:41:01 +08:00 committed by GitHub
parent 0b0e5b1216
commit 655fb1dcca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 11 deletions

View File

@ -74,8 +74,6 @@ services:
restart: always
environment:
- MALLOC_ARENA_MAX=1
# - LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2
# - LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4.5.6
volumes:
- ./path/to/pics:/opt/pics
- ./path/to/exhaust:/opt/exhaust
@ -83,11 +81,6 @@ services:
- ./config.json:/etc/config.json
ports:
- 127.0.0.1:3333:3333
deploy:
resources:
limits:
memory: 400M
memswap_limit: 400M
```
You can refer to [Docker | WebP Server Documentation](https://docs.webp.sh/usage/docker/) for more info, such as custom config, AVIF support etc.

View File

@ -59,7 +59,7 @@ var (
ProxyMode bool
Prefetch bool
Config jsonFile
Version = "0.9.11"
Version = "0.9.12"
WriteLock = cache.New(5*time.Minute, 10*time.Minute)
RemoteRaw = "./remote-raw"
Metadata = "./metadata"
@ -86,9 +86,9 @@ type jsonFile struct {
func init() {
flag.StringVar(&ConfigPath, "config", "config.json", "/path/to/config.json. (Default: ./config.json)")
flag.BoolVar(&Prefetch, "prefetch", false, "Prefetch and convert image to webp")
flag.BoolVar(&Prefetch, "prefetch", false, "Prefetch and convert image to WebP format.")
flag.IntVar(&Jobs, "jobs", runtime.NumCPU(), "Prefetch thread, default is all.")
flag.BoolVar(&DumpConfig, "dump-config", false, "Print sample config.json")
flag.BoolVar(&DumpConfig, "dump-config", false, "Print sample config.json.")
flag.BoolVar(&DumpSystemd, "dump-systemd", false, "Print sample systemd service file.")
flag.BoolVar(&ShowVersion, "V", false, "Show version information.")
}

View File

@ -191,3 +191,11 @@ func HashFile(filepath string) string {
buf, _ := os.ReadFile(filepath)
return fmt.Sprintf("%x", xxhash.Sum64(buf))
}
func GetEnv(key string, defaultVal ...string) string {
value := os.Getenv(key)
if value == "" && len(defaultVal) > 0 {
return defaultVal[0]
}
return value
}

View File

@ -1,10 +1,12 @@
package helper
import (
"slices"
"testing"
"webp_server_go/config"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
)
func TestMain(m *testing.M) {
@ -45,3 +47,51 @@ func TestCheckAllowedType(t *testing.T) {
assert.True(t, CheckAllowedType("test.jpg"))
})
}
func TestGuessSupportedFormat(t *testing.T) {
tests := []struct {
name string
userAgent string
accept string
expected []string
}{
{
name: "WebP/AVIF Supported",
userAgent: "iPhone OS 16",
accept: "image/webp, image/png",
expected: []string{"raw", "webp", "avif"},
},
{
name: "Both Supported",
userAgent: "iPhone OS 16",
accept: "image/webp, image/avif",
expected: []string{"raw", "webp", "avif"},
},
{
name: "No Supported Formats",
userAgent: "Unknown OS",
accept: "image/jpeg, image/gif",
expected: []string{"raw"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
header := &fasthttp.RequestHeader{}
header.Set("user-agent", test.userAgent)
header.Set("accept", test.accept)
result := GuessSupportedFormat(header)
if len(result) != len(test.expected) {
t.Errorf("Expected %v, but got %v", test.expected, result)
}
for _, format := range test.expected {
if !slices.Contains(result, format) {
t.Errorf("Expected format %s is not in the result", format)
}
}
})
}
}

View File

@ -5,9 +5,11 @@ import (
"fmt"
"os"
"runtime"
"strconv"
"webp_server_go/config"
"webp_server_go/encoder"
"webp_server_go/handler"
"webp_server_go/helper"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/etag"
@ -16,11 +18,24 @@ import (
log "github.com/sirupsen/logrus"
)
var (
ReadBufferSizeStr = helper.GetEnv("READ_BUFFER_SIZE", "4096") // Default: 4096
ReadBufferSize, _ = strconv.Atoi(ReadBufferSizeStr)
ConcurrencyStr = helper.GetEnv("CONCURRENCY", "262144") // Default: 256 * 1024
Concurrency, _ = strconv.Atoi(ConcurrencyStr)
DisableKeepaliveStr = helper.GetEnv("DISABLE_KEEPALIVE", "false") // Default: false
DisableKeepalive, _ = strconv.ParseBool(DisableKeepaliveStr)
)
// https://docs.gofiber.io/api/fiber
var app = fiber.New(fiber.Config{
ServerHeader: "WebP Server Go",
AppName: "WebP Server Go",
DisableStartupMessage: true,
ProxyHeader: "X-Real-IP",
ReadBufferSize: ReadBufferSize, // per-connection buffer size for requests' reading. This also limits the maximum header size. Increase this buffer if your clients send multi-KB RequestURIs and/or multi-KB headers (for example, BIG cookies).
Concurrency: Concurrency, // Maximum number of concurrent connections.
DisableKeepalive: DisableKeepalive, // Disable keep-alive connections, the server will close incoming connections after sending the first response to the client
})
func setupLogger() {