Skip to content

Commit

Permalink
add monitor logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiekai committed Apr 25, 2016
1 parent 987f53f commit 82952c3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cmd/relay
34 changes: 34 additions & 0 deletions monitor/monitor.go
Original file line number Diff line number Diff line change
@@ -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{}{}
}
7 changes: 6 additions & 1 deletion relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"io"
"net"
"sync"

"github.com/zjiekai/relay/monitor"
)

var (
Expand All @@ -16,6 +18,7 @@ var (
)

func Start() {
monitor.Init()
ln, err := net.Listen("tcp", *laddr)
if err != nil {
return
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -91,6 +96,6 @@ func handleConn(c net.Conn) {
}

func main() {
flag.Parse()
flag.Parse()
Start()
}
3 changes: 1 addition & 2 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
"revision": "23def4e6c14b4da8ac2ed8007337bc5eb5007998",
"revisionTime": "2016-01-25T20:49:56Z"
}
],
"rootPath": "zjk/relay"
]
}

0 comments on commit 82952c3

Please sign in to comment.