Stats callbacks

This commit is contained in:
9seconds
2018-07-09 11:44:42 +03:00
parent 045d417ece
commit 1aa8cfe036
3 changed files with 14 additions and 4 deletions
+5
View File
@@ -12,6 +12,7 @@ import (
"github.com/9seconds/mtg/client"
"github.com/9seconds/mtg/config"
"github.com/9seconds/mtg/mtproto"
"github.com/9seconds/mtg/stats"
"github.com/9seconds/mtg/telegram"
"github.com/9seconds/mtg/wrappers"
)
@@ -45,6 +46,7 @@ func (p *Proxy) accept(conn net.Conn) {
conn.Close()
if err := recover(); err != nil {
stats.NewCrash()
log.Errorw("Crash of accept handler", "error", err)
}
}()
@@ -58,6 +60,9 @@ func (p *Proxy) accept(conn net.Conn) {
}
defer client.(io.Closer).Close()
stats.ClientConnected(opts.ConnectionType, client.RemoteAddr())
defer stats.ClientDisconnected(opts.ConnectionType, client.RemoteAddr())
server, err := p.getTelegramConn(opts, connID)
if err != nil {
log.Errorw("Cannot initialize server connection", "error", err)
+1 -1
View File
@@ -45,7 +45,7 @@ func connectionManager() {
for event := range ConnectionsChan {
instance.mutex.RLock()
isIPv4 := event.addr.IP.To4() == nil
isIPv4 := event.addr.IP.To4() != nil
var inc uint32 = 1
if !event.connected {
inc = ^uint32(0)
+8 -3
View File
@@ -5,6 +5,8 @@ import (
"time"
"go.uber.org/zap"
"github.com/9seconds/mtg/stats"
)
type ConnPurpose uint8
@@ -31,9 +33,10 @@ const (
)
type Conn struct {
connID string
conn net.Conn
logger *zap.SugaredLogger
connID string
conn net.Conn
logger *zap.SugaredLogger
publicIPv4 net.IP
publicIPv6 net.IP
}
@@ -43,6 +46,7 @@ func (c *Conn) Write(p []byte) (int, error) {
n, err := c.conn.Write(p)
c.logger.Debugw("Write to stream", "bytes", n, "error", err)
stats.EgressTraffic(n)
return n, err
}
@@ -52,6 +56,7 @@ func (c *Conn) Read(p []byte) (int, error) {
n, err := c.conn.Read(p)
c.logger.Debugw("Read from stream", "bytes", n, "error", err)
stats.IngressTraffic(n)
return n, err
}