module advance

This commit is contained in:
unknown
2019-10-14 18:12:37 +08:00
parent eed3efd18f
commit 3904f0c797
10 changed files with 385 additions and 60 deletions

26
core/config.go Normal file
View File

@@ -0,0 +1,26 @@
package core
// This structure is used to describe the plugin configuration item name and description.
type Config struct {
ConfigName string
Description string
}
type NpsConfigs struct {
configs []*Config
}
func NewNpsConfigs(name, des string) *NpsConfigs {
c := &NpsConfigs{}
c.configs = make([]*Config, 0)
c.Add(name, des)
return c
}
func (config *NpsConfigs) Add(name, des string) {
config.configs = append(config.configs, &Config{ConfigName: name, Description: des})
}
func (config *NpsConfigs) GetAll() []*Config {
return config.configs
}

174
core/pool.go Normal file
View File

@@ -0,0 +1,174 @@
package core
import (
"bytes"
"sync"
)
const PoolSize = 64 * 1024
const PoolSizeSmall = 100
const PoolSizeUdp = 1472
const PoolSizeCopy = 32 << 10
const PoolSizeWindow = 1<<16 - 1
var BufPool = sync.Pool{
New: func() interface{} {
return make([]byte, PoolSize)
},
}
var BufPoolUdp = sync.Pool{
New: func() interface{} {
return make([]byte, PoolSizeUdp)
},
}
var BufPoolMax = sync.Pool{
New: func() interface{} {
return make([]byte, PoolSize)
},
}
var BufPoolSmall = sync.Pool{
New: func() interface{} {
return make([]byte, PoolSizeSmall)
},
}
var BufPoolCopy = sync.Pool{
New: func() interface{} {
return make([]byte, PoolSizeCopy)
},
}
func PutBufPoolUdp(buf []byte) {
if cap(buf) == PoolSizeUdp {
BufPoolUdp.Put(buf[:PoolSizeUdp])
}
}
func PutBufPoolCopy(buf []byte) {
if cap(buf) == PoolSizeCopy {
BufPoolCopy.Put(buf[:PoolSizeCopy])
}
}
func GetBufPoolCopy() []byte {
return (BufPoolCopy.Get().([]byte))[:PoolSizeCopy]
}
func PutBufPoolMax(buf []byte) {
if cap(buf) == PoolSize {
BufPoolMax.Put(buf[:PoolSize])
}
}
type copyBufferPool struct {
pool sync.Pool
}
func (Self *copyBufferPool) New() {
Self.pool = sync.Pool{
New: func() interface{} {
return make([]byte, PoolSizeCopy, PoolSizeCopy)
},
}
}
func (Self *copyBufferPool) Get() []byte {
buf := Self.pool.Get().([]byte)
return buf[:PoolSizeCopy] // just like make a new slice, but data may not be 0
}
func (Self *copyBufferPool) Put(x []byte) {
if len(x) == PoolSizeCopy {
Self.pool.Put(x)
} else {
x = nil // buf is not full, not allowed, New method returns a full buf
}
}
type windowBufferPool struct {
pool sync.Pool
}
func (Self *windowBufferPool) New() {
Self.pool = sync.Pool{
New: func() interface{} {
return make([]byte, 0, PoolSizeWindow)
},
}
}
func (Self *windowBufferPool) Get() (buf []byte) {
buf = Self.pool.Get().([]byte)
return buf[:0]
}
func (Self *windowBufferPool) Put(x []byte) {
if cap(x) == PoolSizeWindow {
Self.pool.Put(x[:PoolSizeWindow]) // make buf to full
} else {
x = nil
}
}
type bufferPool struct {
pool sync.Pool
}
func (Self *bufferPool) New() {
Self.pool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
}
func (Self *bufferPool) Get() *bytes.Buffer {
return Self.pool.Get().(*bytes.Buffer)
}
func (Self *bufferPool) Put(x *bytes.Buffer) {
x.Reset()
Self.pool.Put(x)
}
type muxPackagerPool struct {
pool sync.Pool
}
func (Self *muxPackagerPool) New() {
Self.pool = sync.Pool{
New: func() interface{} {
pack := MuxPackager{}
return &pack
},
}
}
func (Self *muxPackagerPool) Get() *MuxPackager {
pack := Self.pool.Get().(*MuxPackager)
buf := CopyBuff.Get()
pack.Content = buf
return pack
}
func (Self *muxPackagerPool) Put(pack *MuxPackager) {
CopyBuff.Put(pack.Content)
Self.pool.Put(pack)
}
var once = sync.Once{}
var BuffPool = bufferPool{}
var CopyBuff = copyBufferPool{}
var MuxPack = muxPackagerPool{}
var WindowBuff = windowBufferPool{}
func newPool() {
BuffPool.New()
CopyBuff.New()
MuxPack.New()
WindowBuff.New()
}
func init() {
once.Do(newPool)
}

View File

@@ -2,14 +2,9 @@ package core
import (
"context"
"errors"
)
// This structure is used to describe the plugin configuration item name and description.
type Config struct {
ConfigName string
Description string
}
type Stage uint8
// These constants are meant to describe the stage in which the plugin is running.
@@ -21,11 +16,23 @@ const (
STAGE_START
STAGE_END
STAGE_RUN
PROXY_CONNECTION_TYPE = "proxy_target_type"
PROXY_CONNECTION_ADDR = "proxy_target_addr"
PROXY_CONNECTION_PORT = "proxy_target_port"
CLIENT_CONNECTION = "clientConn"
BRIDGE = "bridge"
CLIENT_ID = "client_id"
)
var (
CLIENT_CONNECTION_NOT_EXIST = errors.New("the client connection is not exist")
BRIDGE_NOT_EXIST = errors.New("the client connection is not exist")
REQUEST_EOF = errors.New("the request has finished")
)
// Plugin interface, all plugins must implement those functions.
type Plugin interface {
GetConfigName() []*Config
GetConfigName() *NpsConfigs
GetBeforePlugin() Plugin
GetStage() Stage
Start(ctx context.Context, config map[string]string) error

30
core/utils.go Normal file
View File

@@ -0,0 +1,30 @@
package core
import "io"
func CopyBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
buf := CopyBuff.Get()
defer CopyBuff.Put(buf)
for {
nr, er := src.Read(buf)
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er != nil {
err = er
break
}
}
return written, err
}