From a4f7e1690dc711e764b2afbbbe4f2a31afb47f7a Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Thu, 20 Jun 2024 08:40:55 +0100 Subject: [PATCH] Aligning struct fields for better memory management. --- cache/cache.go | 2 +- cache/redis/redis.go | 6 +++--- logging/logger.go | 6 +++--- logging/logger_test.go | 6 +++--- monitoring/helpers.go | 2 +- monitoring/helpers_test.go | 2 +- struct_config.go | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index 377e650..443bbf6 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -15,10 +15,10 @@ type CacheConfig struct { Logger *libpack_logger.Logger Client CacheClient Redis struct { - Enable bool `json:"enable"` URL string `json:"url"` Password string `json:"password"` DB int `json:"db"` + Enable bool `json:"enable"` } TTL int `json:"ttl"` } diff --git a/cache/redis/redis.go b/cache/redis/redis.go index 40f9d66..01edcbb 100644 --- a/cache/redis/redis.go +++ b/cache/redis/redis.go @@ -11,10 +11,10 @@ import ( ) type RedisConfig struct { - client *redis.Client ctx context.Context - prefix string + client *redis.Client builderPool *sync.Pool + prefix string } func (c *RedisConfig) prependKeyName(key string) string { @@ -29,8 +29,8 @@ func (c *RedisConfig) prependKeyName(key string) string { type RedisClientConfig struct { RedisServer string RedisPassword string - RedisDB int Prefix string + RedisDB int } func New(redisClientConfig *RedisClientConfig) *RedisConfig { diff --git a/logging/logger.go b/logging/logger.go index 33a2ceb..7b1a964 100644 --- a/logging/logger.go +++ b/logging/logger.go @@ -42,16 +42,16 @@ const ( var defaultOutput = os.Stdout type Logger struct { + output io.Writer format string minLogLevel int showCaller bool - output io.Writer } type LogMessage struct { - Message string - Pairs map[string]any output io.Writer + Pairs map[string]any + Message string } func (m *LogMessage) String() string { diff --git a/logging/logger_test.go b/logging/logger_test.go index f029159..92d04a9 100644 --- a/logging/logger_test.go +++ b/logging/logger_test.go @@ -59,13 +59,13 @@ func (suite *LoggerTestSuite) Test_LogsLevelsPrint() { logger := New().SetOutput(output) tests := []struct { + pairs map[string]any name string method string + message string loggerMinLevel int messageLogLevel int - message string - pairs map[string]any - wantOutput bool // Whether we expect output to be written + wantOutput bool }{ { name: "Log: Debug, Level: Debug - no pairs", diff --git a/monitoring/helpers.go b/monitoring/helpers.go index cc9aa10..7412b91 100644 --- a/monitoring/helpers.go +++ b/monitoring/helpers.go @@ -14,8 +14,8 @@ import ( // Cache for sorted label keys to avoid repeated sorting var sortedLabelKeysCache = struct { - sync.RWMutex m map[string][]string + sync.RWMutex }{m: make(map[string][]string)} func (ms *MetricsSetup) get_metrics_name(name string, labels map[string]string) string { diff --git a/monitoring/helpers_test.go b/monitoring/helpers_test.go index c937362..f3f0981 100644 --- a/monitoring/helpers_test.go +++ b/monitoring/helpers_test.go @@ -167,9 +167,9 @@ func TestEnsureDefaultLabels(t *testing.T) { libpack_config.PKG_NAME = "example_microservice" tests := []struct { - name string inputLabels map[string]string expectedLabels map[string]string + name string }{ { name: "Nil labels", diff --git a/struct_config.go b/struct_config.go index 6d7f756..534e9c9 100644 --- a/struct_config.go +++ b/struct_config.go @@ -11,8 +11,8 @@ import ( type config struct { Trace struct { Client func() - Enable bool TraceEndpoint string + Enable bool } Logger *libpack_logging.Logger LogLevel string