1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
package handlers
import (
"bytes"
"fmt"
"io"
"sync"
"github.com/mimecast/dtail/internal/io/line"
"github.com/mimecast/dtail/internal/io/pool"
"github.com/mimecast/dtail/internal/protocol"
)
// GrepLineProcessor processes lines for grep operations without using channels.
// It writes directly to the output writer for better performance.
type GrepLineProcessor struct {
writer io.Writer
hostname string
plain bool
serverless bool
// Buffering for efficiency
writeBuf bytes.Buffer
bufSize int
mutex sync.Mutex
// Stats
linesProcessed uint64
bytesWritten uint64
}
var _ line.Processor = (*GrepLineProcessor)(nil)
// HandlerWriter adapts a ServerHandler to implement io.Writer
type HandlerWriter struct {
handler *ServerHandler
serverMessages chan<- string
}
// Write sends data through the server messages channel
func (w *HandlerWriter) Write(p []byte) (n int, err error) {
// Convert bytes to string and send through serverMessages channel
// This will be picked up by the handler's Read() method
message := string(p)
select {
case w.serverMessages <- message:
return len(p), nil
default:
return 0, fmt.Errorf("server messages channel full")
}
}
// NewGrepLineProcessor creates a new processor for grep operations.
func NewGrepLineProcessor(writer io.Writer, hostname string, plain, serverless bool) *GrepLineProcessor {
return &GrepLineProcessor{
writer: writer,
hostname: hostname,
plain: plain,
serverless: serverless,
bufSize: 64 * 1024, // 64KB buffer
}
}
// ProcessLine processes a single line and writes it to the output.
func (p *GrepLineProcessor) ProcessLine(lineContent *bytes.Buffer, lineNum uint64, sourceID string) error {
p.mutex.Lock()
defer p.mutex.Unlock()
if !p.plain && !p.serverless {
formatRemoteLine(&p.writeBuf, p.hostname, defaultTransmittedPerc, lineNum, sourceID, lineContent.Bytes())
} else {
p.writeBuf.Write(lineContent.Bytes())
p.writeBuf.WriteByte(protocol.MessageDelimiter)
}
// Recycle the line buffer
pool.RecycleBytesBuffer(lineContent)
// Update stats
p.linesProcessed++
p.bytesWritten += uint64(p.writeBuf.Len())
// Flush if buffer is getting full
if p.writeBuf.Len() >= p.bufSize {
return p.flushBuffer()
}
return nil
}
// Flush writes any buffered data to the output.
func (p *GrepLineProcessor) Flush() error {
p.mutex.Lock()
defer p.mutex.Unlock()
return p.flushBuffer()
}
// flushBuffer writes the buffer content to the writer (must be called with mutex held).
func (p *GrepLineProcessor) flushBuffer() error {
if p.writeBuf.Len() == 0 {
return nil
}
_, err := p.writer.Write(p.writeBuf.Bytes())
p.writeBuf.Reset()
return err
}
// Close cleans up the processor.
func (p *GrepLineProcessor) Close() error {
// Flush any remaining data
return p.Flush()
}
// Stats returns processing statistics.
func (p *GrepLineProcessor) Stats() (linesProcessed, bytesWritten uint64) {
p.mutex.Lock()
defer p.mutex.Unlock()
return p.linesProcessed, p.bytesWritten
}
// ServerMessageProcessor handles server messages separately from line data.
type ServerMessageProcessor struct {
writer io.Writer
hostname string
plain bool
serverless bool
mutex sync.Mutex
}
// NewServerMessageProcessor creates a processor for server messages.
func NewServerMessageProcessor(writer io.Writer, hostname string, plain, serverless bool) *ServerMessageProcessor {
return &ServerMessageProcessor{
writer: writer,
hostname: hostname,
plain: plain,
serverless: serverless,
}
}
// SendMessage sends a server message.
func (p *ServerMessageProcessor) SendMessage(message string) error {
if p.serverless {
return nil
}
p.mutex.Lock()
defer p.mutex.Unlock()
var buf bytes.Buffer
// Skip empty server messages when in plain mode
if p.plain && (message == "" || message == "\n") {
return nil
}
// Handle hidden messages
if len(message) > 0 && message[0] == '.' {
buf.WriteString(message)
buf.WriteByte(protocol.MessageDelimiter)
_, err := p.writer.Write(buf.Bytes())
return err
}
formatServerMessage(&buf, p.hostname, message, p.plain)
_, err := p.writer.Write(buf.Bytes())
return err
}
|