3
0
mirror of https://github.com/ergochat/ergo.git synced 2025-02-16 13:40:48 +01:00

collect statistics on bytes read and written to sockets

Fixes #1296
This commit is contained in:
Shivaram Lingamneni 2021-08-25 23:06:18 -04:00
parent 8b2f6de3e0
commit baab8bf6c8
3 changed files with 39 additions and 15 deletions

View File

@ -55,15 +55,17 @@ func (client *Client) Sessions() (sessions []*Session) {
} }
type SessionData struct { type SessionData struct {
ctime time.Time ctime time.Time
atime time.Time atime time.Time
ip net.IP ip net.IP
hostname string hostname string
certfp string certfp string
deviceID string deviceID string
connInfo string connInfo string
sessionID int64 sessionID int64
caps []string caps []string
bytesRead uint64
bytesWritten uint64
} }
func (client *Client) AllSessionData(currentSession *Session, hasPrivs bool) (data []SessionData, currentIndex int) { 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
if session == currentSession { if session == currentSession {
currentIndex = i currentIndex = i
} }
bytesRead, bytesWritten := session.socket.Stats()
data[i] = SessionData{ data[i] = SessionData{
atime: session.lastActive, atime: session.lastActive,
ctime: session.ctime, ctime: session.ctime,
hostname: session.rawHostname, hostname: session.rawHostname,
certfp: session.certfp, certfp: session.certfp,
deviceID: session.deviceID, deviceID: session.deviceID,
sessionID: session.sessionID, sessionID: session.sessionID,
bytesRead: bytesRead,
bytesWritten: bytesWritten,
} }
if session.proxiedIP != nil { if session.proxiedIP != nil {
data[i].ip = session.proxiedIP data[i].ip = session.proxiedIP

View File

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

View File

@ -35,6 +35,9 @@ type Socket struct {
sendQExceeded bool sendQExceeded bool
finalData []byte // what to send when we die finalData []byte // what to send when we die
finalized bool finalized bool
bytesRead uint64
bytesWritten uint64
} }
// NewSocket returns a new Socket. // NewSocket returns a new Socket.
@ -56,6 +59,12 @@ func (socket *Socket) Close() {
socket.wakeWriter() socket.wakeWriter()
} }
func (socket *Socket) Stats() (bytesRead, bytesWritten uint64) {
socket.Lock()
defer socket.Unlock()
return socket.bytesRead, socket.bytesWritten
}
// Read returns a single IRC line from a Socket. // Read returns a single IRC line from a Socket.
func (socket *Socket) Read() (string, error) { func (socket *Socket) Read() (string, error) {
// immediately fail if Close() has been called, even if there's // immediately fail if Close() has been called, even if there's
@ -67,6 +76,10 @@ func (socket *Socket) Read() (string, error) {
lineBytes, err := socket.conn.ReadLine() lineBytes, err := socket.conn.ReadLine()
line := string(lineBytes) line := string(lineBytes)
socket.Lock()
socket.bytesRead += uint64(len(lineBytes))
socket.Unlock()
if err == io.EOF { if err == io.EOF {
socket.Close() socket.Close()
} }
@ -96,6 +109,7 @@ func (socket *Socket) Write(data []byte) (err error) {
} else { } else {
socket.buffers = append(socket.buffers, data) socket.buffers = append(socket.buffers, data)
socket.totalLength = prospectiveLen socket.totalLength = prospectiveLen
socket.bytesWritten += uint64(len(data))
} }
} }
socket.Unlock() socket.Unlock()
@ -136,6 +150,10 @@ func (socket *Socket) BlockingWrite(data []byte) (err error) {
return io.EOF return io.EOF
} }
socket.Lock()
socket.bytesWritten += uint64(len(data))
socket.Unlock()
err = socket.conn.WriteLine(data) err = socket.conn.WriteLine(data)
if err != nil { if err != nil {
socket.finalize() socket.finalize()