微信号:Golangweb

介绍:欢迎来到 Go语言社区 社区网址:www.golangweb.com APP网址:app.golangweb.com 社区非公司性质,完全个人爱好建立;做的不好的地方大家见谅.

用Go编写的TCP连接监视器库

2018-10-22 22:00 Golang语言社区

原文作者:gihnius
来源:GitHub

monconn

A TCP connection monitor library written in Go.

About

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

  • reject remote ip by managing an ip blacklist

  • limit ip connections

  • record upload/download traffics of all connections

  • check idle tcp connection

  • show connecting ip connections count

  • show read and write bytes(hex array) on a connection

  • gracefull stop a tcp listener

Usage

Install

1go get -u -v github.com/gihnius/monconn

Configuration

 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

Example

 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

API

monconn package api

  • NewService(sid) to create a service

  • GetService(sid) get service by sid

  • DelService(sid) delete a service

  • TotalConns() total connections count for live services

  • IPs() all connecting client ips, return as comma-seperated string

  • Shutdown() Stop all services

Service instance method

  • RejectIP(ip) add ip to blacklist

  • ReleaseIP(ip) remove ip from blacklist

  • Acquirable() check if continue to monitor new conection

  • WrapMonConn()

  • Start() start monitor the listener

  • Stop()

  • EliminateBytes(r, w) see godoc

  • ReadWriteBytes() return how many bytes read or write in a service

  • IPs() service's connecting ip

  • Uptime() service's uptime, in seconds

  • Sid() service's sid getter

  • AccessAt() latest client connect time

  • BootAt() service start from time

  • ConnCount() how many realtime connections

  • IPCount() realtime ips

  • Stats() json format stats

  • Log()

MonConn instance method

  • Idle() tell if client read idle

  • Stats()

  • Log()

see godoc : https://godoc.org/github.com/gihnius/monconn


版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢。



Golang语言社区

ID:Golangweb

www.bytedancing.com

游戏服务器架构丨分布式技术丨大数据丨游戏算法学习


 
Golang语言社区 更多文章 Google Cloud 宣布支持Go 1.11 Google Cloud 宣布支持Go 1.11 Golang 单元测试框架 gocheck 使用介绍 实现高并发更轻松!这个编程语言竟然超过了Java... Golang的反射reflect深入理解和示例
猜您喜欢 把服务器打造成Docker容器只需4步 Android 静动态壁纸开发-WallpaperService ENode框架Conference案例分析系列之 - 事件溯源如何处理重构问题 DeepMind重大突破:AI学会了“抄近路” “八亿件衬衫换架飞机”,中国制造还是廉价代名词吗 | 品牌新事