mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-10-19 06:27:38 +00:00
improvements mid may 2025 (#24)
* General improvements and bug fixes. * Improve tests coverage. * fixup! Improve tests coverage. * Update README.md with latest changes. * Fix the uint32 * Resolve issue with race condition for logging. * fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * Fix the test of the rate limiter * Add default ratelimit.json file * Update dependencies. * Significant refactor. * fixup! Significant refactor. * fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025 * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Merge remote-tracking branch 'origin/main' into improvements-mid-apr-2025
This commit is contained in:
+7
-1
@@ -42,6 +42,7 @@ type Logger struct {
|
||||
timeFormat string
|
||||
minLogLevel int
|
||||
showCaller bool
|
||||
mu sync.Mutex // Mutex to protect concurrent access to output
|
||||
}
|
||||
|
||||
// LogMessage represents a log message with optional pairs.
|
||||
@@ -82,7 +83,9 @@ func New() *Logger {
|
||||
|
||||
// SetOutput sets the output destination for the logger.
|
||||
func (l *Logger) SetOutput(output io.Writer) *Logger {
|
||||
l.mu.Lock()
|
||||
l.output = output
|
||||
l.mu.Unlock()
|
||||
return l
|
||||
}
|
||||
|
||||
@@ -150,8 +153,11 @@ func (l *Logger) log(level int, m *LogMessage) {
|
||||
fmt.Fprintln(os.Stderr, "Error marshalling log message:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Lock the mutex before writing to the output to prevent race conditions
|
||||
l.mu.Lock()
|
||||
_, err = l.output.Write(buffer.Bytes())
|
||||
l.mu.Unlock()
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Error writing log message:", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package libpack_logger
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Test_LogConcurrentAccess verifies that the logger correctly handles concurrent access
|
||||
// without race conditions
|
||||
func TestLogConcurrentAccess(t *testing.T) {
|
||||
output := &bytes.Buffer{}
|
||||
logger := New().SetOutput(output).SetMinLogLevel(LEVEL_DEBUG)
|
||||
|
||||
// Number of concurrent goroutines
|
||||
numGoroutines := 100
|
||||
// Wait group to synchronize goroutines
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(numGoroutines)
|
||||
|
||||
// Launch multiple goroutines to log concurrently
|
||||
for i := 0; i < numGoroutines; i++ {
|
||||
go func(id int) {
|
||||
defer wg.Done()
|
||||
msg := &LogMessage{
|
||||
Message: "concurrent log test",
|
||||
Pairs: map[string]interface{}{
|
||||
"goroutine_id": id,
|
||||
},
|
||||
}
|
||||
// Use different log levels to test all paths
|
||||
switch id % 5 {
|
||||
case 0:
|
||||
logger.Debug(msg)
|
||||
case 1:
|
||||
logger.Info(msg)
|
||||
case 2:
|
||||
logger.Warn(msg)
|
||||
case 3:
|
||||
logger.Error(msg)
|
||||
case 4:
|
||||
logger.Fatal(msg)
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
|
||||
// Wait for all goroutines to complete
|
||||
wg.Wait()
|
||||
|
||||
// If we make it here without a race detector failure, the test passes
|
||||
if output.Len() == 0 {
|
||||
t.Error("Expected log output, but got none")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user