Skip to content

Commit 8a2f8eb

Browse files
committed
Added timing logging
1 parent 3ad3994 commit 8a2f8eb

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

jsonrpc/jsonrpc_connection.go

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"strconv"
1717
"sync"
1818
"sync/atomic"
19+
"time"
1920

2021
"go.bug.st/json"
2122
)
@@ -83,6 +84,8 @@ func (c *Connection) SetLogger(l Logger) {
8384
func (c *Connection) Run() {
8485
in := textproto.NewReader(c.in)
8586
for {
87+
start := time.Now()
88+
8689
head, err := in.ReadMIMEHeader()
8790
if err != nil {
8891
c.errorHandler(err)
@@ -107,6 +110,12 @@ func (c *Connection) Run() {
107110
} else if n != dataLen {
108111
c.errorHandler(fmt.Errorf("expected %d bytes but %d have been read", dataLen, n))
109112
}
113+
114+
elapsed := time.Since(start)
115+
c.loggerMutex.Lock()
116+
c.logger.LogIncomingDataDelay(elapsed)
117+
c.loggerMutex.Unlock()
118+
110119
c.handleIncomingData(jsonData)
111120
}
112121
}
@@ -313,6 +322,7 @@ func (c *Connection) send(data interface{}) error {
313322
return err
314323
}
315324

325+
start := time.Now()
316326
c.outMutex.Lock()
317327
defer c.outMutex.Unlock()
318328
if _, err := fmt.Fprintf(c.out, "Content-Length: %d\r\n\r\n", len(buff)); err != nil {
@@ -325,5 +335,9 @@ func (c *Connection) send(data interface{}) error {
325335
}
326336
buff = buff[n:]
327337
}
338+
elapsed := time.Since(start)
339+
c.loggerMutex.Lock()
340+
c.logger.LogOutgoingDataDelay(elapsed)
341+
c.loggerMutex.Unlock()
328342
return nil
329343
}

jsonrpc/logger.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
package jsonrpc
88

9-
import "go.bug.st/json"
9+
import (
10+
"time"
11+
12+
"go.bug.st/json"
13+
)
1014

1115
type Logger interface {
1216
LogOutgoingRequest(id string, method string, params json.RawMessage)
@@ -17,6 +21,8 @@ type Logger interface {
1721
LogIncomingNotification(method string, params json.RawMessage) FunctionLogger
1822
LogIncomingCancelRequest(id string)
1923
LogOutgoingCancelRequest(id string)
24+
LogIncomingDataDelay(time.Duration)
25+
LogOutgoingDataDelay(time.Duration)
2026
}
2127

2228
type FunctionLogger interface {
@@ -52,3 +58,7 @@ func (NullLogger) LogOutgoingCancelRequest(id string) {}
5258
type NullFunctionLogger struct{}
5359

5460
func (NullFunctionLogger) Logf(format string, a ...interface{}) {}
61+
62+
func (NullLogger) LogIncomingDataDelay(time.Duration) {}
63+
64+
func (NullLogger) LogOutgoingDataDelay(time.Duration) {}

0 commit comments

Comments
 (0)