smaller buffer and flush

This commit is contained in:
Peter Steinberger 2025-06-20 20:02:52 +02:00
parent 37b36ab801
commit 12ce2cd834
2 changed files with 17 additions and 7 deletions

View file

@ -137,6 +137,11 @@ func (w *StreamWriter) writeEvent(eventType EventType, data []byte) error {
return err return err
} }
// Immediately flush if the writer supports it for real-time output
if flusher, ok := w.writer.(interface{ Flush() error }); ok {
flusher.Flush()
}
// Schedule sync instead of immediate sync for better performance // Schedule sync instead of immediate sync for better performance
w.scheduleBatchSync() w.scheduleBatchSync()
@ -150,8 +155,8 @@ func (w *StreamWriter) scheduleFlush() {
w.flushTimer.Stop() w.flushTimer.Stop()
} }
// Set up new timer for 1ms flush delay for better real-time performance // Set up immediate flush for real-time performance
w.flushTimer = time.AfterFunc(1*time.Millisecond, func() { w.flushTimer = time.AfterFunc(0, func() {
w.mutex.Lock() w.mutex.Lock()
defer w.mutex.Unlock() defer w.mutex.Unlock()
@ -186,6 +191,11 @@ func (w *StreamWriter) scheduleFlush() {
return return
} }
// Immediately flush if the writer supports it for real-time output
if flusher, ok := w.writer.(interface{ Flush() error }); ok {
flusher.Flush()
}
// Schedule sync instead of immediate sync for better performance // Schedule sync instead of immediate sync for better performance
w.scheduleBatchSync() w.scheduleBatchSync()
@ -203,8 +213,8 @@ func (w *StreamWriter) scheduleBatchSync() {
w.syncTimer.Stop() w.syncTimer.Stop()
} }
// Schedule sync after 1ms for better real-time performance // Schedule immediate sync for real-time performance
w.syncTimer = time.AfterFunc(1*time.Millisecond, func() { w.syncTimer = time.AfterFunc(0, func() {
if w.needsSync { if w.needsSync {
if file, ok := w.writer.(*os.File); ok { if file, ok := w.writer.(*os.File); ok {
if err := file.Sync(); err != nil { if err := file.Sync(); err != nil {

View file

@ -70,7 +70,7 @@ func fdIsSet(set *syscall.FdSet, fd int) bool {
// pollWithSelect polls multiple file descriptors using select // pollWithSelect polls multiple file descriptors using select
func (p *PTY) pollWithSelect() error { func (p *PTY) pollWithSelect() error {
// Buffer for reading // Buffer for reading
buf := make([]byte, 32*1024) buf := make([]byte, 4*1024) // 4KB buffer for more responsive output
// Get file descriptors // Get file descriptors
ptyFd := int(p.pty.Fd()) ptyFd := int(p.pty.Fd())
@ -106,8 +106,8 @@ func (p *PTY) pollWithSelect() error {
fds = append(fds, controlFd) fds = append(fds, controlFd)
} }
// Wait for activity with 100ms timeout for better responsiveness // Wait for activity with 10ms timeout for real-time responsiveness
ready, err := selectRead(fds, 100*time.Millisecond) ready, err := selectRead(fds, 10*time.Millisecond)
if err != nil { if err != nil {
log.Printf("[ERROR] select error: %v", err) log.Printf("[ERROR] select error: %v", err)
return err return err