netm

package
v0.0.0-...-0a980e4 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

README

smart net

stgnet是smartgo中对网络层通讯的封装,包括协议封装、解包,对网络连接的管理和优化,提供高性能的网络基础组件。

使用注意事项

程序最大线程数

程序最大线程数的设置,GO进程的默认最大线程数是10000,stgnet的每个长连接都使用了不同的线程接收数据(之后可能优化)。如果你的业务有成千上万个连接,请main中使用debug.SetMaxThreads(100000)配置最大线程数

系统最大连接数

配置服务宿主机系统的最大连接数,以centos 7为例。 临时设置,使用命令

ulimit -n 655350

永久生效,编辑vi /etc/security/limits.conf,文件末尾加入:

* soft nofile 655350
* hard nofile 655350
  • 表示属于用户,可以指定用户。
系统TCP参数优化(参考,以机器配置和测试结果为准)

编辑/etc/sysctl.conf

net.ipv4.ip_local_port_range = 1024 65536
net.core.rmem_max=16777216
net.core.wmem_max=16777216
net.ipv4.tcp_rmem=4096 87380 16777216
net.ipv4.tcp_wmem=4096 65536 16777216
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_window_scaling = 0
net.ipv4.tcp_sack = 0
net.core.netdev_max_backlog = 30000
net.ipv4.tcp_no_metrics_save=1
net.core.somaxconn = 262144
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

生效命令:

sysctl -p /etc/sysctl.conf
sysctl -w net.ipv4.route.flush=1

遗留问题

  1. 逻辑粘包乱序问题没有处理(netty应该也没有处理,初步确定)
  2. 高并发情况下的缓冲处理能力。
  3. 长连接是否有方案使用复用线程的可能性。

Read the docs

Documentation

Index

Constants

