Compare commits

...

10 Commits

Author SHA1 Message Date
orgin
7f93aa5ff9 新增对连续buff内存中指定索引位打标记函数 2022-09-27 16:49:11 +08:00
orgin
7a8d312aeb 修复WSService服务在特定服务安装顺序下设置MessageType不成功的问题 2022-09-23 10:49:44 +08:00
orgin
f931f61f7b 新增编译标记以及编译操作系统类型 2022-09-22 10:54:27 +08:00
orgin
151ed123f4 优化WSClient的MessageType 2022-09-22 10:50:24 +08:00
orgin
5a6a4c8a0d 新增编译标记以及编译操作系统类型 2022-09-22 10:37:27 +08:00
orgin
280c04a5d7 优化二分查找算法 2022-09-08 09:18:18 +08:00
orgin
1520dae223 替换ioutil包为os/io包,它在1.16开始被弃用 2022-08-17 14:28:01 +08:00
orgin
84f3429564 新增加、减、乘的运算函数 2022-08-08 15:28:21 +08:00
orgin
89fd5d273b 优化自恢复开协程函数GoRecover 2022-07-15 20:25:51 +08:00
orgin
3ce873ef04 优化获取NodeId接口 2022-07-11 15:34:28 +08:00
16 changed files with 367 additions and 185 deletions

View File

@@ -465,7 +465,7 @@ func GetNodeByServiceName(serviceName string) map[int]struct{} {
return nil return nil
} }
var mapNodeId map[int]struct{} mapNodeId := map[int]struct{}{}
for nodeId,_ := range mapNode { for nodeId,_ := range mapNode {
mapNodeId[nodeId] = struct{}{} mapNodeId[nodeId] = struct{}{}
} }

View File

