Parcourir la source

collect statistics on bytes read and written to sockets

Fixes #1296
pull/1785/head
Shivaram Lingamneni il y a 2 ans
Parent
révision
baab8bf6c8
3 fichiers modifiés avec 39 ajouts et 15 suppressions
  1. 20
    15
      irc/getters.go
  2. 1
    0
      irc/nickserv.go
  3. 18
    0
      irc/socket.go

+ 20
- 15
irc/getters.go Voir le fichier

55
 }
55
 }
56
 
56
 
57
 type SessionData struct {
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
 func (client *Client) AllSessionData(currentSession *Session, hasPrivs bool) (data []SessionData, currentIndex int) {
71
 func (client *Client) AllSessionData(currentSession *Session, hasPrivs bool) (data []SessionData, currentIndex int) {
76
 		if session == currentSession {
78
 		if session == currentSession {
77
 			currentIndex = i
79
 			currentIndex = i
78
 		}
80
 		}
81
+		bytesRead, bytesWritten := session.socket.Stats()
79
 		data[i] = SessionData{
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
 		if session.proxiedIP != nil {
92
 		if session.proxiedIP != nil {
88
 			data[i].ip = session.proxiedIP
93
 			data[i].ip = session.proxiedIP

+ 1
- 0
irc/nickserv.go Voir le fichier

1275
 				service.Notice(rb, fmt.Sprintf(client.t("IRCv3 CAPs:  %s"), capStr))
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 Voir le fichier

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

Chargement…
Annuler
Enregistrer