View Source
const (
	// VERSION is the current version.
	VERSION = "1.0.0"

	// ACCEPT_MIN_SLEEP is the minimum acceptable sleep times on temporary errors.
	ACCEPT_MIN_SLEEP = 10 * time.Millisecond

	// ACCEPT_MAX_SLEEP is the maximum acceptable sleep times on temporary errors
	ACCEPT_MAX_SLEEP = 1 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bootstrap

type Bootstrap struct {
	// contains filtered or unexported fields
}

Bootstrap 启动器

func NewBootstrap

func NewBootstrap() *Bootstrap

NewBootstrap 创建启动器

func (*Bootstrap) Bind

func (bootstrap *Bootstrap) Bind(host string, port int) *Bootstrap

Bind 监听地址、端口

func (*Bootstrap) Connect

func (bootstrap *Bootstrap) Connect(host string, port int) error

Connect 连接指定地址、端口(服务器地址管理连接)

func (*Bootstrap) ConnectJoinAddr

func (bootstrap *Bootstrap) ConnectJoinAddr(addr string) error

Connect 使用指定地址、端口的连接字符串连接

func (*Bootstrap) ConnectJoinAddrAndReturn

func (bootstrap *Bootstrap) ConnectJoinAddrAndReturn(addr string) (Context, error)

Connect 使用指定地址、端口的连接字符串进行连接并返回连接

func (*Bootstrap) Context

func (bootstrap *Bootstrap) Context(addr string) Context

Contexts 返回指定context

func (*Bootstrap) Contexts

func (bootstrap *Bootstrap) Contexts() []Context

Contexts 返回context

func (*Bootstrap) Debug

func (bootstrap *Bootstrap) Debug(v ...interface{})

Debug logs a debug statement

func (*Bootstrap) Debugf

func (bootstrap *Bootstrap) Debugf(format string, v ...interface{})

Debugf logs a debug statement

func (*Bootstrap) Disconnect

func (bootstrap *Bootstrap) Disconnect(addr string)

Disconnect 关闭指定连接

func (*Bootstrap) Error

func (bootstrap *Bootstrap) Error(v ...interface{})

Error logs an error

func (*Bootstrap) Errorf

func (bootstrap *Bootstrap) Errorf(format string, v ...interface{})

Errorf logs an error

func (*Bootstrap) Fatal

func (bootstrap *Bootstrap) Fatal(v ...interface{})

Fatal logs a fatal error

func (*Bootstrap) Fatalf

func (bootstrap *Bootstrap) Fatalf(format string, v ...interface{})

Fatalf logs a fatal error

func (*Bootstrap) HasConnect

func (bootstrap *Bootstrap) HasConnect(addr string) bool

HasConnect find connect by addr, return bool

func (*Bootstrap) LogFlush

func (bootstrap *Bootstrap) LogFlush()

LogFlush logs flush

func (*Bootstrap) NewRandomConnect

func (bootstrap *Bootstrap) NewRandomConnect(sraddr, sladdr string) (Context, error)

NewRandomConnect 连接指定本地和远程地址、端口(laddr端口为0为随机端口)。特殊业务使用

func (*Bootstrap) Notice

func (bootstrap *Bootstrap) Notice(v ...interface{})

Notice logs a notice statement

func (*Bootstrap) Noticef

func (bootstrap *Bootstrap) Noticef(format string, v ...interface{})

Noticef logs a notice statement

func (*Bootstrap) RegisterContextListener

func (bootstrap *Bootstrap) RegisterContextListener(contextListener ContextListener) *Bootstrap

RegisterContextListener 注册连接的监听接口

func (*Bootstrap) RegisterHandler

func (bootstrap *Bootstrap) RegisterHandler(fns ...Handler) *Bootstrap

RegisterHandler 注册连接接收数据时回调执行函数

func (*Bootstrap) SetIdle

func (bootstrap *Bootstrap) SetIdle(idle int) *Bootstrap

设置空闲时间,单位秒

func (*Bootstrap) SetKeepAlive

func (bootstrap *Bootstrap) SetKeepAlive(keepalive bool) *Bootstrap

SetKeepAlive 配置连接keepalive,default is false

func (*Bootstrap) Shutdown

func (bootstrap *Bootstrap) Shutdown()

Shutdown 关闭bootstrap

func (*Bootstrap) Size

func (bootstrap *Bootstrap) Size() int

Size 当前连接数

func (*Bootstrap) Sync

func (bootstrap *Bootstrap) Sync()

Sync 启动服务

func (*Bootstrap) Trace

func (bootstrap *Bootstrap) Trace(v ...interface{})

Trace logs a trace statement

func (*Bootstrap) Tracef

func (bootstrap *Bootstrap) Tracef(format string, v ...interface{})

Tracef logs a trace statement

func (*Bootstrap) Warn

func (bootstrap *Bootstrap) Warn(v ...interface{})

Warn logs an warn

func (*Bootstrap) Warnf

func (bootstrap *Bootstrap) Warnf(format string, v ...interface{})

Warnf logs an warn

func (*Bootstrap) Write

func (bootstrap *Bootstrap) Write(addr string, buffer []byte) (n int, e error)

Write 发送消息

type Context

type Context interface {
	Read(b []byte) (n int, err error)
	Write(b []byte) (n int, err error)
	WriteSerialObject(s Serializable) (n int, e error)
	LocalAddr() net.Addr
	RemoteAddr() net.Addr
	Close() error
	IsClosed() bool
	Idle() time.Duration
	Addr() string
	ToString() string
}

Context the context of connection, like conn channel, not go chan.

type ContextListener

type ContextListener interface {
	OnContextConnect(ctx Context)
	OnContextClose(ctx Context)
	OnContextError(ctx Context)
	OnContextIdle(ctx Context)
}

type DefaultContext

type DefaultContext struct {
	// contains filtered or unexported fields
}

DefaultContext default context

func (*DefaultContext) Addr

func (ctx *DefaultContext) Addr() string

Addr 返回索引地址

func (*DefaultContext) Close

func (ctx *DefaultContext) Close() error

Close 关闭连接

func (*DefaultContext) Idle

func (ctx *DefaultContext) Idle() time.Duration

Idle 返回空闲时间

func (*DefaultContext) IsClosed

func (ctx *DefaultContext) IsClosed() bool

IsClosed 返回索引地址

func (*DefaultContext) LocalAddr

func (ctx *DefaultContext) LocalAddr() net.Addr

LocalAddr 本地连接地址

func (*DefaultContext) Read

func (ctx *DefaultContext) Read(b []byte) (n int, e error)

Read 读取数据

func (*DefaultContext) RemoteAddr

func (ctx *DefaultContext) RemoteAddr() net.Addr

RemoteAddr 远程连接地址

func (*DefaultContext) ToString

func (ctx *DefaultContext) ToString() string

ToString 打印net.Conn的基本参数

func (*DefaultContext) Write

func (ctx *DefaultContext) Write(b []byte) (n int, e error)

Write 写数据

func (*DefaultContext) WriteSerialObject

func (ctx *DefaultContext) WriteSerialObject(s Serializable) (n int, e error)

WriteSerialObject 写序列化数据

type Handler

type Handler func(buffer []byte, ctx Context)

type Options

type Options struct {
	Host string `json:"addr"`
	Port int    `json:"port"`
	Idle int    `json:"idle"`
}

type Serializable

type Serializable interface {
	Bytes() []byte
}

Jump to

Keyboard shortcuts

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