Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): enhance error handling and thread safety in log management #1013

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,21 @@ func logHandler(w http.ResponseWriter, r *http.Request) {
case "GET":
// Send current state of the log file immediately
w.Header().Set("Content-Type", "application/jsonlines")
_, _ = app.MemoryLog.WriteTo(w)
if _, err := app.MemoryLog.WriteTo(w); err != nil {
log.Printf("Error writing memory log: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
case "DELETE":
app.MemoryLog.Reset()
if err := app.MemoryLog.Reset(); err != nil {
log.Printf("Error resetting memory log: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
Response(w, "OK", "text/plain")
default:
http.Error(w, "Method not allowed", http.StatusBadRequest)
w.Header().Set("Allow", "GET, DELETE")
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
}

Expand Down
58 changes: 50 additions & 8 deletions internal/app/log.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package app

import (
"errors"
"io"
"os"
"sync"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -53,6 +55,7 @@ const chunkSize = 1 << 16
type circularBuffer struct {
chunks [][]byte
r, w int
mu sync.Mutex
}

func newBuffer(chunks int) *circularBuffer {
Expand All @@ -63,20 +66,28 @@ func newBuffer(chunks int) *circularBuffer {
}

func (b *circularBuffer) Write(p []byte) (n int, err error) {
b.mu.Lock()
defer b.mu.Unlock()

n = len(p)
if n == 0 {
return 0, nil
}

if len(b.chunks) == 0 {
b.chunks = append(b.chunks, make([]byte, 0, chunkSize))
}

// check if chunk has size
if len(b.chunks[b.w])+n > chunkSize {
// increase write chunk index
if b.w++; b.w == cap(b.chunks) {
b.w++
if b.w == cap(b.chunks) {
b.w = 0
}
// check overflow
if b.r == b.w {
// increase read chunk index
if b.r++; b.r == cap(b.chunks) {
b.r = 0
}
return 0, errors.New("circularBuffer overflow, cannot write without overwriting unread data")
}
// check if current chunk exists
if b.w == len(b.chunks) {
Expand All @@ -89,7 +100,7 @@ func (b *circularBuffer) Write(p []byte) (n int, err error) {
}

b.chunks[b.w] = append(b.chunks[b.w], p...)
return
return n, nil
}

func (b *circularBuffer) WriteTo(w io.Writer) (n int64, err error) {
Expand All @@ -110,8 +121,39 @@ func (b *circularBuffer) WriteTo(w io.Writer) (n int64, err error) {
return
}

func (b *circularBuffer) Reset() {
b.chunks[0] = b.chunks[0][:0]
func (b *circularBuffer) Reset() error {
b.mu.Lock()
defer b.mu.Unlock()

for i := range b.chunks {
b.chunks[i] = b.chunks[i][:0]
}

b.r = 0
b.w = 0

return nil
}

// Bytes concatenates all chunks into a single byte slice.
func (b *circularBuffer) Bytes() []byte {
var totalLen int
for _, chunk := range b.chunks {
totalLen += len(chunk)
}
result := make([]byte, 0, totalLen)

for i := b.r; ; {
result = append(result, b.chunks[i]...)

if i == b.w {
break
}

i++
if i == len(b.chunks) {
i = 0
}
}
return result
}
147 changes: 147 additions & 0 deletions internal/app/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package app

import (
"reflect"
"testing"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

func TestCircularBuffer(t *testing.T) {
buf := newBuffer(2) // Small buffer for testing

// Test writing and wrapping
msg1 := []byte("hello")
msg2 := []byte("world")
_, err := buf.Write(msg1)
if err != nil {
t.Errorf("Write failed: %v", err)
}
_, err = buf.Write(msg2)
if err != nil {
t.Errorf("Write failed: %v", err)
}

// Test buffer content
expected := "helloworld"
if string(buf.Bytes()) != expected {
t.Errorf("Expected %s, got %s", expected, string(buf.Bytes()))
}

// Test reset
buf.Reset()
if len(buf.Bytes()) != 0 {
t.Errorf("Expected empty buffer after reset, got %d bytes", len(buf.Bytes()))
}
}

func TestNewLogger(t *testing.T) {
tests := []struct {
format string
level string
}{
{"json", "info"},
{"text", "debug"},
}

for _, tc := range tests {
logger := NewLogger(tc.format, tc.level)

// Check if logger has the correct level
lvl := logger.GetLevel()
expectedLvl, _ := zerolog.ParseLevel(tc.level)
if lvl != expectedLvl {
t.Errorf("Expected level %s, got %s", tc.level, lvl.String())
}

// Additional checks can be added here for format verification
}
}

func TestGetLogger(t *testing.T) {
modules = map[string]string{
"module1": "debug",
"module2": "warn",
}

logger1 := GetLogger("module1")
if logger1.GetLevel() != zerolog.DebugLevel {
t.Errorf("Expected debug level for module1, got %s", logger1.GetLevel().String())
}

logger2 := GetLogger("module2")
if logger2.GetLevel() != zerolog.WarnLevel {
t.Errorf("Expected warn level for module2, got %s", logger2.GetLevel().String())
}

// Test non-existent module (should default to global logger level)
logger3 := GetLogger("nonexistent")
if logger3.GetLevel() != log.Logger.GetLevel() {
t.Errorf("Expected default logger level for nonexistent module, got %s", logger3.GetLevel().String())
}
}

func TestCircularBuffer_Bytes(t *testing.T) {
tests := []struct {
name string
buffer *circularBuffer
expected []byte
}{
{
name: "empty buffer",
buffer: &circularBuffer{
chunks: make([]([]byte), 5),
r: 0,
w: 0,
},
expected: []byte{},
},
{
name: "partially filled buffer",
buffer: &circularBuffer{
chunks: [][]byte{{'a', 'b'}, {'c', 'd'}, {}, {}, {}},
r: 0,
w: 2,
},
expected: []byte{'a', 'b', 'c', 'd'},
},
{
name: "wrapped around buffer",
buffer: &circularBuffer{
chunks: [][]byte{{'e', 'f'}, {'g', 'h'}, {'a', 'b'}, {'c', 'd'}, {}},
r: 2,
w: 1,
},
expected: []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.buffer.Bytes(); !reflect.DeepEqual(got, tt.expected) {
t.Errorf("circularBuffer.Bytes() = %v, want %v", got, tt.expected)
}
})
}
}

// BenchmarkCircularBuffer_Bytes benchmarks the Bytes method of circularBuffer.
func BenchmarkCircularBuffer_Bytes(b *testing.B) {
buffer := &circularBuffer{
chunks: make([]([]byte), 1024), // Assuming a buffer capacity of 1024 chunks
r: 0,
w: 512, // Assuming the buffer is half full for this benchmark
}

// Pre-fill the buffer with data to simulate a real-world scenario.
for i := range buffer.chunks[:buffer.w] {
buffer.chunks[i] = []byte("some repeated sample data")
}

// The actual benchmark loop
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = buffer.Bytes()
}
}