add stats support for tunnel handler

This commit is contained in:
ginuerzh
2024-08-28 13:34:05 +08:00
parent bc0d6953bc
commit ff20711c25
7 changed files with 120 additions and 36 deletions
+27 -5
View File
@@ -4,6 +4,7 @@ import (
"errors"
"io"
"net"
"sync"
"syscall"
"github.com/go-gost/core/metadata"
@@ -18,17 +19,23 @@ var (
type conn struct {
net.Conn
stats *stats.Stats
stats *stats.Stats
closed chan struct{}
mu sync.Mutex
}
func WrapConn(c net.Conn, stats *stats.Stats) net.Conn {
if stats == nil {
func WrapConn(c net.Conn, pStats *stats.Stats) net.Conn {
if pStats == nil {
return c
}
pStats.Add(stats.KindTotalConns, 1)
pStats.Add(stats.KindCurrentConns, 1)
return &conn{
Conn: c,
stats: stats,
Conn: c,
stats: pStats,
closed: make(chan struct{}),
}
}
@@ -44,6 +51,21 @@ func (c *conn) Write(b []byte) (n int, err error) {
return
}
func (c *conn) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
select {
case <-c.closed:
return nil
default:
close(c.closed)
}
c.stats.Add(stats.KindCurrentConns, -1)
return c.Conn.Close()
}
func (c *conn) SyscallConn() (rc syscall.RawConn, err error) {
if sc, ok := c.Conn.(syscall.Conn); ok {
rc, err = sc.SyscallConn()