xnet

package
v0.0.0-...-f89cf20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2020 License: MIT Imports: 4 Imported by: 0

README

XNet

标准库实现了大量的net函数,这里作为补充,提供了LimitedListenerKeepaliveListener, 以及一个辅助函数PrivateAddress获取内网地址。

LimitedListener

对最大同时连接数做了上限限制n

package xnet

type limitedListener struct {
	net.Listener
	sem       chan struct{}
	closeOnce sync.Once     // ensures the done chan is only closed once
	done      chan struct{} // no values sent; closed when Close is called
}

func LimitListener(l net.Listener, n int) net.Listener {
	return &limitedListener{
		Listener: l,
		sem:      make(chan struct{}, n),
		done:     make(chan struct{}),
	}
}

KeepaliveListener

会定时发送keepalive message的tcpListener

package xnet

type keepaliveListener struct {
	*net.TCPListener
	timeout time.Duration
}
// 创建一个新的keepalive的listener,keepalive间隔为keepalive,如果listener不是*TcpListener,则会原样返回。
func KeepAliveListener(l net.Listener, keepalive time.Duration) net.Listener
// 设置net.TcpConn keepalive的时间间隔
func SetKeepAlive(c net.Conn, keepalive time.Duration)

TimeoutListener

会自动设置超时间的tcpListener

package xnet

type timeoutListener struct {
	*net.TCPListener
	timeout time.Duration
}
// 创建一个新的具有超时时间的listener,超时时间为timeout,如果listener不是*TcpListener,则会原样返回。
func TimeoutListener(l net.Listener, timeout time.Duration) net.Listener
// 设置net.TcpConn的超时时间
func SetTimeout(c net.Conn, timeout time.Duration)

Documentation

Overview

Package xnet provides network utility functions, complementing the more common ones in the net package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IPAddresses

func IPAddresses() []string

IPAddresses returns all known ips

func KeepAliveListener

func KeepAliveListener(l net.Listener, keepalive time.Duration) net.Listener

KeepAliveListener returns listener if l is a *net.TCPListener. Otherwise, l is returned without change.

func LimitedListener

func LimitedListener(l net.Listener, n int) net.Listener

LimitedListener returns a Listener that accepts at most n simultaneous connections from the provided Listener.

func PrivateAddress

func PrivateAddress() (string, error)

PrivateAddress returns a real ip

func SetKeepAlive

func SetKeepAlive(c net.Conn, keepalive time.Duration)

SetKeepAlive enables TCP keep-alive if c is a *net.TCPConn.

func SetTimeout

func SetTimeout(c net.Conn, timeout time.Duration)

func TimeoutListener

func TimeoutListener(l net.Listener, timeout time.Duration) net.Listener

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL