comm

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

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

Go to latest
Published: Dec 17, 2021 License: GPL-3.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TIME_EPOLL_BASE int64 = 1577808000 //2020-01-01 00:00:00

	TIME_FORMAT_SEC  = "2006-01-02 15:04:05"
	TIME_FORMAT_MILL = "2006-01-02 15:04:05.000"
	TIME_FORMAT_MICR = "2006-01-02 15:04:05.000000"
	TIME_FORMAT_NANO = "2006-01-02 15:04:05.000000000"

	DEFAULT_SERVER_SLEEP_IDLE = 5 //ms. server sleeps when idle

	INFO_EXIT       = 0 //0 server exit #sig-int
	INFO_RELOAD_CFG = 1 //1 server reload config #sig-usr1
	INFO_USR2       = 2 //2 sig-usr2 #sig-usr2
	INFO_PPROF      = 3 //3 go pprof #sig-term

	SELECT_METHOD_RAND = 1 //select id by rand
	SELECT_METHOD_HASH = 2 //select by hash

	RAND_STR_POOL = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678!@#$%^&*()-_=+?<>;:"
)
View Source
const (
	TEMP_DIR_PREFIX        = "/tmp/sg_run_2020"
	REWRITE_LOCK_FILE_TICK = (3600 * 6) //seconds maintain lock file in /tmp
)
View Source
const (
	HARD_MAX_CONN_LIMIT = 1000
	REDIS_CONN_NONE     = 0
	REDIS_CONN_ING      = 1
	REDIS_CONN_DONE     = 2
)
View Source
const (
	//if manger count > 1 or no use
	REPORT_METHOD_SEQ  = 1 //report status to manager which is latest valid
	REPORT_METHOD_ALL  = 2 //report status to all manager
	REPORT_METHOD_MOD  = 3 //report status to proc_id%manager_count to manager
	REPORT_METHOD_RAND = 4 //report status to a rand manager

	//MSG CHAN BUFFER
	REPROT_MSG_CHAN_SIZE = 100
	//MAX SEND&RECV
	MAX_SEND_PER_TICK = 10
	MAX_RECV_PER_TICK = 10
)
View Source
const (
	MAX_REPORT_MSG_LEN = 10 * 1024
	//sys defined proto here
	REPORT_PROTO_MONITOR  = 1     //monitor information from top cmd
	REPORT_PROTO_RESERVED = 10000 //reserved proto id
	//user defined proto starts here
	REPORT_PROTO_SERVER_START = 10001 //server start time
	REPORT_PROTO_SERVER_HEART = 10002 //server heartbeat
	REPORT_PROTO_CONN_NUM     = 10003 //server connection number.
	REPORT_PROTO_SYNC_SERVER  = 10004 //sync basic server information
	REPORT_PROTO_SERVER_STOP  = 10005 //server stop time
	REPORT_PROTO_CMD_REQ      = 10006 //cmd to reload cfg
	REPORT_PROTO_CMD_RSP      = 10007 //reload rsp

	//Cmd Stat
	CMD_STAT_NONE    = ""
	CMD_STAT_ING     = "ing"
	CMD_STAT_SUCCESS = "done"
	CMD_STAT_FAIL    = "fail"
	CMD_STAT_NOP     = "nop" //no operation

	//Report Cmd
	CMD_CMD_NONE    = ""
	CMD_RELOAD_CFG  = "reload_cfg"
	CMD_RELOAD_TAB  = "reload_table"
	CMD_STOP_SERVER = "stop_server"
	CMD_LOG_LEVEL   = "log_level"  //chg log filt level
	CMD_LOG_DEGREE  = "log_degree" //chg log time degree
	CMD_LOG_ROTATE  = "log_rotate" //chg log rotate
	CMD_LOG_SIZE    = "log_size"   //chg log size
	CMD_START_GPROF = "start_gprof"
	CMD_END_GRPOF   = "end_gprof"
)

report proto

