前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用Go编写的TCP连接监视器库

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

作者头像
李海彬
发布2018-11-22 17:58:17
1.2K0
发布2018-11-22 17:58:17
举报
文章被收录于专栏:Golang语言社区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

代码语言:javascript
复制
1go get -u -v github.com/gihnius/monconn

Configuration

代码语言:javascript
复制
 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

代码语言:javascript
复制
 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


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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-10-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • monconn
    • About
      • Usage
        • Install
        • Configuration
        • Example
      • API
        • monconn package api
        • Service instance method
        • MonConn instance method
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档