Skip to content

Commit

Permalink
优化性能,减少缓存大小,1万客户端的服务,持续发数据,内存占用(任务管理器:280MB,pprof:54MB),cpu占用0.5%~3.8…
Browse files Browse the repository at this point in the history
…%(i7-7700)
  • Loading branch information
injoyai committed Feb 21, 2024
1 parent f341d1c commit c4f14a3
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 202 deletions.
14 changes: 12 additions & 2 deletions buf/buf_read_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@ import (
"bufio"
"bytes"
"io"
"sync"
"time"
)

const (
DefaultBufferSize = 1024
)

type (
ReadFunc func(buf *bufio.Reader) (bytes []byte, err error)
WriteFunc func(req []byte) ([]byte, error)
)

var buffPool = sync.Pool{New: func() interface{} {
return make([]byte, DefaultBufferSize)
}}

// ReadWithAll 默认读取函数,读取全部数据
func ReadWithAll(buf *bufio.Reader) (bytes []byte, err error) {
//read,单次读取大小不影响速度
num := 4096
num := DefaultBufferSize
data := buffPool.Get().([]byte)
defer buffPool.Put(data)
for {
data := make([]byte, num)
length, err := buf.Read(data)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion extend/rpc/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewServer(port int, waitTimeout time.Duration, option ...io.OptionServer) (
}
s.SetReadWriteWithPkg()
s.SetDealFunc(ser.dealFunc)
s.SetTimeout(io.DefaultKeepAlive * 3)
s.SetTimeout(io.DefaultTimeout)
s.SetTimeoutInterval(io.DefaultKeepAlive)
ser.Bind(io.Register, func(ctx context.Context, c *io.Client, msg *io.Model) (interface{}, error) {
key := g.UUID()
Expand Down
20 changes: 13 additions & 7 deletions io_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (this *Client) Debug(b ...bool) {

// SetLogger 设置日志
func (this *Client) SetLogger(logger Logger) *Client {
l := newLogger(logger)
l := NewLogger(logger)
this.Logger = l
this.IWriter.Logger = l
this.IReadCloser.IReader.Logger = l
Expand Down Expand Up @@ -282,14 +282,20 @@ func (this *Client) SetOptions(options ...OptionClient) *Client {

// SetDealFunc 设置处理数据函数,默认响应ping>pong,忽略pong
func (this *Client) SetDealFunc(fn func(c *Client, msg Message)) *Client {
pingLen := len(Ping)
pongLen := len(Pong)
this.IReadCloser.SetDealFunc(func(msg Message) {
switch msg.String() {
case Ping:
this.WriteString(Pong)
case Pong:
default:
fn(this, msg)
//先判断长度,减少字节转字符的内存分配,最好用指针的方式(直接用字节的指针)
if msg.Len() == pingLen || msg.Len() == pongLen {
switch msg.String() {
case Ping:
this.WriteString(Pong)
return
case Pong:
return
}
}
fn(this, msg)
})
return this
}
Expand Down
2 changes: 1 addition & 1 deletion io_client_manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewClientManage(ctx context.Context, key string) *ClientManage {
mu: sync.RWMutex{},
ctx: ctx,
readChan: make(chan Message, DefaultChannelSize),
timeout: DefaultKeepAlive * 3,
timeout: DefaultTimeout,
timeoutInterval: DefaultTimeoutInterval,
ClientOptions: ClientOptions{
beforeFunc: nil,
Expand Down
17 changes: 9 additions & 8 deletions io_const.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ const (
PB = 1024 * TB //1PB
EB = 1024 * PB //1EB

DefaultBufferSize = KB4 //默认buff大小,4KB
DefaultChannelSize = 100 //默认通道大小
DefaultPort = 10086 //默认端口
DefaultPortStr = ":10086" //默认端口
DefaultConnectTimeout = time.Second * 2 //默认连接时间
DefaultKeepAlive = time.Minute * 10 //默认保持连接时间
DefaultTimeoutInterval = time.Minute //默认离线检查间隔
DefaultResponseTimeout = time.Second * 10 //默认响应超时时间
DefaultBufferSize = KB //默认buff大小,1KB
DefaultChannelSize = 100 //默认通道大小
DefaultPort = 10086 //默认端口
DefaultPortStr = ":10086" //默认端口
DefaultConnectTimeout = time.Second * 2 //默认连接时间
DefaultKeepAlive = time.Minute * 10 //默认保持连接时间
DefaultTimeout = DefaultKeepAlive * 3 //默认超时时间,3个keepalive时间
DefaultTimeoutInterval = time.Minute //默认离线检查间隔
DefaultResponseTimeout = time.Second * 10 //默认响应超时时间

)

Expand Down
4 changes: 2 additions & 2 deletions io_i_closer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type ICloser struct {

// SetLogger 设置日志
func (this *ICloser) SetLogger(logger Logger) *ICloser {
this.Logger = newLogger(logger)
this.Logger = NewLogger(logger)
return this
}

Expand Down Expand Up @@ -120,7 +120,7 @@ func (this *ICloser) timer(ctx context.Context, dealErr func(error) error, inter
if interval > 0 {
timer := time.NewTimer(interval)
defer timer.Stop()
for {
for i := 0; ; i++ {
timer.Reset(interval)
select {
case <-ctx.Done():
Expand Down
2 changes: 1 addition & 1 deletion io_i_read_closer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (this *IReadCloser) Debug(b ...bool) {
}

func (this *IReadCloser) SetLogger(logger Logger) *IReadCloser {
l := newLogger(logger)
l := NewLogger(logger)
this.IReader.Logger = l
this.ICloser.Logger = l
return this
Expand Down
2 changes: 1 addition & 1 deletion io_i_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewIReader(r Reader) *IReader {
i.buf = v
default:
//todo 优化缓存大小可配置
i.buf = bufio.NewReaderSize(r, DefaultBufferSize)
i.buf = bufio.NewReaderSize(r, DefaultBufferSize+1)
}
i.SetReadFunc(buf.ReadWithAll)
return i
Expand Down
2 changes: 1 addition & 1 deletion io_i_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type IWriter struct {

// SetLogger 设置日志
func (this *IWriter) SetLogger(logger Logger) *IWriter {
this.Logger = newLogger(logger)
this.Logger = NewLogger(logger)
return this
}

Expand Down
22 changes: 7 additions & 15 deletions io_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const (
)

var (
nullLogger = NewLoggerWithWriter(&null{})
nullLogger = NewLoggerWithWriter(&null{})
stdoutLogger = NewLoggerWithWriter(os.Stdout)
)

type Level int
Expand All @@ -39,14 +40,15 @@ func (this Level) Name() string {
}

func defaultLogger() *logger {
return newLogger(NewLoggerWithStdout())
return NewLogger(stdoutLogger)
}

func NewLogger(l Logger) *logger {
return newLogger(l)
// NewLoggerWithNull 无输出的日志
func NewLoggerWithNull() *logger {
return NewLogger(nullLogger)
}

func newLogger(l Logger) *logger {
func NewLogger(l Logger) *logger {
if l2, ok := l.(*logger); ok {
return l2
}
Expand Down Expand Up @@ -150,11 +152,6 @@ func NewLoggerWithWriter(w Writer) Logger {
return &logWriter{w}
}

// NewLoggerWithStdout 新建输出到终端的日志
func NewLoggerWithStdout() Logger {
return NewLoggerWithWriter(os.Stdout)
}

// NewLoggerWithChan 新建输出到指定通道的日志
func NewLoggerWithChan(c chan []byte) Logger {
return NewLoggerWithWriter(TryChan(c))
Expand All @@ -166,11 +163,6 @@ func NewLoggerChan() (Logger, chan []byte) {
return NewLoggerWithChan(c), c
}

// NullLogger 无输出的日志
func NullLogger() Logger {
return nullLogger
}

type Logger interface {
Readf(format string, v ...interface{})
Writef(format string, v ...interface{})
Expand Down
2 changes: 1 addition & 1 deletion io_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (this *Server) Debug(b ...bool) {
}

func (this *Server) SetLogger(logger Logger) *Server {
l := newLogger(logger)
l := NewLogger(logger)
this.Logger = l
this.ICloser.Logger = l
this.ClientManage.Logger = l
Expand Down
3 changes: 2 additions & 1 deletion listen/listen_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ func RunServer(s *io.Server, err error, options ...io.OptionServer) error {
if err != nil {
return err
}
return s.SetOptions(options...).Run()
s.SetOptions(options...)
return s.Run()
}
9 changes: 0 additions & 9 deletions testdata/main/build_arm7.sh

This file was deleted.

10 changes: 0 additions & 10 deletions testdata/main/build_x86.sh

This file was deleted.

15 changes: 0 additions & 15 deletions testdata/main/config/config.json

This file was deleted.

105 changes: 0 additions & 105 deletions testdata/main/main.go

This file was deleted.

27 changes: 27 additions & 0 deletions testdata/testconn/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"github.com/injoyai/io"
"github.com/injoyai/io/dial"
"time"
)

func main() {
for i := 0; i < 10000; i++ {
<-time.After(time.Millisecond * 1)
go dial.NewTCP("127.0.0.1:12000", func(c *io.Client) {
//closeTime := time.Now().Add(time.Second * time.Duration(rand.Intn(10)+10))
c.Debug(false)
c.Logger.SetLevel(io.LevelError)
go c.Run()
c.GoTimerWriter(time.Second*2, func(w *io.IWriter) error {
//if time.Since(closeTime) < 0 {
// return c.Close()
//}
_, err := w.WriteString("ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss")
return err
})
})
}
select {}
}
Loading

0 comments on commit c4f14a3

Please sign in to comment.