v0.18.1 file watcher retirement diagnostics update
This commit is contained in:
+20
-14
@@ -452,14 +452,17 @@ func (h *HTTPSink) handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
"version": version.Short(),
|
||||
"instance_id": h.id,
|
||||
"server": map[string]any{
|
||||
"type": "http",
|
||||
"host": h.config.Host,
|
||||
"port": h.config.Port,
|
||||
"tls": h.tlsConfig != nil,
|
||||
"auth": h.auth.Describe(),
|
||||
"active_clients": h.activeClients.Load(),
|
||||
"buffer_size": h.config.BufferSize,
|
||||
"uptime_seconds": int(time.Since(h.startTime).Seconds()),
|
||||
"type": "http",
|
||||
"host": h.config.Host,
|
||||
"port": h.config.Port,
|
||||
"tls": h.tlsConfig != nil,
|
||||
"auth": h.auth.Describe(),
|
||||
"active_clients": h.activeClients.Load(),
|
||||
"buffer_size": h.config.BufferSize,
|
||||
"client_buffer_size": h.config.ClientBufferSize,
|
||||
"max_connections": h.config.MaxConnections,
|
||||
"write_timeout_ms": h.config.WriteTimeoutMS,
|
||||
"uptime_seconds": int(time.Since(h.startTime).Seconds()),
|
||||
},
|
||||
"endpoints": map[string]string{
|
||||
"stream": h.config.StreamPath,
|
||||
@@ -481,12 +484,15 @@ func (h *HTTPSink) handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *HTTPSink) GetStats() sink.SinkStats {
|
||||
lastProc, _ := h.lastProcessed.Load().(time.Time)
|
||||
details := map[string]any{
|
||||
"host": h.config.Host,
|
||||
"port": h.config.Port,
|
||||
"buffer_size": h.config.BufferSize,
|
||||
"tls": h.tlsConfig != nil,
|
||||
"dropped_writes": h.droppedWrites.Load(),
|
||||
"rejected_clients": h.rejectedClients.Load(),
|
||||
"host": h.config.Host,
|
||||
"port": h.config.Port,
|
||||
"buffer_size": h.config.BufferSize,
|
||||
"client_buffer_size": h.config.ClientBufferSize,
|
||||
"max_connections": h.config.MaxConnections,
|
||||
"write_timeout_ms": h.config.WriteTimeoutMS,
|
||||
"tls": h.tlsConfig != nil,
|
||||
"dropped_writes": h.droppedWrites.Load(),
|
||||
"rejected_clients": h.rejectedClients.Load(),
|
||||
"endpoints": map[string]string{
|
||||
"stream": h.config.StreamPath,
|
||||
"status": h.config.StatusPath,
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"logwisp/internal/session"
|
||||
"logwisp/internal/sink"
|
||||
|
||||
"github.com/lixenwraith/log"
|
||||
)
|
||||
|
||||
func TestStatusReportsQueueAndConnectionBounds(t *testing.T) {
|
||||
manager := session.NewManager(time.Hour)
|
||||
defer manager.Stop()
|
||||
created, err := NewHTTPSinkPlugin(
|
||||
"stream",
|
||||
map[string]any{
|
||||
"host": "127.0.0.1",
|
||||
"port": int64(8081),
|
||||
"buffer_size": int64(4096),
|
||||
"client_buffer_size": int64(512),
|
||||
"max_connections": int64(32),
|
||||
"write_timeout_ms": int64(5000),
|
||||
},
|
||||
log.NewLogger(),
|
||||
session.NewProxy(manager, "stream"),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
httpSink, ok := created.(*HTTPSink)
|
||||
if !ok {
|
||||
t.Fatalf("sink type = %T", created)
|
||||
}
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
httpSink.handleStatus(recorder, httptest.NewRequest("GET", "/status", nil))
|
||||
if recorder.Code != 200 {
|
||||
t.Fatalf("status code = %d", recorder.Code)
|
||||
}
|
||||
var response struct {
|
||||
Server map[string]any `json:"server"`
|
||||
}
|
||||
if err := json.NewDecoder(recorder.Body).Decode(&response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for key, want := range map[string]float64{
|
||||
"buffer_size": 4096,
|
||||
"client_buffer_size": 512,
|
||||
"max_connections": 32,
|
||||
"write_timeout_ms": 5000,
|
||||
} {
|
||||
if got := response.Server[key]; got != want {
|
||||
t.Errorf("server.%s = %v, want %v", key, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
stats := httpSink.GetStats()
|
||||
details := stats.Details
|
||||
for key, want := range map[string]int64{
|
||||
"buffer_size": 4096,
|
||||
"client_buffer_size": 512,
|
||||
"max_connections": 32,
|
||||
"write_timeout_ms": 5000,
|
||||
} {
|
||||
if got := details[key]; got != want {
|
||||
t.Errorf("details[%q] = %v, want %v", key, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
var _ sink.Sink = httpSink
|
||||
}
|
||||
@@ -292,12 +292,21 @@ func (fs *FileSource) ensureWatcher(path string) {
|
||||
}
|
||||
}
|
||||
|
||||
fs.mu.Lock()
|
||||
delete(fs.watchers, path)
|
||||
fs.mu.Unlock()
|
||||
fs.removeWatcher(path, w)
|
||||
}()
|
||||
}
|
||||
|
||||
// removeWatcher removes only the watcher that finished. A deleted file can be
|
||||
// recreated before its old watcher observes stop; in that case ensureWatcher
|
||||
// has already installed a replacement under the same path, which must survive.
|
||||
func (fs *FileSource) removeWatcher(path string, watcher *fileWatcher) {
|
||||
fs.mu.Lock()
|
||||
if fs.watchers[path] == watcher {
|
||||
delete(fs.watchers, path)
|
||||
}
|
||||
fs.mu.Unlock()
|
||||
}
|
||||
|
||||
// cleanupWatchers stops and removes watchers for files that no longer exist.
|
||||
func (fs *FileSource) cleanupWatchers() {
|
||||
fs.mu.Lock()
|
||||
@@ -369,4 +378,3 @@ func globToRegex(glob string) string {
|
||||
regex = strings.ReplaceAll(regex, `\?`, `.`)
|
||||
return "^" + regex + "$"
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"logwisp/internal/core"
|
||||
)
|
||||
|
||||
func TestStoppedWatcherReturnsNormally(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "session.jsonl")
|
||||
if err := os.WriteFile(path, nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
watcher := newFileWatcher(path, true, true, func(_ core.LogEntry) {}, nil)
|
||||
watcher.stop()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
if err := watcher.watch(ctx); err != nil {
|
||||
t.Fatalf("stopped watcher returned error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveWatcherPreservesReplacement(t *testing.T) {
|
||||
oldWatcher := &fileWatcher{}
|
||||
replacement := &fileWatcher{}
|
||||
source := &FileSource{
|
||||
watchers: map[string]*fileWatcher{
|
||||
"session.jsonl": replacement,
|
||||
},
|
||||
}
|
||||
|
||||
source.removeWatcher("session.jsonl", oldWatcher)
|
||||
if got := source.watchers["session.jsonl"]; got != replacement {
|
||||
t.Fatalf("replacement watcher = %p, want %p", got, replacement)
|
||||
}
|
||||
|
||||
source.removeWatcher("session.jsonl", replacement)
|
||||
if _, exists := source.watchers["session.jsonl"]; exists {
|
||||
t.Fatal("finished watcher was not removed")
|
||||
}
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func (w *fileWatcher) watch(ctx context.Context) error {
|
||||
return ctx.Err()
|
||||
case <-ticker.C:
|
||||
if w.isStopped() {
|
||||
return fmt.Errorf("watcher stopped")
|
||||
return nil
|
||||
}
|
||||
if err := w.checkFile(); err != nil {
|
||||
// Log error but continue watching
|
||||
|
||||
Reference in New Issue
Block a user