View Source
const (
	CPU_PROFILE string = "cpu.profile"
	MEM_PROFILE string = "mem.profile"
)
View Source
const (
	TablePath     = "./table"
	HeadTableFile = "__head.json"
)
View Source
const (
	MAX_PKG_LEN = ss.MAX_SS_MSG_SIZE //200K

	TCP_SERV_ACC_TIMEOUT = 5 //tcp_serv accept timeout ms
	CLIENT_STAT_CLOSING  = 0
	CLIENT_STAT_NORMAL   = 1

	MAX_QUEUE_DATA = MAX_PKG_LEN //max queue data == max_pkg_len
	//RECV_QUEUE_LEN=128
	SND_QUEUE_LEN = 128

	SERV_RECV_QUEUE  = 4096
	SERV_SND_QUEUE   = 4096
	MAX_PKG_PER_RECV = 200 //max pkg per recv

	//QUEUE_INFO
	QUEUE_INFO_NORMAL    = 0
	QUEUE_INFO_CLOSE     = 1
	QUEUE_INFO_BROADCAST = 2

	//ClientPkg.PkgType
	CLIENT_PKG_T_NORMAL      = 1 // special pkg using key
	CLIENT_PKG_T_BROADCAST   = 2 //broadcast pkg
	CLIENT_PKG_T_CONN_CLOSED = 3 //client connection closed
	CLIENT_PKG_T_CLOSE_CONN  = 4 //close client

	//R&W TIMEOUT
	CLIENT_RW_TIMEOUT = 2 //ms
	//RW CACHE
	CLIENT_RW_CACHE_SHRINK_FACTOR = 4           //if cache_size / base_size > factor will shrink memory
	CLIENT_RW_CACHE_BASE          = (10 * 1024) //base 10K
)
View Source
const (
	TICKER_TYPE_SINGLE int8  = 1
	TICKER_TYPE_CIRCLE int8  = 2
	MAX_EXPIRE_MS      int64 = 0xFFFFFFFFFFFF

	PERIOD_HEART_BEAT_DEFAULT      = 10000 //10s
	PERIOD_REPORT_SYNC_DEFAULT     = 60000 //1min
	PERIOD_RECV_REPORT_CMD_DEFAULT = 5000  //5s
)

Variables

This section is empty.

Functions

func CloseProFile

func CloseProFile(pconfig *CommConfig)

func Conv2Bytes

func Conv2Bytes(result interface{}) ([]byte, error)

func Conv2Float64

func Conv2Float64(result interface{}) (float64, error)

func Conv2Int

func Conv2Int(result interface{}) (int, error)

Conver Wrapper refer reply.go in goredis

func Conv2Int64

func Conv2Int64(result interface{}) (int64, error)

func Conv2Int64Map

func Conv2Int64Map(result interface{}) (map[string]int64, error)

func Conv2IntMap

func Conv2IntMap(result interface{}) (map[string]int, error)

func Conv2String

func Conv2String(result interface{}) (string, error)

func Conv2StringMap

func Conv2StringMap(result interface{}) (map[string]string, error)

func Conv2Strings

func Conv2Strings(result interface{}) ([]string, error)

func Conv2UInt64

func Conv2UInt64(result interface{}) (uint64, error)

func Conv2Values

func Conv2Values(result interface{}) ([]interface{}, error)

func Daemonize

func Daemonize()

daemon process

func DecodeReportMsg

func DecodeReportMsg(data []byte, pmsg *ReportMsg) error

func DefaultHandleProfile

func DefaultHandleProfile(pconfig *CommConfig) bool

func EncPassString

func EncPassString(pass string, salt string) string

enc password

func EncSha256

func EncSha256(p []byte) string

func EncodeReportMsg

func EncodeReportMsg(pmsg *ReportMsg) ([]byte, error)

func EndPProf

func EndPProf(pconfig *CommConfig) bool

func ExDispMsg

func ExDispMsg(pdisp *ss.MsgDisp) (interface{}, error)

Extract MsgDispxx sub msg from MsgDisp. this function should be modified when new ss.DISP_PROTO_TYPE added @return:success: MsgDispxxxx and nil if failed

func FillSSPkg

func FillSSPkg(ss_msg *ss.SSMsg, proto ss.SS_PROTO_TYPE, pmsg interface{}) error

Fill SSMsg By ProtoType and MsgBody This is a helper for wrapper pkg @pmsg: ss.Msg*** defined in SSMsg.msg_body @pss_msg: fill info of this ss_msg @return: error

func GenDispMsg

func GenDispMsg(target ss.DISP_MSG_TARGET, method ss.DISP_MSG_METHOD, proto ss.DISP_PROTO_TYPE, spec int, sender int, hash_v int64,
	disp_msg interface{}) (*ss.SSMsg, error)

Generate Disp Msg by arg. this function should be modified when new ss.DISP_PROTO_TYPE added @disp_msg: Disp sub msg like ss.MsgDispxxxx @target:target server type @method:choose spec target server method @spec:spec target server will ignore @target and @method @hash_v: if method is hash , this specify hash_value @return:ss_msg , error

func GenRandStr

func GenRandStr(str_len int) (string, error)

generate rand str lenth==str_len

func GenerateLocalId

func GenerateLocalId(wid int16, seq *uint16) int64

generate local id var seq uint16 = 1;

func HandleSignal

func HandleSignal(pconfig *CommConfig)

func IsNetError

func IsNetError(err error) bool

Check Is NetError

func LoadJsonFile

func LoadJsonFile(config_file string, file_config interface{}, pconfig *CommConfig) bool

func LoadTableFiles

func LoadTableFiles(table_map TableMap, pconfig *CommConfig) bool

func LockUniqFile

func LockUniqFile(pconfig *CommConfig, name_space string, proc_id int, proc_name string) bool

uniq process lock file

func ReLoadTableFiles

func ReLoadTableFiles(table_map TableMap, pconfig *CommConfig) bool

func ReadFile

func ReadFile(file_name string, close bool) ([]byte, error)

Read All File content @close:if close file after reading

func SelectProperServ

func SelectProperServ(pconfig *CommConfig, method int, hash_v int64, candidate []int, stats map[int]int64, life_time int64) int

select a proper id * @method:refer SELECT_METHOD_XX * @hash_v:if method==SELECT_METHOD_HASH ,it sets hash value * @candidate:: candidate proc_id of servers * @stats: server heart stats * @life_time: valid life_time in stats * @result <=0 failed >0 success * PS:if candidate lens == 1 will only select one if in life_time

func StartPProf

func StartPProf(pconfig *CommConfig) bool

func UnlockUniqFile

func UnlockUniqFile(pconfig *CommConfig, name_space string, proc_id int, proc_name string) bool

unlock

Types

type ClientPkg

type ClientPkg struct {
	PkgType int8
	//Flag uint8
	ClientKey int64 //the key identify a client
	Data      []byte
}

Caller Proc <-> tcp_serv

type CmdExtraMsg

type CmdExtraMsg struct {
	ExtraValue string `json:"ext_value"`
}

type CommConfig

type CommConfig struct {
	StartTs        int64
	Log            log.LogHeader
	LockFile       *os.File
	Proc           proc.ProcHeader
	ChSig          chan os.Signal
	ChInfo         chan int
	PeerStats      map[int]int64 //peer [procid]->heart_beat_ts
	TickPool       *TickPool
	ServerCfg      interface{} //server *config if assigend
	PProf          ProfileConfig
	ReportCmdToken int64 //if exe report cmd
	ReportCmd      string
}

func InitCommConfig

func InitCommConfig(log_file string, name_space string, proc_id int) *CommConfig

type HeadTable

type HeadTable struct {
	ReloadList []string `json:"reload_list"`
}

type ProfileConfig

type ProfileConfig struct {
	Stat bool //false:closed true:open
	// contains filtered or unexported fields
}

type RedisCallBack

type RedisCallBack func(pconfig *CommConfig, result interface{}, cb_arg []interface{})

call back

type RedisClient

type RedisClient struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewRedisClient

func NewRedisClient(pconfig *CommConfig, redis_addr string, auth string, max_conn, normal_conn int) *RedisClient

New RedisClient

func (*RedisClient) AllocSyncCmdHead

func (pclient *RedisClient) AllocSyncCmdHead() *SyncCmdHead

Alloc Synchronise Cmd Head Warning: 1.This Method Must Be Put in an independent go-routine or will block main process 2.Must Release Head After using head

func (*RedisClient) Close

func (pclient *RedisClient) Close()

func (*RedisClient) FreeSyncCmdHead

func (pclient *RedisClient) FreeSyncCmdHead(phead *SyncCmdHead)

Release Synchronise Cmd Head

func (*RedisClient) GetConnNum

func (pclient *RedisClient) GetConnNum() int

func (*RedisClient) RedisExeCmd

func (pclient *RedisClient) RedisExeCmd(pconfig *CommConfig, cb_func RedisCallBack, cb_arg []interface{}, cmd string, arg ...interface{})

redis exe cmd Asynchronously

func (*RedisClient) RedisExeCmdSync

func (pclient *RedisClient) RedisExeCmdSync(phead *SyncCmdHead, cmd string, arg ...interface{}) (interface{}, error)

redis exe cmd synchronised

func (*RedisClient) Reset

func (pclient *RedisClient) Reset(redis_addr string, auth string, max_conn int, normal_conn int)

