From b19b17b7c46ad0ec5f1321224137913dafbbb687 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Thu, 14 Dec 2023 17:16:38 +0000 Subject: [PATCH] Realign the structs to decrease memory footprint. Add the timeout settings to address the connection drops. --- cache/cache.go | 6 ++--- graphql_test.go | 10 +++---- monitoring/monitoring.go | 2 +- ratelimit.go | 4 +-- server.go | 8 +++--- struct_config.go | 58 +++++++++++++++++----------------------- 6 files changed, 40 insertions(+), 48 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index b9fd8ad..519be4c 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -9,15 +9,15 @@ import ( ) type CacheEntry struct { - Value []byte ExpiresAt time.Time + Value []byte } type Cache struct { - sync.RWMutex + bytePool sync.Pool entries sync.Map globalTTL time.Duration - bytePool sync.Pool + sync.RWMutex } func New(globalTTL time.Duration) *Cache { diff --git a/graphql_test.go b/graphql_test.go index 3c10ca7..61900a2 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -11,18 +11,18 @@ import ( func (suite *Tests) Test_parseGraphQLQuery() { type results struct { - is_cached bool - cached_ttl int - should_block bool - should_ignore bool op_name string op_type string + cached_ttl int returnCode int + is_cached bool + should_block bool + should_ignore bool } type queries struct { - body string headers map[string]string + body string } tests := []struct { diff --git a/monitoring/monitoring.go b/monitoring/monitoring.go index c6570a4..fe582bf 100644 --- a/monitoring/monitoring.go +++ b/monitoring/monitoring.go @@ -15,9 +15,9 @@ import ( ) type MetricsSetup struct { - metrics_prefix string metrics_set *metrics.Set metrics_set_custom *metrics.Set + metrics_prefix string } var ( diff --git a/ratelimit.go b/ratelimit.go index c66665c..d66f9da 100644 --- a/ratelimit.go +++ b/ratelimit.go @@ -8,9 +8,9 @@ import ( ) type RateLimitConfig struct { - Req int `json:"req"` - Interval string `json:"interval"` RateCounterTicker *goratecounter.RateCounter + Interval string `json:"interval"` + Req int `json:"req"` } var rateLimits map[string]RateLimitConfig diff --git a/server.go b/server.go index c734e28..f930b43 100644 --- a/server.go +++ b/server.go @@ -21,9 +21,9 @@ func StartHTTPProxy() { server := fiber.New(fiber.Config{ DisableStartupMessage: true, AppName: fmt.Sprintf("GraphQL Monitoring Proxy - %s v%s", libpack_config.PKG_NAME, libpack_config.PKG_VERSION), - IdleTimeout: time.Second * 60, - ReadTimeout: time.Second * 60, - WriteTimeout: time.Second * 60, + IdleTimeout: time.Duration(cfg.Client.ClientTimeout) * time.Second * 2, + ReadTimeout: time.Duration(cfg.Client.ClientTimeout) * time.Second * 2, + WriteTimeout: time.Duration(cfg.Client.ClientTimeout) * time.Second * 2, }) server.Use(cors.New(cors.Config{ @@ -154,7 +154,7 @@ func processGraphQLRequest(c *fiber.Ctx) error { timeTaken := time.Since(startTime) // Logging & Monitoring - go logAndMonitorRequest(c, extractedUserID, opType, opName, wasCached, timeTaken, startTime) + logAndMonitorRequest(c, extractedUserID, opType, opName, wasCached, timeTaken, startTime) return nil } diff --git a/struct_config.go b/struct_config.go index da98ad3..bdaed9d 100644 --- a/struct_config.go +++ b/struct_config.go @@ -10,47 +10,39 @@ import ( // config is a struct that holds the configuration of the application. type config struct { + Cache struct { + CacheClient *libpack_cache.Cache + CacheTTL int + CacheEnable bool + } Logger *libpack_logging.LogConfig Monitoring *libpack_monitoring.MetricsSetup - - // Server holds the configuration of the server _ONLY_. - Server struct { - PortGraphQL int - PortMonitoring int - HostGraphQL string - HealthcheckGraphQL string - AccessLog bool - ReadOnlyMode bool - AllowURLs []string - EnableApi bool - ApiPort int - PurgeOnCrawl bool - PurgeEvery int - } - - Client struct { - JWTUserClaimPath string - JWTRoleClaimPath string - RoleRateLimit bool - RoleFromHeader string + Api struct{ BannedUsersFile string } + Client struct { GQLClient *graphql.BaseClient FastProxyClient *fasthttp.Client + JWTUserClaimPath string + JWTRoleClaimPath string + RoleFromHeader string proxy string ClientTimeout int + RoleRateLimit bool } - - Cache struct { - CacheEnable bool - CacheTTL int - CacheClient *libpack_cache.Cache - } - - Api struct { - BannedUsersFile string - } - Security struct { - BlockIntrospection bool IntrospectionAllowed []string + BlockIntrospection bool + } + Server struct { + HostGraphQL string + HealthcheckGraphQL string + AllowURLs []string + PortGraphQL int + PortMonitoring int + ApiPort int + PurgeEvery int + AccessLog bool + ReadOnlyMode bool + EnableApi bool + PurgeOnCrawl bool } }