加入了Tunnel统计功能。
This commit is contained in:
70
stat/tunnelstatus.go
Normal file
70
stat/tunnelstatus.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package stat
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"github.com/TransX/log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type TunnelStatusManager struct {
|
||||
mux sync.Mutex
|
||||
tunnelList list.List
|
||||
regChan chan interface{}
|
||||
unregChan chan interface{}
|
||||
}
|
||||
|
||||
func NewTunnelStatusManager() *TunnelStatusManager {
|
||||
t := new(TunnelStatusManager)
|
||||
t.regChan = make(chan interface{})
|
||||
t.unregChan = make(chan interface{})
|
||||
go t.chanListener()
|
||||
return t
|
||||
}
|
||||
|
||||
func (this *TunnelStatusManager) GetRegChan() chan interface{} {
|
||||
return this.regChan
|
||||
}
|
||||
|
||||
func (this *TunnelStatusManager) GetUnregChan() chan interface{} {
|
||||
return this.unregChan
|
||||
}
|
||||
|
||||
func (this *TunnelStatusManager) register(t interface{}) {
|
||||
this.mux.Lock()
|
||||
defer this.mux.Unlock()
|
||||
this.tunnelList.PushBack(t)
|
||||
}
|
||||
|
||||
func (this *TunnelStatusManager) unregister(t interface{}) {
|
||||
this.mux.Lock()
|
||||
defer this.mux.Unlock()
|
||||
l := this.tunnelList
|
||||
for e := l.Front(); e != nil; e = e.Next() {
|
||||
if e == t {
|
||||
l.Remove(e)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (this *TunnelStatusManager) chanListener() {
|
||||
for {
|
||||
select {
|
||||
case r := <-this.regChan:
|
||||
this.register(r)
|
||||
log.Info("A tunnel registered")
|
||||
log.Info(this.QueryStatString())
|
||||
case ur := <-this.unregChan:
|
||||
this.unregister(ur)
|
||||
log.Info("A tunnel unregistered")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *TunnelStatusManager) QueryStatString() string {
|
||||
this.mux.Lock()
|
||||
defer this.mux.Unlock()
|
||||
return fmt.Sprintf("There %d tunnels running.\n", this.tunnelList.Len())
|
||||
}
|
||||
Reference in New Issue
Block a user