core

package
v0.0.0-...-d86056c Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2018 License: Apache-2.0 Imports: 8 Imported by: 9

README

优化策略

程序最大线程数

程序最大线程数的设置,GO进程的默认最大线程数是10000,每个长连接都使用了不同的线程接收数据(之后可能优化)。如果你的业务有成千上万个连接,请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

Documentation

Overview

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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

	// VERSION is the current version.
	READ_BUFFER_SIZE = 1024
)

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(sraddr string) error

Connect 连接服务器连接地址、端口,使用随机端口连接

func (*Bootstrap) ConnectUseInterface

func (bootstrap *Bootstrap) ConnectUseInterface(sraddr, sladdr string) error

ConnectUseInterface 连接服务器连接地址、端口,使用指定网络接口连接(laddr端口为0使用随机端口)。

func (*Bootstrap) CreateContext

func (bootstrap *Bootstrap) CreateContext(sraddr string) (Context, error)

CreateContext 连接服务器连接地址、端口,使用指定网络接口连接(laddr端口为0使用随机端口)。

func (*Bootstrap) CreateContextUseInterface

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

CreateContextUseInterface 连接服务器连接地址、端口,使用指定网络接口连接(laddr端口为0使用随机端口)。

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) 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) LogFlush

func (bootstrap *Bootstrap) LogFlush()

LogFlush logs flush

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) RegisterHandler

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

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

func (*Bootstrap) SetEventListener

func (bootstrap *Bootstrap) SetEventListener(eventListener EventListener) *Bootstrap

SetEventListener 设置连接的事件监听

func (*Bootstrap) SetKeepAlive

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

SetKeepAlive 配置连接keepalive,default is false

func (*Bootstrap) SetReadBufferSize

func (bootstrap *Bootstrap) SetReadBufferSize(readBufferSize int) *Bootstrap

SetReadBufferSize 配置连接读缓存大小。

func (*Bootstrap) Shutdown

func (bootstrap *Bootstrap) Shutdown()

Shutdown 关闭bootstrap

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

type Context

type Context interface {
	Read(b []byte) (n int, err error)
	Write(b []byte) (n int, err error)
	WriteSerialData(s Serializable) (n int, e error)
	LocalAddr() net.Addr
	RemoteAddr() net.Addr
	LocalAddrToSocketAddr() *SocketAddr
	RemoteAddrToSocketAddr() *SocketAddr
	UniqueSocketAddr() *SocketAddr
	ServMode() bool
	Close() error
	Closed() bool
	String() string
}

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

type DefaultEventListener

type DefaultEventListener struct {
}

func (*DefaultEventListener) OnContextActive

func (listener *DefaultEventListener) OnContextActive(ctx Context)

func (*DefaultEventListener) OnContextClosed

func (listener *DefaultEventListener) OnContextClosed(ctx Context)

func (*DefaultEventListener) OnContextConnect

func (listener *DefaultEventListener) OnContextConnect(ctx Context)

func (*DefaultEventListener) OnContextError

func (listener *DefaultEventListener) OnContextError(ctx Context, err error)

type EventListener

type EventListener interface {
	OnContextActive(ctx Context)
	OnContextConnect(ctx Context)
	OnContextClosed(ctx Context)
	OnContextError(ctx Context, err error)
}

type Handler

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

type Options

type Options struct {
	Host           string `json:"addr"`
	Port           int    `json:"port"`
	Keepalive      bool   `json:"keepalive"`
	ReadBufferSize int    `json:"readBufferSize"`
}

type Serializable

type Serializable interface {
	Bytes() []byte
}

type SocketAddr

type SocketAddr struct {
	IP   [net.IPv4len]byte
	Port int
}

func AddrToSocketAddr

func AddrToSocketAddr(addr string) SocketAddr

func (*SocketAddr) String

func (sa *SocketAddr) String() string

Jump to

Keyboard shortcuts

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