Skip to content

Commit a517d19

Browse files
committed
Pass the client address to the handler as an additional field
Signed-off-by: Alex Bligh <[email protected]>
1 parent 29e6d7f commit a517d19

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

server.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,17 @@ func (s *Server) goScanConnection(connection net.Conn) {
160160
var scanCloser *ScanCloser
161161
scanCloser = &ScanCloser{scanner, connection}
162162

163+
remoteAddr := connection.RemoteAddr()
164+
var client string
165+
if remoteAddr != nil {
166+
client = remoteAddr.String()
167+
}
168+
163169
s.wait.Add(1)
164-
go s.scan(scanCloser)
170+
go s.scan(scanCloser, client)
165171
}
166172

167-
func (s *Server) scan(scanCloser *ScanCloser) {
173+
func (s *Server) scan(scanCloser *ScanCloser, client string) {
168174
loop:
169175
for {
170176
select {
@@ -176,7 +182,7 @@ loop:
176182
scanCloser.closer.SetReadDeadline(time.Now().Add(time.Duration(s.readTimeoutMilliseconds) * time.Millisecond))
177183
}
178184
if scanCloser.Scan() {
179-
s.parser([]byte(scanCloser.Text()))
185+
s.parser([]byte(scanCloser.Text()), client)
180186
} else {
181187
break loop
182188
}
@@ -186,14 +192,17 @@ loop:
186192
s.wait.Done()
187193
}
188194

189-
func (s *Server) parser(line []byte) {
195+
func (s *Server) parser(line []byte, client string) {
190196
parser := s.format.GetParser(line)
191197
err := parser.Parse()
192198
if err != nil {
193199
s.lastError = err
194200
}
195201

196-
go s.handler.Handle(parser.Dump(), int64(len(line)), err)
202+
logParts := parser.Dump()
203+
logParts["client"] = client
204+
205+
go s.handler.Handle(logParts, int64(len(line)), err)
197206
}
198207

199208
//Returns the last error
@@ -290,7 +299,7 @@ func (s *Server) goParseDatagrams() {
290299
if !ok {
291300
return
292301
}
293-
s.parser(msg.message)
302+
s.parser(msg.message, msg.client)
294303
}
295304
}
296305
}()

0 commit comments

Comments
 (0)