Skip to content
This repository has been archived by the owner on May 6, 2023. It is now read-only.

Commit

Permalink
🆕 增加统计当前在线客户端数量
Browse files Browse the repository at this point in the history
  • Loading branch information
perrornet committed Dec 12, 2021
1 parent b69b31c commit 1733f56
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ type Proxy struct {
}

var (
totalSize uint64
totalSize uint64
onlineCount int64
)

// New - Create a new Proxy instance. Takes over local connection passed in,
Expand All @@ -59,27 +60,29 @@ var (
startTime = time.Now()
)

func (p Proxy) TimerPrint() {
once.Do(func() {
func init() {
go once.Do(func() {
t := time.Now()
for range time.Tick(time.Second * 30) {
total := atomic.LoadUint64(&totalSize)

log.Printf("从 %s 至现在总计加密转发 %s 数据; 平均转发速度 %s/秒 \n",
log.Printf("从 %s 至现在总计加密转发 %s 数据; 平均转发速度 %s/秒; 当前在线 %d 个客户端 \n",
t.Format("2006-01-02 15:04:05"),
humanize.Bytes(total),
humanize.Bytes(uint64(float64(total)/time.Since(startTime).Seconds())),
atomic.LoadInt64(&onlineCount),
)
}
})

}

// Start - open connection to remote and start proxying data.
func (p *Proxy) Start() {
defer pkg.Recover(true)
defer p.lconn.Close()
go p.TimerPrint()
defer func() {
atomic.AddInt64(&onlineCount, -1)
}()
atomic.AddInt64(&onlineCount, 1)
conn, err := p.connRemote()
if err != nil {
p.err("Remote connection failed: %s", err)
Expand Down

0 comments on commit 1733f56

Please sign in to comment.