Reset Addr * @redis_addr:new redis addr; "" means no use * @auth:new redis auth. "" means no use * @max_conn:new max conn. <=0 means no use;(ATTENTION: max_conn only support expand,and <=HARD_MAX_CONN_LIMIT) * @normal_conn: new normal conn. <=0 means no use

type ReportHead

type ReportHead struct {
	ProtoId  int         `json:"proto"`
	ProcId   int         `json:"proc_id"`
	IntValue int64       `json:"intv"` //for normal int value
	StrValue string      `json:"strv"` //for normal string value
	Sub      interface{} `json:"-"`    //for complex info
}

type ReportMsg

type ReportMsg struct {
	ProtoId  int         `json:"proto"`
	ProcId   int         `json:"proc_id"`
	IntValue int64       `json:"intv"` //for normal int value
	StrValue string      `json:"strv"` //for normal string value
	Sub      interface{} `json:sub`    //for complex info
}

report msg

type ReportServ

type ReportServ struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func StartReport

func StartReport(pconfig *CommConfig, proc_id int, proc_name string, manage_addr []string, method int8, monitor_inv int) *ReportServ

Start a Report Go-Routine * @proc_id process proc id * @proc_name process proc_name * @manage_addr manage servers addr, it could be many servers. addr like: "ip:port" * @moniter_inv monitor interval of monitor goroutine. -1:no monitor =0:no sleep(at least 1 second sleep) >0 sleep seconds

func (*ReportServ) Close

func (pserv *ReportServ) Close()

func (*ReportServ) Recv

func (pserv *ReportServ) Recv() *ReportMsg

func (*ReportServ) Report

func (pserv *ReportServ) Report(proto int, v_int int64, v_str string, v_msg interface{}) bool

* Report Msg To Manager * @proto:Refer REPORT_PROTO_XX * @v_msg: used for complex information. should be defined in report_proto.go

func (*ReportServ) SetMonitor

func (pserv *ReportServ) SetMonitor(interval int)

set monitor interval

type SyncCmdHead

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

redis exe cmd using synchronized

type SyncServerMsg

type SyncServerMsg struct {
	StartTime int64
}

-----------------------------------SubMsg--------------------

type TableMap

type TableMap map[string]interface{}

type TcpServ

type TcpServ struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func StartTcpServ

func StartTcpServ(pconfig *CommConfig, addr string, max_conn int, valid_conn int8, enc_type int8, rsa_pub_key []byte,
	rsa_pri_key []byte) *TcpServ

new tcp_serv and start listener serve rsa_pub_key & rsa_pri_key is needed when enc_type==NET_ENCRYPT_RSA

func (*TcpServ) Close

func (pserv *TcpServ) Close(pconfig *CommConfig)

close tcp serv

func (*TcpServ) GetConnNum

func (pserv *TcpServ) GetConnNum() int

func (*TcpServ) Recv

func (pserv *TcpServ) Recv(pconfig *CommConfig, pkgs []*ClientPkg) int

read from clients @return: nil:read fail or empty; []*ClientPkg read success

func (*TcpServ) Send

func (pserv *TcpServ) Send(pconfig *CommConfig, ppkg *ClientPkg) int

Send pkg to client @return: -1: failed 0:success

func (*TcpServ) SetMaxConn

func (pserv *TcpServ) SetMaxConn(max_conn int)

type TickFunc

type TickFunc func(arg interface{})

type TickPool

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

func NewTickPool

func NewTickPool(pconfig *CommConfig) *TickPool

create tick pool

func (*TickPool) AddTicker

func (pool *TickPool) AddTicker(name string, ctype int8, start_ms int64, period_ms int64, callback TickFunc, arg interface{}) bool
add ticker

@ctype:ticker type refer TICKER_TYPE_XX @start_ms:started ms. <=0:started from current >0 futrue timestamp @period_ms:interval timeing @callback:trig function if not nil @arg: arg for callback func if not nil

func (*TickPool) Tick

func (pool *TickPool) Tick(sleep_us int64)

tick *@sleep_us if>0 sleep xx microseconds else no sleep

type TinyNewFunc

type TinyNewFunc func() interface{}

type TinyPool

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

func NewTinyPool

func NewTinyPool(pool_size int, new_func TinyNewFunc) *TinyPool

New TinyPool

func (*TinyPool) Get

func (pool *TinyPool) Get() interface{}

Get an obj

func (*TinyPool) Put

func (pool *TinyPool) Put(v interface{})

Put an obj

Jump to

Keyboard shortcuts

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