原文作者:gihnius 来源:GitHub
A TCP connection monitor library written in Go.
This is a library(tool) for monitoring and debugging network services.
I built this for the purpose of facilitating debugging and monitoring the network connections of IoT devices. At this stage, it is just an experimental tool.
Note: For each tcp connection, an additional goroutine is used for monitoring, and uses a MonConn struct to store the monitored info, which takes up a bit more memory. For 5000 long connections, it takes up about 80~100MB of memory.
So it's not recommended to use it in high performance production environment.
features
1go get -u -v github.com/gihnius/monconn
1import "github.com/gihnius/monconn"
2
3// --------------- global setup
4monconn.Debug = true // enable debug log
5monconn.DebugFunc = func(format string, v ...interface{}) {
6 // custom log function
7}
8
9// set net.Conn buffer size, default 4k
10monconn.ReadBufSize = 4 << 10
11monconn.WriteBufSize = 4 << 10
12
13// set how many concurrency clients(ip) can connect to a service
14// default 64
15monconn.MaxIPLimit = 1000
16
17// --------------- service setup
18// set net.Conn read write timeout
19service.ReadTimeout = 600
20service.WriteTimeout = 600
21// close wait timeout
22service.WaitTimeout = 60
23// max idle check in seconds
24service.MaxIdle = 900
25// connections count to a service, default 0, no limit
26service.ConnLimit = 0
27// ips count to service, default 0, no limit
28service.IPLimit = 0
29// set tcp connection keepalive, default true
30service.KeepAlive = true
31// Print read write bytes in hex format, default false
32service.PrintBytes = false
1// create service by given a sid: "127.0.0.1:1234"
2// a service is binding to a tcp listener, so normally
3// use a listen address as sid to identify a service
4// you can also choose a uniqe string as sid.
5service := monconn.NewService("127.0.0.1:1234")
6// configure service like above
7// service.ReadTimeout = ...
8// listen a tcp port
9ln, _ := net.Listen("tcp", "127.0.0.1:1234")
10// start the service monitor on ln
11service.Start(ln)
12
13// accept connection and monitor the connection
14conn, err := ln.Accept(
15if err != nil {
16 // handle err
17}
18// acquire the connection
19// if no ip or connection limits or ip blacklist provided
20// there is no need to acquire connection
21// just call WrapMonConn(conn)
22if service.Acquirable(conn) {
23 // wrap net.Conn with monconn.MonConn by service.WrapMonConn()
24 monitoredConn := service.WrapMonConn(conn)
25 // where monitoredConn is also a net.Conn
26 // handle the tcp connection
27 go HandleConn(monitoredConn)
28}
29
30// when everything done, usually before program exit,
31// call service.Stop() to stop the listener as well as service
32service.Stop()
33// or call Shutdown() to Stop all services if there are multiple started.
34monconn.Shutdown()
35
36// or pls checkout the example code in examples/
37
38// checkout the API to see how to grab the monitored infomation.
39
40// ... that's all
41
see godoc : https://godoc.org/github.com/gihnius/monconn
版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢。