mirror of
https://github.com/ehang-io/nps.git
synced 2025-09-02 03:16:53 +00:00
fine mux, add goroutine pool
This commit is contained in:
@@ -2,8 +2,6 @@ package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -151,53 +149,34 @@ func (Self *muxPackagerPool) Put(pack *MuxPackager) {
|
||||
Self.pool.Put(pack)
|
||||
}
|
||||
|
||||
type connGroup struct {
|
||||
src net.Conn
|
||||
dst net.Conn
|
||||
wg *sync.WaitGroup
|
||||
type ListElement struct {
|
||||
Buf []byte
|
||||
L uint16
|
||||
Part bool
|
||||
}
|
||||
|
||||
func newConnGroup(src net.Conn, dst net.Conn, wg *sync.WaitGroup) connGroup {
|
||||
return connGroup{
|
||||
src: src,
|
||||
dst: dst,
|
||||
wg: wg,
|
||||
type listElementPool struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
func (Self *listElementPool) New() {
|
||||
Self.pool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
element := ListElement{}
|
||||
return &element
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func copyConnGroup(group interface{}) {
|
||||
cg, ok := group.(connGroup)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
_, err := CopyBuffer(cg.src, cg.dst)
|
||||
if err != nil {
|
||||
cg.src.Close()
|
||||
cg.dst.Close()
|
||||
//logs.Warn("close npc by copy from nps", err, c.connId)
|
||||
}
|
||||
cg.wg.Done()
|
||||
func (Self *listElementPool) Get() *ListElement {
|
||||
return Self.pool.Get().(*ListElement)
|
||||
}
|
||||
|
||||
type Conns struct {
|
||||
conn1 net.Conn
|
||||
conn2 net.Conn
|
||||
}
|
||||
|
||||
func NewConns(c1 net.Conn, c2 net.Conn) Conns {
|
||||
return Conns{
|
||||
conn1: c1,
|
||||
conn2: c2,
|
||||
}
|
||||
}
|
||||
|
||||
func copyConns(group interface{}) {
|
||||
conns := group.(Conns)
|
||||
wg := new(sync.WaitGroup)
|
||||
wg.Add(2)
|
||||
_ = connCopyPool.Invoke(newConnGroup(conns.conn1, conns.conn2, wg))
|
||||
_ = connCopyPool.Invoke(newConnGroup(conns.conn2, conns.conn1, wg))
|
||||
wg.Wait()
|
||||
func (Self *listElementPool) Put(element *ListElement) {
|
||||
element.L = 0
|
||||
element.Buf = nil
|
||||
element.Part = false
|
||||
Self.pool.Put(element)
|
||||
}
|
||||
|
||||
var once = sync.Once{}
|
||||
@@ -205,14 +184,14 @@ var BuffPool = bufferPool{}
|
||||
var CopyBuff = copyBufferPool{}
|
||||
var MuxPack = muxPackagerPool{}
|
||||
var WindowBuff = windowBufferPool{}
|
||||
var connCopyPool, _ = ants.NewPoolWithFunc(200000, copyConnGroup, ants.WithNonblocking(false))
|
||||
var CopyConnsPool, _ = ants.NewPoolWithFunc(100000, copyConns, ants.WithNonblocking(false))
|
||||
var ListElementPool = listElementPool{}
|
||||
|
||||
func newPool() {
|
||||
BuffPool.New()
|
||||
CopyBuff.New()
|
||||
MuxPack.New()
|
||||
WindowBuff.New()
|
||||
ListElementPool.New()
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
Reference in New Issue
Block a user