Browse Source

collect statistics on bytes read and written to sockets

Fixes #1296
pull/1785/head
Shivaram Lingamneni 2 years ago
parent
commit
baab8bf6c8
3 changed files with 39 additions and 15 deletions
  1. 20
    15
      irc/getters.go
  2. 1
    0
      irc/nickserv.go
  3. 18
    0
      irc/socket.go

+ 20
- 15
irc/getters.go View File

@@ -55,15 +55,17 @@ func (client *Client) Sessions() (sessions []*Session) {
55 55
 }
56 56
 
57 57
 type SessionData struct {
58
-	ctime     time.Time
59
-	atime     time.Time
60
-	ip        net.IP
61
-	hostname  string
62
-	certfp    string
63
-	deviceID  string
64
-	connInfo  string
65
-	sessionID int64
66
-	caps      []string
58
+	ctime        time.Time
59
+	atime        time.Time
60
+	ip           net.IP
61
+	hostname     string
62
+	certfp       string
63
+	deviceID     string
64
+	connInfo     string
65
+	sessionID    int64
66
+	caps         []string
67
+	bytesRead    uint64
68
+	bytesWritten uint64
67 69
 }
68 70
 
69 71
 func (client *Client) AllSessionData(currentSession *Session, hasPrivs bool) (data []SessionData, currentIndex int) {
@@ -76,13 +78,16 @@ func (client *Client) AllSessionData(currentSession *Session, hasPrivs bool) (da
76 78
 		if session == currentSession {
77 79
 			currentIndex = i
78 80
 		}
81
+		bytesRead, bytesWritten := session.socket.Stats()
79 82
 		data[i] = SessionData{
80
-			atime:     session.lastActive,
81
-			ctime:     session.ctime,
82
-			hostname:  session.rawHostname,
83
-			certfp:    session.certfp,
84
-			deviceID:  session.deviceID,
85
-			sessionID: session.sessionID,
83
+			atime:        session.lastActive,
84
+			ctime:        session.ctime,
85
+			hostname:     session.rawHostname,
86
+			certfp:       session.certfp,
87
+			deviceID:     session.deviceID,
88
+			sessionID:    session.sessionID,
89
+			bytesRead:    bytesRead,
90
+			bytesWritten: bytesWritten,
86 91
 		}
87 92
 		if session.proxiedIP != nil {
88 93
 			data[i].ip = session.proxiedIP

+ 1
- 0
irc/nickserv.go View File

@@ -1275,6 +1275,7 @@ func nsClientsListHandler(service *ircService, server *Server, client *Client, p
1275 1275
 				service.Notice(rb, fmt.Sprintf(client.t("IRCv3 CAPs:  %s"), capStr))
1276 1276
 			}
1277 1277
 		}
1278
+		service.Notice(rb, fmt.Sprintf(client.t("Bytes RX/TX: %d / %d"), session.bytesRead, session.bytesWritten))
1278 1279
 	}
1279 1280
 }
1280 1281
 

+ 18
- 0
irc/socket.go View File

@@ -35,6 +35,9 @@ type Socket struct {
35 35
 	sendQExceeded bool
36 36
 	finalData     []byte // what to send when we die
37 37
 	finalized     bool
38
+
39
+	bytesRead    uint64
40
+	bytesWritten uint64
38 41
 }
39 42
 
40 43
 // NewSocket returns a new Socket.
@@ -56,6 +59,12 @@ func (socket *Socket) Close() {
56 59
 	socket.wakeWriter()
57 60
 }
58 61
 
62
+func (socket *Socket) Stats() (bytesRead, bytesWritten uint64) {
63
+	socket.Lock()
64
+	defer socket.Unlock()
65
+	return socket.bytesRead, socket.bytesWritten
66
+}
67
+
59 68
 // Read returns a single IRC line from a Socket.
60 69
 func (socket *Socket) Read() (string, error) {
61 70
 	// immediately fail if Close() has been called, even if there's
@@ -67,6 +76,10 @@ func (socket *Socket) Read() (string, error) {
67 76
 	lineBytes, err := socket.conn.ReadLine()
68 77
 	line := string(lineBytes)
69 78
 
79
+	socket.Lock()
80
+	socket.bytesRead += uint64(len(lineBytes))
81
+	socket.Unlock()
82
+
70 83
 	if err == io.EOF {
71 84
 		socket.Close()
72 85
 	}
@@ -96,6 +109,7 @@ func (socket *Socket) Write(data []byte) (err error) {
96 109
 		} else {
97 110
 			socket.buffers = append(socket.buffers, data)
98 111
 			socket.totalLength = prospectiveLen
112
+			socket.bytesWritten += uint64(len(data))
99 113
 		}
100 114
 	}
101 115
 	socket.Unlock()
@@ -136,6 +150,10 @@ func (socket *Socket) BlockingWrite(data []byte) (err error) {
136 150
 		return io.EOF
137 151
 	}
138 152
 
153
+	socket.Lock()
154
+	socket.bytesWritten += uint64(len(data))
155
+	socket.Unlock()
156
+
139 157
 	err = socket.conn.WriteLine(data)
140 158
 	if err != nil {
141 159
 		socket.finalize()

Loading…
Cancel
Save