From 82952c3f86d7ae043ad9e60aaa446501f9bfae26 Mon Sep 17 00:00:00 2001 From: Jiekai Date: Mon, 25 Apr 2016 15:22:29 +0800 Subject: [PATCH] add monitor logging --- .gitignore | 1 + monitor/monitor.go | 34 ++++++++++++++++++++++++++++++++++ relay.go | 7 ++++++- vendor/vendor.json | 3 +-- 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 monitor/monitor.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..768659e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +cmd/relay diff --git a/monitor/monitor.go b/monitor/monitor.go new file mode 100644 index 0000000..9a3c9bf --- /dev/null +++ b/monitor/monitor.go @@ -0,0 +1,34 @@ +package monitor + +import "github.com/golang/glog" + +var openPipe chan struct{} +var closePipe chan struct{} + +func Init() { + openPipe = make(chan struct{}) + closePipe = make(chan struct{}) + go monitor() +} + +func monitor() { + pipeCount := 0 + for { + select { + case <-openPipe: + pipeCount++ + glog.Infof("a new pipe opened. %v pipe(s) open", pipeCount) + case <-closePipe: + pipeCount-- + glog.Infof("an old pipe closed. %v pipe(s) open", pipeCount) + } + } +} + +func PipeOpen() { + openPipe <- struct{}{} +} + +func PipeClose() { + closePipe <- struct{}{} +} diff --git a/relay.go b/relay.go index 20f830f..4988429 100644 --- a/relay.go +++ b/relay.go @@ -7,6 +7,8 @@ import ( "io" "net" "sync" + + "github.com/zjiekai/relay/monitor" ) var ( @@ -16,6 +18,7 @@ var ( ) func Start() { + monitor.Init() ln, err := net.Listen("tcp", *laddr) if err != nil { return @@ -50,7 +53,9 @@ func Pipe(src io.ReadWriteCloser, dst io.ReadWriteCloser) (int64, int64) { o.Do(close) }() + monitor.PipeOpen() <-c + monitor.PipeClose() return sent, received } @@ -91,6 +96,6 @@ func handleConn(c net.Conn) { } func main() { - flag.Parse() + flag.Parse() Start() } diff --git a/vendor/vendor.json b/vendor/vendor.json index 179de1e..2fc899d 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -8,6 +8,5 @@ "revision": "23def4e6c14b4da8ac2ed8007337bc5eb5007998", "revisionTime": "2016-01-25T20:49:56Z" } - ], - "rootPath": "zjk/relay" + ] }