@@ -5,7 +5,7 @@ import (
"github.com/duanhf2012/origin/log" "github.com/duanhf2012/origin/log"
"github.com/duanhf2012/origin/rpc" "github.com/duanhf2012/origin/rpc"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"io/ioutil" "os"
"strings" "strings"
) )
@@ -18,7 +18,7 @@ type NodeInfoList struct {
func (cls *Cluster) ReadClusterConfig(filepath string) (*NodeInfoList, error) { func (cls *Cluster) ReadClusterConfig(filepath string) (*NodeInfoList, error) {
c := &NodeInfoList{} c := &NodeInfoList{}
d, err := ioutil.ReadFile(filepath) d, err := os.ReadFile(filepath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -33,7 +33,7 @@ func (cls *Cluster) ReadClusterConfig(filepath string) (*NodeInfoList, error) {
func (cls *Cluster) readServiceConfig(filepath string) (interface{}, map[string]interface{}, map[int]map[string]interface{}, error) { func (cls *Cluster) readServiceConfig(filepath string) (interface{}, map[string]interface{}, map[int]map[string]interface{}, error) {
c := map[string]interface{}{} c := map[string]interface{}{}
//读取配置 //读取配置
d, err := ioutil.ReadFile(filepath) d, err := os.ReadFile(filepath)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
@@ -69,7 +69,7 @@ func (cls *Cluster) readLocalClusterConfig(nodeId int) ([]NodeInfo, []NodeInfo,
var nodeInfoList []NodeInfo var nodeInfoList []NodeInfo
var masterDiscoverNodeList []NodeInfo var masterDiscoverNodeList []NodeInfo
clusterCfgPath := strings.TrimRight(configDir, "/") + "/cluster" clusterCfgPath := strings.TrimRight(configDir, "/") + "/cluster"
fileInfoList, err := ioutil.ReadDir(clusterCfgPath) fileInfoList, err := os.ReadDir(clusterCfgPath)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("Read dir %s is fail :%+v", clusterCfgPath, err) return nil, nil, fmt.Errorf("Read dir %s is fail :%+v", clusterCfgPath, err)
} }
@@ -111,7 +111,7 @@ func (cls *Cluster) readLocalClusterConfig(nodeId int) ([]NodeInfo, []NodeInfo,
func (cls *Cluster) readLocalService(localNodeId int) error { func (cls *Cluster) readLocalService(localNodeId int) error {
clusterCfgPath := strings.TrimRight(configDir, "/") + "/cluster" clusterCfgPath := strings.TrimRight(configDir, "/") + "/cluster"
fileInfoList, err := ioutil.ReadDir(clusterCfgPath) fileInfoList, err := os.ReadDir(clusterCfgPath)
if err != nil { if err != nil {
return fmt.Errorf("Read dir %s is fail :%+v", clusterCfgPath, err) return fmt.Errorf("Read dir %s is fail :%+v", clusterCfgPath, err)
} }

View File

@@ -14,6 +14,7 @@ type WSClient struct {
ConnectInterval time.Duration ConnectInterval time.Duration
PendingWriteNum int PendingWriteNum int
MaxMsgLen uint32 MaxMsgLen uint32
MessageType int
HandshakeTimeout time.Duration HandshakeTimeout time.Duration
AutoReconnect bool AutoReconnect bool
NewAgent func(*WSConn) Agent NewAgent func(*WSConn) Agent
@@ -21,7 +22,7 @@ type WSClient struct {
cons WebsocketConnSet cons WebsocketConnSet
wg sync.WaitGroup wg sync.WaitGroup
closeFlag bool closeFlag bool
messageType int
} }
func (client *WSClient) Start() { func (client *WSClient) Start() {
@@ -63,7 +64,11 @@ func (client *WSClient) init() {
if client.cons != nil { if client.cons != nil {
log.SFatal("client is running") log.SFatal("client is running")
} }
client.messageType = websocket.TextMessage
if client.MessageType == 0 {
client.MessageType = websocket.TextMessage
}
client.cons = make(WebsocketConnSet) client.cons = make(WebsocketConnSet)
client.closeFlag = false client.closeFlag = false
client.dialer = websocket.Dialer{ client.dialer = websocket.Dialer{
@@ -84,9 +89,6 @@ func (client *WSClient) dial() *websocket.Conn {
} }
} }
func (client *WSClient) SetMessageType(messageType int){
client.messageType = messageType
}
func (client *WSClient) connect() { func (client *WSClient) connect() {
defer client.wg.Done() defer client.wg.Done()
@@ -106,7 +108,7 @@ reconnect:
client.cons[conn] = struct{}{} client.cons[conn] = struct{}{}
client.Unlock() client.Unlock()
wsConn := newWSConn(conn, client.PendingWriteNum, client.MaxMsgLen,client.messageType) wsConn := newWSConn(conn, client.PendingWriteNum, client.MaxMsgLen,client.MessageType)
agent := client.NewAgent(wsConn) agent := client.NewAgent(wsConn)
agent.Run() agent.Run()

View File

@@ -139,6 +139,7 @@ func (server *WSServer) Start() {
maxMsgLen: server.MaxMsgLen, maxMsgLen: server.MaxMsgLen,
newAgent: server.NewAgent, newAgent: server.NewAgent,
conns: make(WebsocketConnSet), conns: make(WebsocketConnSet),
messageType:server.messageType,
upgrader: websocket.Upgrader{ upgrader: websocket.Upgrader{
HandshakeTimeout: server.HTTPTimeout, HandshakeTimeout: server.HTTPTimeout,
CheckOrigin: func(_ *http.Request) bool { return true }, CheckOrigin: func(_ *http.Request) bool { return true },

View File

@@ -8,9 +8,9 @@ import (
"github.com/duanhf2012/origin/log" "github.com/duanhf2012/origin/log"
"github.com/duanhf2012/origin/profiler" "github.com/duanhf2012/origin/profiler"
"github.com/duanhf2012/origin/service" "github.com/duanhf2012/origin/service"
"github.com/duanhf2012/origin/util/timer"
"github.com/duanhf2012/origin/util/buildtime" "github.com/duanhf2012/origin/util/buildtime"
"io/ioutil" "github.com/duanhf2012/origin/util/timer"
"io"
slog "log" slog "log"
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
@@ -31,6 +31,13 @@ var bValid bool
var configDir = "./config/" var configDir = "./config/"
var logLevel string = "debug" var logLevel string = "debug"
var logPath string var logPath string
type BuildOSType = int8
const(
Windows BuildOSType = 0
Linux BuildOSType = 1
Mac BuildOSType = 2
)
func init() { func init() {
@@ -107,7 +114,7 @@ func getRunProcessPid(nodeId int) (int,error) {
return 0, err return 0, err
} }
pidByte,errs := ioutil.ReadAll(f) pidByte, errs := io.ReadAll(f)
if errs != nil { if errs != nil {
return 0, errs return 0, errs
} }
@@ -274,7 +281,6 @@ func startNode(args interface{}) error{
return nil return nil
} }
func Setup(s ...service.IService) { func Setup(s ...service.IService) {
for _, sv := range s { for _, sv := range s {
sv.OnSetup(sv) sv.OnSetup(sv)

View File

@@ -15,3 +15,7 @@ func KillProcess(processId int){
fmt.Printf("kill processid %d is successful.\n",processId) fmt.Printf("kill processid %d is successful.\n",processId)
} }
} }
func GetBuildOSType() BuildOSType{
return Linux
}

View File

@@ -15,3 +15,7 @@ func KillProcess(processId int){
fmt.Printf("kill processid %d is successful.\n",processId) fmt.Printf("kill processid %d is successful.\n",processId)
} }
} }
func GetBuildOSType() BuildOSType{
return Mac
}

View File

@@ -5,3 +5,7 @@ package node
func KillProcess(processId int){ func KillProcess(processId int){
} }
func GetBuildOSType() BuildOSType{
return Windows
}

View File

@@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@@ -103,7 +103,7 @@ func (m *HttpClientModule) Request(method string, url string, body []byte, heade
} }
defer rsp.Body.Close() defer rsp.Body.Close()
ret.Body, err = ioutil.ReadAll(rsp.Body) ret.Body, err = io.ReadAll(rsp.Body)
if err != nil { if err != nil {
ret.Err = err ret.Err = err
return ret return ret

View File

@@ -8,7 +8,6 @@ import (
"github.com/duanhf2012/origin/util/uuid" "github.com/duanhf2012/origin/util/uuid"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@@ -85,7 +84,6 @@ type HttpSession struct {
sessionDone chan *HttpSession sessionDone chan *HttpSession
} }
type HttpService struct { type HttpService struct {
service.Service service.Service
@@ -496,7 +494,7 @@ func (httpService *HttpService) ServeHTTP(w http.ResponseWriter, r *http.Request
session.w = w session.w = w
defer r.Body.Close() defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
session.WriteStatusCode(http.StatusGatewayTimeout) session.WriteStatusCode(http.StatusGatewayTimeout)
session.flush() session.flush()

View File

@@ -10,7 +10,7 @@ type Element[ValueType NumberType] interface {
} }
//BiSearch 二分查找,切片必需有序号。matchUp表示是否向上范围查找。比如数列10 20 30 当value传入25时返回结果是2,表示落到3的范围 //BiSearch 二分查找,切片必需有序号。matchUp表示是否向上范围查找。比如数列10 20 30 当value传入25时返回结果是2,表示落到3的范围
func BiSearch[ValueType NumberType, T Element[ValueType]](sElement []T, value ValueType, matchUp bool) int { func BiSearch[ValueType NumberType, T Element[ValueType]](sElement []T, value ValueType, matchUp int) int {
low, high := 0, len(sElement)-1 low, high := 0, len(sElement)-1
if high == -1 { if high == -1 {
return -1 return -1
@@ -28,12 +28,29 @@ func BiSearch[ValueType NumberType, T Element[ValueType]](sElement []T, value Va
} }
} }
if matchUp == true { switch matchUp {
case 1:
if (sElement[mid].GetValue()) < value && if (sElement[mid].GetValue()) < value &&
(mid+1 < len(sElement)-1) { (mid+1 < len(sElement)-1) {
return mid + 1 return mid + 1
} }
return mid return mid
case -1:
if (sElement[mid].GetValue()) > value {
if mid - 1 < 0 {
return -1
} else {
return mid - 1
}
} else if (sElement[mid].GetValue()) < value {
if (mid+1 < len(sElement)-1) {
return mid + 1
} else {
return mid
}
} else {
return mid
}
} }
return -1 return -1

View File

@@ -0,0 +1,61 @@
package algorithms
import (
"errors"
"unsafe"
)
type BitNumber interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr
}
type UnsignedNumber interface {
uint | uint8 | uint16 | uint32 | uint64 | uintptr
}
func getBitTagIndex[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number, bitPositionIndex UNumber) (uintptr, uintptr, bool) {
sliceIndex := uintptr(bitPositionIndex) / (8 * unsafe.Sizeof(bitBuff[0]))
sliceBitIndex := uintptr(bitPositionIndex) % (8 * unsafe.Sizeof(bitBuff[0]))
//位index不能越界
if uintptr(bitPositionIndex) >= uintptr(len(bitBuff))*unsafe.Sizeof(bitBuff[0])*8 {
return 0, 0, false
}
return sliceIndex, sliceBitIndex, true
}
func setBitTagByIndex[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number, bitPositionIndex UNumber, setTag bool) bool {
sliceIndex, sliceBitIndex, ret := getBitTagIndex(bitBuff, bitPositionIndex)
if ret == false {
return ret
}
if setTag {
bitBuff[sliceIndex] = bitBuff[sliceIndex] | 1<<sliceBitIndex
} else {
bitBuff[sliceIndex] = bitBuff[sliceIndex] &^ (1 << sliceBitIndex)
}
return true
}
func GetBitwiseTag[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number, bitPositionIndex UNumber) (bool, error) {
sliceIndex, sliceBitIndex, ret := getBitTagIndex(bitBuff, bitPositionIndex)
if ret == false {
return false, errors.New("Invalid parameter")
}
return (bitBuff[sliceIndex] & (1 << sliceBitIndex)) > 0, nil
}
func SetBitwiseTag[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number, bitPositionIndex UNumber) bool {
return setBitTagByIndex(bitBuff, bitPositionIndex, true)
}
func ClearBitwiseTag[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number, bitPositionIndex UNumber) bool {
return setBitTagByIndex(bitBuff, bitPositionIndex, false)
}
func GetBitwiseNum[Number BitNumber](bitBuff []Number) int {
return len(bitBuff) * int(unsafe.Sizeof(bitBuff[0])*8)
}

View File

@@ -0,0 +1,37 @@
package algorithms
import "testing"
func Test_Bitwise(t *testing.T) {
//1.预分配10个byte切片用于存储位标识
byteBuff := make([]byte, 10)
//2.获取buff总共位数
bitNum := GetBitwiseNum(byteBuff)
t.Log(bitNum)
//3..对索引79位打标记注意是从0开始79即为最后一个位
idx := uint(79)
//4.对byteBuff索引idx位置打上标记
SetBitwiseTag(byteBuff, idx)
//5.获取索引idx位置标记
isTag, ret := GetBitwiseTag(byteBuff, idx)
t.Log("set index ", idx, " :", isTag, ret)
if isTag != true {
t.Fatal("error")
}
//6.清除掉索引idx位标记
ClearBitwiseTag(byteBuff, idx)
//7.获取索引idx位置标记
isTag, ret = GetBitwiseTag(byteBuff, idx)
t.Log("get index ", idx, " :", isTag, ret)
if isTag != false {
t.Fatal("error")
}
}

View File

@@ -6,10 +6,15 @@ go tool nm ./originserver.exe |grep buildtime
//编译传入编译时间信息 //编译传入编译时间信息
go build -ldflags "-X 'github.com/duanhf2012/origin/util/buildtime.BuildTime=20200101'" go build -ldflags "-X 'github.com/duanhf2012/origin/util/buildtime.BuildTime=20200101'"
go build -ldflags "-X github.com/duanhf2012/origin/util/buildtime.BuildTime=20200101 -X github.com/duanhf2012/origin/util/buildtime.BuildTag=debug"
*/ */
var BuildTime string var BuildTime string
var BuildTag string
func GetBuildDateTime() string { func GetBuildDateTime() string {
return BuildTime return BuildTime
} }
func GetBuildTag() string {
return BuildTag
}

View File

@@ -2,6 +2,7 @@ package coroutine
import ( import (
"fmt" "fmt"
"github.com/duanhf2012/origin/log"
"reflect" "reflect"
"runtime/debug" "runtime/debug"
) )
@@ -12,10 +13,11 @@ func F(callback interface{},recoverNum int, args ...interface{}) {
var coreInfo string var coreInfo string
coreInfo = string(debug.Stack()) coreInfo = string(debug.Stack())
coreInfo += "\n" + fmt.Sprintf("Core information is %v\n", r) coreInfo += "\n" + fmt.Sprintf("Core information is %v\n", r)
fmt.Print(coreInfo) log.SError(coreInfo)
if recoverNum > 0{
if recoverNum==-1 ||recoverNum-1 >= 0 {
recoverNum -= 1 recoverNum -= 1
}
if recoverNum == -1 || recoverNum > 0 {
go F(callback,recoverNum, args...) go F(callback,recoverNum, args...)
} }
} }

View File

@@ -1,5 +1,7 @@
package math package math
import "github.com/duanhf2012/origin/log"
type NumberType interface { type NumberType interface {
int | int8 | int16 | int32 | int64 | float32 | float64 | uint | uint8 | uint16 | uint32 | uint64 int | int8 | int16 | int32 | int64 | float32 | float64 | uint | uint8 | uint16 | uint32 | uint64
} }
@@ -35,3 +37,42 @@ func Abs[NumType SignedNumberType](Num NumType) NumType {
return Num return Num
} }
func Add[NumType NumberType](number1 NumType, number2 NumType) NumType {
ret := number1 + number2
if number2> 0 && ret < number1 {
log.SStack("Calculation overflow , number1 is ",number1," number2 is ",number2)
}else if (number2<0 && ret > number1){
log.SStack("Calculation overflow , number1 is ",number1," number2 is ",number2)
}
return ret
}
func Sub[NumType NumberType](number1 NumType, number2 NumType) NumType {
ret := number1 - number2
if number2> 0 && ret > number1 {
log.SStack("Calculation overflow , number1 is ",number1," number2 is ",number2)
}else if (number2<0 && ret < number1){
log.SStack("Calculation overflow , number1 is ",number1," number2 is ",number2)
}
return ret
}
func Mul[NumType NumberType](number1 NumType, number2 NumType) NumType {
ret := number1 * number2
if number1 == 0 || number2 == 0 {
return ret
}
if ret / number2 == number1 {
return ret
}
log.SStack("Calculation overflow , number1 is ",number1," number2 is ",number2)
return ret
}