mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-07 17:24:41 +08:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7f93aa5ff9 | ||
|
|
7a8d312aeb | ||
|
|
f931f61f7b | ||
|
|
151ed123f4 | ||
|
|
5a6a4c8a0d | ||
|
|
280c04a5d7 | ||
|
|
1520dae223 | ||
|
|
84f3429564 | ||
|
|
89fd5d273b | ||
|
|
3ce873ef04 |
@@ -465,7 +465,7 @@ func GetNodeByServiceName(serviceName string) map[int]struct{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
var mapNodeId map[int]struct{}
|
||||
mapNodeId := map[int]struct{}{}
|
||||
for nodeId,_ := range mapNode {
|
||||
mapNodeId[nodeId] = struct{}{}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"github.com/duanhf2012/origin/log"
|
||||
"github.com/duanhf2012/origin/rpc"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -18,7 +18,7 @@ type NodeInfoList struct {
|
||||
|
||||
func (cls *Cluster) ReadClusterConfig(filepath string) (*NodeInfoList, error) {
|
||||
c := &NodeInfoList{}
|
||||
d, err := ioutil.ReadFile(filepath)
|
||||
d, err := os.ReadFile(filepath)
|
||||
if err != nil {
|
||||
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) {
|
||||
c := map[string]interface{}{}
|
||||
//读取配置
|
||||
d, err := ioutil.ReadFile(filepath)
|
||||
d, err := os.ReadFile(filepath)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
@@ -69,7 +69,7 @@ func (cls *Cluster) readLocalClusterConfig(nodeId int) ([]NodeInfo, []NodeInfo,
|
||||
var nodeInfoList []NodeInfo
|
||||
var masterDiscoverNodeList []NodeInfo
|
||||
clusterCfgPath := strings.TrimRight(configDir, "/") + "/cluster"
|
||||
fileInfoList, err := ioutil.ReadDir(clusterCfgPath)
|
||||
fileInfoList, err := os.ReadDir(clusterCfgPath)
|
||||
if err != nil {
|
||||
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 {
|
||||
clusterCfgPath := strings.TrimRight(configDir, "/") + "/cluster"
|
||||
fileInfoList, err := ioutil.ReadDir(clusterCfgPath)
|
||||
fileInfoList, err := os.ReadDir(clusterCfgPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Read dir %s is fail :%+v", clusterCfgPath, err)
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ type WSClient struct {
|
||||
ConnectInterval time.Duration
|
||||
PendingWriteNum int
|
||||
MaxMsgLen uint32
|
||||
MessageType int
|
||||
HandshakeTimeout time.Duration
|
||||
AutoReconnect bool
|
||||
NewAgent func(*WSConn) Agent
|
||||
@@ -21,7 +22,7 @@ type WSClient struct {
|
||||
cons WebsocketConnSet
|
||||
wg sync.WaitGroup
|
||||
closeFlag bool
|
||||
messageType int
|
||||
|
||||
}
|
||||
|
||||
func (client *WSClient) Start() {
|
||||
@@ -63,7 +64,11 @@ func (client *WSClient) init() {
|
||||
if client.cons != nil {
|
||||
log.SFatal("client is running")
|
||||
}
|
||||
client.messageType = websocket.TextMessage
|
||||
|
||||
if client.MessageType == 0 {
|
||||
client.MessageType = websocket.TextMessage
|
||||
}
|
||||
|
||||
client.cons = make(WebsocketConnSet)
|
||||
client.closeFlag = false
|
||||
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() {
|
||||
defer client.wg.Done()
|
||||
|
||||
@@ -106,7 +108,7 @@ reconnect:
|
||||
client.cons[conn] = struct{}{}
|
||||
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.Run()
|
||||
|
||||
|
||||
@@ -139,6 +139,7 @@ func (server *WSServer) Start() {
|
||||
maxMsgLen: server.MaxMsgLen,
|
||||
newAgent: server.NewAgent,
|
||||
conns: make(WebsocketConnSet),
|
||||
messageType:server.messageType,
|
||||
upgrader: websocket.Upgrader{
|
||||
HandshakeTimeout: server.HTTPTimeout,
|
||||
CheckOrigin: func(_ *http.Request) bool { return true },
|
||||
|
||||
160
node/node.go
160
node/node.go
@@ -8,9 +8,9 @@ import (
|
||||
"github.com/duanhf2012/origin/log"
|
||||
"github.com/duanhf2012/origin/profiler"
|
||||
"github.com/duanhf2012/origin/service"
|
||||
"github.com/duanhf2012/origin/util/timer"
|
||||
"github.com/duanhf2012/origin/util/buildtime"
|
||||
"io/ioutil"
|
||||
"github.com/duanhf2012/origin/util/timer"
|
||||
"io"
|
||||
slog "log"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
@@ -31,33 +31,40 @@ var bValid bool
|
||||
var configDir = "./config/"
|
||||
var logLevel string = "debug"
|
||||
var logPath string
|
||||
type BuildOSType = int8
|
||||
|
||||
const(
|
||||
Windows BuildOSType = 0
|
||||
Linux BuildOSType = 1
|
||||
Mac BuildOSType = 2
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
closeSig = make(chan bool,1)
|
||||
closeSig = make(chan bool, 1)
|
||||
sig = make(chan os.Signal, 3)
|
||||
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM,syscall.Signal(10))
|
||||
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.Signal(10))
|
||||
|
||||
console.RegisterCommandBool("help",false,"<-help> This help.",usage)
|
||||
console.RegisterCommandString("name","","<-name nodeName> Node's name.",setName)
|
||||
console.RegisterCommandString("start","","<-start nodeid=nodeid> Run originserver.",startNode)
|
||||
console.RegisterCommandString("stop","","<-stop nodeid=nodeid> Stop originserver process.",stopNode)
|
||||
console.RegisterCommandString("config","","<-config path> Configuration file path.",setConfigPath)
|
||||
console.RegisterCommandBool("help", false, "<-help> This help.", usage)
|
||||
console.RegisterCommandString("name", "", "<-name nodeName> Node's name.", setName)
|
||||
console.RegisterCommandString("start", "", "<-start nodeid=nodeid> Run originserver.", startNode)
|
||||
console.RegisterCommandString("stop", "", "<-stop nodeid=nodeid> Stop originserver process.", stopNode)
|
||||
console.RegisterCommandString("config", "", "<-config path> Configuration file path.", setConfigPath)
|
||||
console.RegisterCommandString("console", "", "<-console true|false> Turn on or off screen log output.", openConsole)
|
||||
console.RegisterCommandString("loglevel", "debug", "<-loglevel debug|release|warning|error|fatal> Set loglevel.", setLevel)
|
||||
console.RegisterCommandString("logpath", "", "<-logpath path> Set log file path.", setLogPath)
|
||||
console.RegisterCommandString("pprof","","<-pprof ip:port> Open performance analysis.",setPprof)
|
||||
console.RegisterCommandString("pprof", "", "<-pprof ip:port> Open performance analysis.", setPprof)
|
||||
}
|
||||
|
||||
func usage(val interface{}) error{
|
||||
func usage(val interface{}) error {
|
||||
ret := val.(bool)
|
||||
if ret == false {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(buildtime.GetBuildDateTime())>0 {
|
||||
fmt.Fprintf(os.Stderr, "Welcome to Origin(build info: %s)\nUsage: originserver [-help] [-start node=1] [-stop] [-config path] [-pprof 0.0.0.0:6060]...\n",buildtime.GetBuildDateTime())
|
||||
}else{
|
||||
if len(buildtime.GetBuildDateTime()) > 0 {
|
||||
fmt.Fprintf(os.Stderr, "Welcome to Origin(build info: %s)\nUsage: originserver [-help] [-start node=1] [-stop] [-config path] [-pprof 0.0.0.0:6060]...\n", buildtime.GetBuildDateTime())
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Welcome to Origin\nUsage: originserver [-help] [-start node=1] [-stop] [-config path] [-pprof 0.0.0.0:6060]...\n")
|
||||
}
|
||||
|
||||
@@ -71,28 +78,28 @@ func setName(val interface{}) error {
|
||||
|
||||
func setPprof(val interface{}) error {
|
||||
listenAddr := val.(string)
|
||||
if listenAddr==""{
|
||||
if listenAddr == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
go func(){
|
||||
go func() {
|
||||
err := http.ListenAndServe(listenAddr, nil)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("%+v",err))
|
||||
panic(fmt.Errorf("%+v", err))
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setConfigPath(val interface{}) error{
|
||||
func setConfigPath(val interface{}) error {
|
||||
configPath := val.(string)
|
||||
if configPath==""{
|
||||
if configPath == "" {
|
||||
return nil
|
||||
}
|
||||
_, err := os.Stat(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cannot find file path %s",configPath)
|
||||
return fmt.Errorf("Cannot find file path %s", configPath)
|
||||
}
|
||||
|
||||
cluster.SetConfigDir(configPath)
|
||||
@@ -100,16 +107,16 @@ func setConfigPath(val interface{}) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func getRunProcessPid(nodeId int) (int,error) {
|
||||
f, err := os.OpenFile(fmt.Sprintf("%s_%d.pid",os.Args[0],nodeId), os.O_RDONLY, 0600)
|
||||
func getRunProcessPid(nodeId int) (int, error) {
|
||||
f, err := os.OpenFile(fmt.Sprintf("%s_%d.pid", os.Args[0], nodeId), os.O_RDONLY, 0600)
|
||||
defer f.Close()
|
||||
if err!= nil {
|
||||
return 0,err
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
pidByte,errs := ioutil.ReadAll(f)
|
||||
if errs!=nil {
|
||||
return 0,errs
|
||||
pidByte, errs := io.ReadAll(f)
|
||||
if errs != nil {
|
||||
return 0, errs
|
||||
}
|
||||
|
||||
return strconv.Atoi(string(pidByte))
|
||||
@@ -117,13 +124,13 @@ func getRunProcessPid(nodeId int) (int,error) {
|
||||
|
||||
func writeProcessPid(nodeId int) {
|
||||
//pid
|
||||
f, err := os.OpenFile(fmt.Sprintf("%s_%d.pid",os.Args[0],nodeId), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0600)
|
||||
f, err := os.OpenFile(fmt.Sprintf("%s_%d.pid", os.Args[0], nodeId), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0600)
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
os.Exit(-1)
|
||||
} else {
|
||||
_,err=f.Write([]byte(fmt.Sprintf("%d",os.Getpid())))
|
||||
_, err = f.Write([]byte(fmt.Sprintf("%d", os.Getpid())))
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
os.Exit(-1)
|
||||
@@ -135,28 +142,28 @@ func GetNodeId() int {
|
||||
return nodeId
|
||||
}
|
||||
|
||||
func initNode(id int){
|
||||
func initNode(id int) {
|
||||
//1.初始化集群
|
||||
nodeId = id
|
||||
err := cluster.GetCluster().Init(GetNodeId(),Setup)
|
||||
err := cluster.GetCluster().Init(GetNodeId(), Setup)
|
||||
if err != nil {
|
||||
log.SFatal("read system config is error ",err.Error())
|
||||
log.SFatal("read system config is error ", err.Error())
|
||||
}
|
||||
|
||||
err = initLog()
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//2.setup service
|
||||
for _,s := range preSetupService {
|
||||
for _, s := range preSetupService {
|
||||
//是否配置的service
|
||||
if cluster.GetCluster().IsConfigService(s.GetName()) == false {
|
||||
continue
|
||||
}
|
||||
|
||||
pServiceCfg := cluster.GetCluster().GetServiceCfg(s.GetName())
|
||||
s.Init(s,cluster.GetRpcClient,cluster.GetRpcServer,pServiceCfg)
|
||||
s.Init(s, cluster.GetRpcClient, cluster.GetRpcServer, pServiceCfg)
|
||||
|
||||
service.Setup(s)
|
||||
}
|
||||
@@ -165,14 +172,14 @@ func initNode(id int){
|
||||
service.Init(closeSig)
|
||||
}
|
||||
|
||||
func initLog() error{
|
||||
if logPath == ""{
|
||||
func initLog() error {
|
||||
if logPath == "" {
|
||||
setLogPath("./log")
|
||||
}
|
||||
|
||||
localnodeinfo := cluster.GetCluster().GetLocalNodeInfo()
|
||||
filepre := fmt.Sprintf("%s_%d_", localnodeinfo.NodeName, localnodeinfo.NodeId)
|
||||
logger,err := log.New(logLevel,logPath,filepre,slog.LstdFlags|slog.Lshortfile,10)
|
||||
logger, err := log.New(logLevel, logPath, filepre, slog.LstdFlags|slog.Lshortfile, 10)
|
||||
if err != nil {
|
||||
fmt.Printf("cannot create log file!\n")
|
||||
return err
|
||||
@@ -183,8 +190,8 @@ func initLog() error{
|
||||
|
||||
func Start() {
|
||||
err := console.Run(os.Args)
|
||||
if err!=nil {
|
||||
fmt.Printf("%+v\n",err)
|
||||
if err != nil {
|
||||
fmt.Printf("%+v\n", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -196,19 +203,19 @@ func stopNode(args interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
sParam := strings.Split(param,"=")
|
||||
sParam := strings.Split(param, "=")
|
||||
if len(sParam) != 2 {
|
||||
return fmt.Errorf("invalid option %s",param)
|
||||
return fmt.Errorf("invalid option %s", param)
|
||||
}
|
||||
if sParam[0]!="nodeid" {
|
||||
return fmt.Errorf("invalid option %s",param)
|
||||
if sParam[0] != "nodeid" {
|
||||
return fmt.Errorf("invalid option %s", param)
|
||||
}
|
||||
nodeId,err:= strconv.Atoi(sParam[1])
|
||||
nodeId, err := strconv.Atoi(sParam[1])
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid option %s",param)
|
||||
return fmt.Errorf("invalid option %s", param)
|
||||
}
|
||||
|
||||
processId,err := getRunProcessPid(nodeId)
|
||||
processId, err := getRunProcessPid(nodeId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -217,26 +224,26 @@ func stopNode(args interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func startNode(args interface{}) error{
|
||||
func startNode(args interface{}) error {
|
||||
//1.解析参数
|
||||
param := args.(string)
|
||||
if param == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
sParam := strings.Split(param,"=")
|
||||
sParam := strings.Split(param, "=")
|
||||
if len(sParam) != 2 {
|
||||
return fmt.Errorf("invalid option %s",param)
|
||||
return fmt.Errorf("invalid option %s", param)
|
||||
}
|
||||
if sParam[0]!="nodeid" {
|
||||
return fmt.Errorf("invalid option %s",param)
|
||||
if sParam[0] != "nodeid" {
|
||||
return fmt.Errorf("invalid option %s", param)
|
||||
}
|
||||
nodeId,err:= strconv.Atoi(sParam[1])
|
||||
nodeId, err := strconv.Atoi(sParam[1])
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid option %s",param)
|
||||
return fmt.Errorf("invalid option %s", param)
|
||||
}
|
||||
|
||||
timer.StartTimer(10*time.Millisecond,1000000)
|
||||
timer.StartTimer(10*time.Millisecond, 1000000)
|
||||
log.SRelease("Start running server.")
|
||||
//2.初始化node
|
||||
initNode(nodeId)
|
||||
@@ -253,7 +260,7 @@ func startNode(args interface{}) error{
|
||||
//6.监听程序退出信号&性能报告
|
||||
bRun := true
|
||||
var pProfilerTicker *time.Ticker = &time.Ticker{}
|
||||
if profilerInterval>0 {
|
||||
if profilerInterval > 0 {
|
||||
pProfilerTicker = time.NewTicker(profilerInterval)
|
||||
}
|
||||
for bRun {
|
||||
@@ -261,7 +268,7 @@ func startNode(args interface{}) error{
|
||||
case <-sig:
|
||||
log.SRelease("receipt stop signal.")
|
||||
bRun = false
|
||||
case <- pProfilerTicker.C:
|
||||
case <-pProfilerTicker.C:
|
||||
profiler.Report()
|
||||
}
|
||||
}
|
||||
@@ -274,11 +281,10 @@ func startNode(args interface{}) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func Setup(s ...service.IService) {
|
||||
for _,sv := range s {
|
||||
func Setup(s ...service.IService) {
|
||||
for _, sv := range s {
|
||||
sv.OnSetup(sv)
|
||||
preSetupService = append(preSetupService,sv)
|
||||
preSetupService = append(preSetupService, sv)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,7 +292,7 @@ func GetService(serviceName string) service.IService {
|
||||
return service.GetService(serviceName)
|
||||
}
|
||||
|
||||
func SetConfigDir(configDir string){
|
||||
func SetConfigDir(configDir string) {
|
||||
configDir = configDir
|
||||
cluster.SetConfigDir(configDir)
|
||||
}
|
||||
@@ -295,58 +301,58 @@ func GetConfigDir() string {
|
||||
return configDir
|
||||
}
|
||||
|
||||
func SetSysLog(strLevel string, pathname string, flag int){
|
||||
logs,_:= log.New(strLevel,pathname, "", flag,10)
|
||||
func SetSysLog(strLevel string, pathname string, flag int) {
|
||||
logs, _ := log.New(strLevel, pathname, "", flag, 10)
|
||||
log.Export(logs)
|
||||
}
|
||||
|
||||
func OpenProfilerReport(interval time.Duration){
|
||||
func OpenProfilerReport(interval time.Duration) {
|
||||
profilerInterval = interval
|
||||
}
|
||||
|
||||
func openConsole(args interface{}) error{
|
||||
func openConsole(args interface{}) error {
|
||||
if args == "" {
|
||||
return nil
|
||||
}
|
||||
strOpen := strings.ToLower(strings.TrimSpace(args.(string)))
|
||||
if strOpen == "false" {
|
||||
log.OpenConsole = false
|
||||
}else if strOpen == "true" {
|
||||
} else if strOpen == "true" {
|
||||
log.OpenConsole = true
|
||||
}else{
|
||||
} else {
|
||||
return errors.New("Parameter console error!")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setLevel(args interface{}) error{
|
||||
if args==""{
|
||||
func setLevel(args interface{}) error {
|
||||
if args == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
logLevel = strings.TrimSpace(args.(string))
|
||||
if logLevel!= "debug" && logLevel!="release"&& logLevel!="warning"&&logLevel!="error"&&logLevel!="fatal" {
|
||||
if logLevel != "debug" && logLevel != "release" && logLevel != "warning" && logLevel != "error" && logLevel != "fatal" {
|
||||
return errors.New("unknown level: " + logLevel)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setLogPath(args interface{}) error{
|
||||
if args == ""{
|
||||
func setLogPath(args interface{}) error {
|
||||
if args == "" {
|
||||
return nil
|
||||
}
|
||||
logPath = strings.TrimSpace(args.(string))
|
||||
dir, err := os.Stat(logPath) //这个文件夹不存在
|
||||
if err == nil && dir.IsDir()==false {
|
||||
return errors.New("Not found dir "+logPath)
|
||||
if err == nil && dir.IsDir() == false {
|
||||
return errors.New("Not found dir " + logPath)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
err = os.Mkdir(logPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return errors.New("Cannot create dir "+logPath)
|
||||
return errors.New("Cannot create dir " + logPath)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,3 +15,7 @@ func KillProcess(processId int){
|
||||
fmt.Printf("kill processid %d is successful.\n",processId)
|
||||
}
|
||||
}
|
||||
|
||||
func GetBuildOSType() BuildOSType{
|
||||
return Linux
|
||||
}
|
||||
|
||||
@@ -15,3 +15,7 @@ func KillProcess(processId int){
|
||||
fmt.Printf("kill processid %d is successful.\n",processId)
|
||||
}
|
||||
}
|
||||
|
||||
func GetBuildOSType() BuildOSType{
|
||||
return Mac
|
||||
}
|
||||
|
||||
@@ -4,4 +4,8 @@ package node
|
||||
|
||||
func KillProcess(processId int){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func GetBuildOSType() BuildOSType{
|
||||
return Windows
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -64,7 +64,7 @@ func (m *HttpClientModule) Init(maxpool int, proxyUrl string) {
|
||||
Proxy: proxyFun,
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
Timeout: 5 * time.Second,
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ func (m *HttpClientModule) Request(method string, url string, body []byte, heade
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
|
||||
ret.Body, err = ioutil.ReadAll(rsp.Body)
|
||||
ret.Body, err = io.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
ret.Err = err
|
||||
return ret
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"github.com/duanhf2012/origin/util/uuid"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -17,13 +16,13 @@ import (
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
var DefaultReadTimeout time.Duration = time.Second*10
|
||||
var DefaultWriteTimeout time.Duration = time.Second*10
|
||||
var DefaultProcessTimeout time.Duration = time.Second*10
|
||||
var DefaultReadTimeout time.Duration = time.Second * 10
|
||||
var DefaultWriteTimeout time.Duration = time.Second * 10
|
||||
var DefaultProcessTimeout time.Duration = time.Second * 10
|
||||
|
||||
//http redirect
|
||||
type HttpRedirectData struct {
|
||||
Url string
|
||||
Url string
|
||||
CookieList []*http.Cookie
|
||||
}
|
||||
|
||||
@@ -44,7 +43,7 @@ type routerMatchData struct {
|
||||
}
|
||||
|
||||
type routerServeFileData struct {
|
||||
matchUrl string
|
||||
matchUrl string
|
||||
localPath string
|
||||
method HTTP_METHOD
|
||||
}
|
||||
@@ -56,44 +55,43 @@ type IHttpRouter interface {
|
||||
|
||||
SetServeFile(method HTTP_METHOD, urlpath string, dirname string) error
|
||||
SetFormFileKey(formFileKey string)
|
||||
GetFormFileKey()string
|
||||
GetFormFileKey() string
|
||||
AddHttpFiltrate(FiltrateFun HttpFiltrate) bool
|
||||
}
|
||||
|
||||
type HttpRouter struct {
|
||||
pathRouter map[HTTP_METHOD] map[string] routerMatchData //url地址,对应本service地址
|
||||
serveFileData map[string] *routerServeFileData
|
||||
httpFiltrateList [] HttpFiltrate
|
||||
pathRouter map[HTTP_METHOD]map[string]routerMatchData //url地址,对应本service地址
|
||||
serveFileData map[string]*routerServeFileData
|
||||
httpFiltrateList []HttpFiltrate
|
||||
|
||||
formFileKey string
|
||||
}
|
||||
|
||||
type HttpSession struct {
|
||||
httpRouter IHttpRouter
|
||||
r *http.Request
|
||||
w http.ResponseWriter
|
||||
r *http.Request
|
||||
w http.ResponseWriter
|
||||
|
||||
//parse result
|
||||
mapParam map[string]string
|
||||
body []byte
|
||||
body []byte
|
||||
|
||||
//processor result
|
||||
statusCode int
|
||||
msg []byte
|
||||
fileData *routerServeFileData
|
||||
statusCode int
|
||||
msg []byte
|
||||
fileData *routerServeFileData
|
||||
redirectData *HttpRedirectData
|
||||
sessionDone chan *HttpSession
|
||||
sessionDone chan *HttpSession
|
||||
}
|
||||
|
||||
|
||||
type HttpService struct {
|
||||
service.Service
|
||||
|
||||
httpServer network.HttpServer
|
||||
postAliasUrl map[HTTP_METHOD] map[string]routerMatchData //url地址,对应本service地址
|
||||
httpRouter IHttpRouter
|
||||
listenAddr string
|
||||
corsHeader *CORSHeader
|
||||
httpServer network.HttpServer
|
||||
postAliasUrl map[HTTP_METHOD]map[string]routerMatchData //url地址,对应本service地址
|
||||
httpRouter IHttpRouter
|
||||
listenAddr string
|
||||
corsHeader *CORSHeader
|
||||
processTimeout time.Duration
|
||||
}
|
||||
|
||||
@@ -109,11 +107,11 @@ func (httpService *HttpService) AddFiltrate(FiltrateFun HttpFiltrate) bool {
|
||||
|
||||
func NewHttpHttpRouter() IHttpRouter {
|
||||
httpRouter := &HttpRouter{}
|
||||
httpRouter.pathRouter =map[HTTP_METHOD] map[string] routerMatchData{}
|
||||
httpRouter.serveFileData = map[string] *routerServeFileData{}
|
||||
httpRouter.pathRouter = map[HTTP_METHOD]map[string]routerMatchData{}
|
||||
httpRouter.serveFileData = map[string]*routerServeFileData{}
|
||||
httpRouter.formFileKey = "file"
|
||||
for i:=METHOD_NONE+1;i<METHOD_INVALID;i++{
|
||||
httpRouter.pathRouter[i] = map[string] routerMatchData{}
|
||||
for i := METHOD_NONE + 1; i < METHOD_INVALID; i++ {
|
||||
httpRouter.pathRouter[i] = map[string]routerMatchData{}
|
||||
}
|
||||
|
||||
return httpRouter
|
||||
@@ -137,7 +135,7 @@ func (slf *HttpSession) Query(key string) (string, bool) {
|
||||
return ret, ok
|
||||
}
|
||||
|
||||
func (slf *HttpSession) GetBody() []byte{
|
||||
func (slf *HttpSession) GetBody() []byte {
|
||||
return slf.body
|
||||
}
|
||||
|
||||
@@ -145,19 +143,19 @@ func (slf *HttpSession) GetMethod() HTTP_METHOD {
|
||||
return slf.getMethod(slf.r.Method)
|
||||
}
|
||||
|
||||
func (slf *HttpSession) GetPath() string{
|
||||
return strings.Trim(slf.r.URL.Path,"/")
|
||||
func (slf *HttpSession) GetPath() string {
|
||||
return strings.Trim(slf.r.URL.Path, "/")
|
||||
}
|
||||
|
||||
func (slf *HttpSession) SetHeader(key, value string) {
|
||||
slf.w.Header().Set(key,value)
|
||||
slf.w.Header().Set(key, value)
|
||||
}
|
||||
|
||||
func (slf *HttpSession) AddHeader(key, value string) {
|
||||
slf.w.Header().Add(key,value)
|
||||
slf.w.Header().Add(key, value)
|
||||
}
|
||||
|
||||
func (slf *HttpSession) GetHeader(key string) string{
|
||||
func (slf *HttpSession) GetHeader(key string) string {
|
||||
return slf.r.Header.Get(key)
|
||||
}
|
||||
|
||||
@@ -165,7 +163,7 @@ func (slf *HttpSession) DelHeader(key string) {
|
||||
slf.r.Header.Del(key)
|
||||
}
|
||||
|
||||
func (slf *HttpSession) WriteStatusCode(statusCode int){
|
||||
func (slf *HttpSession) WriteStatusCode(statusCode int) {
|
||||
slf.statusCode = statusCode
|
||||
}
|
||||
|
||||
@@ -173,7 +171,7 @@ func (slf *HttpSession) Write(msg []byte) {
|
||||
slf.msg = msg
|
||||
}
|
||||
|
||||
func (slf *HttpSession) WriteJsonDone(statusCode int,msgJson interface{}) error {
|
||||
func (slf *HttpSession) WriteJsonDone(statusCode int, msgJson interface{}) error {
|
||||
msg, err := json.Marshal(msgJson)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -187,12 +185,12 @@ func (slf *HttpSession) WriteJsonDone(statusCode int,msgJson interface{}) error
|
||||
|
||||
func (slf *HttpSession) flush() {
|
||||
slf.w.WriteHeader(slf.statusCode)
|
||||
if slf.msg!=nil {
|
||||
if slf.msg != nil {
|
||||
slf.w.Write(slf.msg)
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *HttpSession) Done(){
|
||||
func (slf *HttpSession) Done() {
|
||||
slf.sessionDone <- slf
|
||||
}
|
||||
|
||||
@@ -219,15 +217,15 @@ func (slf *HttpRouter) analysisRouterUrl(url string) (string, error) {
|
||||
return strings.Trim(url, "/"), nil
|
||||
}
|
||||
|
||||
func (slf *HttpSession) Handle(){
|
||||
slf.httpRouter.Router(slf)
|
||||
func (slf *HttpSession) Handle() {
|
||||
slf.httpRouter.Router(slf)
|
||||
}
|
||||
|
||||
func (slf *HttpRouter) SetFormFileKey(formFileKey string){
|
||||
func (slf *HttpRouter) SetFormFileKey(formFileKey string) {
|
||||
slf.formFileKey = formFileKey
|
||||
}
|
||||
|
||||
func (slf *HttpRouter) GetFormFileKey()string{
|
||||
func (slf *HttpRouter) GetFormFileKey() string {
|
||||
return slf.formFileKey
|
||||
}
|
||||
|
||||
@@ -239,19 +237,19 @@ func (slf *HttpRouter) POST(url string, handle HttpHandle) bool {
|
||||
return slf.regRouter(METHOD_POST, url, handle)
|
||||
}
|
||||
|
||||
func (slf *HttpRouter) regRouter(method HTTP_METHOD, url string, handle HttpHandle) bool{
|
||||
mapRouter,ok := slf.pathRouter[method]
|
||||
if ok == false{
|
||||
func (slf *HttpRouter) regRouter(method HTTP_METHOD, url string, handle HttpHandle) bool {
|
||||
mapRouter, ok := slf.pathRouter[method]
|
||||
if ok == false {
|
||||
return false
|
||||
}
|
||||
|
||||
mapRouter[strings.Trim(url,"/")] = routerMatchData{httpHandle:handle}
|
||||
mapRouter[strings.Trim(url, "/")] = routerMatchData{httpHandle: handle}
|
||||
return true
|
||||
}
|
||||
|
||||
func (slf *HttpRouter) Router(session *HttpSession){
|
||||
if slf.httpFiltrateList!=nil {
|
||||
for _,fun := range slf.httpFiltrateList{
|
||||
func (slf *HttpRouter) Router(session *HttpSession) {
|
||||
if slf.httpFiltrateList != nil {
|
||||
for _, fun := range slf.httpFiltrateList {
|
||||
if fun(session) == false {
|
||||
//session.done()
|
||||
return
|
||||
@@ -288,13 +286,13 @@ func (slf *HttpRouter) Router(session *HttpSession){
|
||||
session.Done()
|
||||
}
|
||||
|
||||
func (httpService *HttpService) HttpEventHandler(ev event.IEvent) {
|
||||
func (httpService *HttpService) HttpEventHandler(ev event.IEvent) {
|
||||
ev.(*event.Event).Data.(*HttpSession).Handle()
|
||||
}
|
||||
|
||||
func (httpService *HttpService) SetHttpRouter(httpRouter IHttpRouter,eventHandler event.IEventHandler) {
|
||||
func (httpService *HttpService) SetHttpRouter(httpRouter IHttpRouter, eventHandler event.IEventHandler) {
|
||||
httpService.httpRouter = httpRouter
|
||||
httpService.RegEventReceiverFunc(event.Sys_Event_Http_Event,eventHandler, httpService.HttpEventHandler)
|
||||
httpService.RegEventReceiverFunc(event.Sys_Event_Http_Event, eventHandler, httpService.HttpEventHandler)
|
||||
}
|
||||
|
||||
func (slf *HttpRouter) SetServeFile(method HTTP_METHOD, urlpath string, dirname string) error {
|
||||
@@ -350,48 +348,48 @@ func (httpService *HttpService) OnInit() error {
|
||||
return fmt.Errorf("%s service config is error!", httpService.GetName())
|
||||
}
|
||||
tcpCfg := iConfig.(map[string]interface{})
|
||||
addr,ok := tcpCfg["ListenAddr"]
|
||||
addr, ok := tcpCfg["ListenAddr"]
|
||||
if ok == false {
|
||||
return fmt.Errorf("%s service config is error!", httpService.GetName())
|
||||
}
|
||||
var readTimeout time.Duration = DefaultReadTimeout
|
||||
var writeTimeout time.Duration = DefaultWriteTimeout
|
||||
|
||||
if cfgRead,ok := tcpCfg["ReadTimeout"];ok == true {
|
||||
readTimeout = time.Duration(cfgRead.(float64))*time.Millisecond
|
||||
if cfgRead, ok := tcpCfg["ReadTimeout"]; ok == true {
|
||||
readTimeout = time.Duration(cfgRead.(float64)) * time.Millisecond
|
||||
}
|
||||
|
||||
if cfgWrite,ok := tcpCfg["WriteTimeout"];ok == true {
|
||||
writeTimeout = time.Duration(cfgWrite.(float64))*time.Millisecond
|
||||
if cfgWrite, ok := tcpCfg["WriteTimeout"]; ok == true {
|
||||
writeTimeout = time.Duration(cfgWrite.(float64)) * time.Millisecond
|
||||
}
|
||||
|
||||
httpService.processTimeout = DefaultProcessTimeout
|
||||
if cfgProcessTimeout,ok := tcpCfg["ProcessTimeout"];ok == true {
|
||||
httpService.processTimeout = time.Duration(cfgProcessTimeout.(float64))*time.Millisecond
|
||||
if cfgProcessTimeout, ok := tcpCfg["ProcessTimeout"]; ok == true {
|
||||
httpService.processTimeout = time.Duration(cfgProcessTimeout.(float64)) * time.Millisecond
|
||||
}
|
||||
|
||||
httpService.httpServer.Init(addr.(string), httpService, readTimeout, writeTimeout)
|
||||
//Set CAFile
|
||||
caFileList,ok := tcpCfg["CAFile"]
|
||||
caFileList, ok := tcpCfg["CAFile"]
|
||||
if ok == false {
|
||||
return nil
|
||||
}
|
||||
iCaList := caFileList.([]interface{})
|
||||
var caFile [] network.CAFile
|
||||
for _,i := range iCaList {
|
||||
var caFile []network.CAFile
|
||||
for _, i := range iCaList {
|
||||
mapCAFile := i.(map[string]interface{})
|
||||
c,ok := mapCAFile["Certfile"]
|
||||
if ok == false{
|
||||
c, ok := mapCAFile["Certfile"]
|
||||
if ok == false {
|
||||
continue
|
||||
}
|
||||
k,ok := mapCAFile["Keyfile"]
|
||||
if ok == false{
|
||||
k, ok := mapCAFile["Keyfile"]
|
||||
if ok == false {
|
||||
continue
|
||||
}
|
||||
|
||||
if c.(string)!="" && k.(string)!="" {
|
||||
caFile = append(caFile,network.CAFile{
|
||||
CertFile: c.(string),
|
||||
if c.(string) != "" && k.(string) != "" {
|
||||
caFile = append(caFile, network.CAFile{
|
||||
CertFile: c.(string),
|
||||
Keyfile: k.(string),
|
||||
})
|
||||
}
|
||||
@@ -405,12 +403,12 @@ func (httpService *HttpService) SetAllowCORS(corsHeader *CORSHeader) {
|
||||
httpService.corsHeader = corsHeader
|
||||
}
|
||||
|
||||
func (httpService *HttpService) ProcessFile(session *HttpSession){
|
||||
func (httpService *HttpService) ProcessFile(session *HttpSession) {
|
||||
uPath := session.r.URL.Path
|
||||
idx := strings.Index(uPath, session.fileData.matchUrl)
|
||||
subPath := strings.Trim(uPath[idx+len(session.fileData.matchUrl):], "/")
|
||||
|
||||
destLocalPath := session.fileData.localPath + "/"+subPath
|
||||
destLocalPath := session.fileData.localPath + "/" + subPath
|
||||
|
||||
switch session.GetMethod() {
|
||||
case METHOD_GET:
|
||||
@@ -454,29 +452,29 @@ func (httpService *HttpService) ProcessFile(session *HttpSession){
|
||||
defer localFd.Close()
|
||||
io.Copy(localFd, resourceFile)
|
||||
session.WriteStatusCode(http.StatusOK)
|
||||
session.Write([]byte(uPath+"/"+fileName))
|
||||
session.Write([]byte(uPath + "/" + fileName))
|
||||
session.flush()
|
||||
}
|
||||
}
|
||||
|
||||
func NewAllowCORSHeader() *CORSHeader{
|
||||
func NewAllowCORSHeader() *CORSHeader {
|
||||
header := &CORSHeader{}
|
||||
header.AllowCORSHeader = map[string][]string{}
|
||||
header.AllowCORSHeader["Access-Control-Allow-Origin"] = []string{"*"}
|
||||
header.AllowCORSHeader["Access-Control-Allow-Methods"] =[]string{ "POST, GET, OPTIONS, PUT, DELETE"}
|
||||
header.AllowCORSHeader["Access-Control-Allow-Methods"] = []string{"POST, GET, OPTIONS, PUT, DELETE"}
|
||||
header.AllowCORSHeader["Access-Control-Allow-Headers"] = []string{"Content-Type"}
|
||||
|
||||
return header
|
||||
}
|
||||
|
||||
func (slf *CORSHeader) AddAllowHeader(key string,val string){
|
||||
slf.AllowCORSHeader["Access-Control-Allow-Headers"] = append(slf.AllowCORSHeader["Access-Control-Allow-Headers"],fmt.Sprintf("%s,%s",key,val))
|
||||
func (slf *CORSHeader) AddAllowHeader(key string, val string) {
|
||||
slf.AllowCORSHeader["Access-Control-Allow-Headers"] = append(slf.AllowCORSHeader["Access-Control-Allow-Headers"], fmt.Sprintf("%s,%s", key, val))
|
||||
}
|
||||
|
||||
func (slf *CORSHeader) copyTo(header http.Header){
|
||||
for k,v := range slf.AllowCORSHeader{
|
||||
for _,val := range v{
|
||||
header.Add(k,val)
|
||||
func (slf *CORSHeader) copyTo(header http.Header) {
|
||||
for k, v := range slf.AllowCORSHeader {
|
||||
for _, val := range v {
|
||||
header.Add(k, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -491,12 +489,12 @@ func (httpService *HttpService) ServeHTTP(w http.ResponseWriter, r *http.Request
|
||||
return
|
||||
}
|
||||
|
||||
session := &HttpSession{sessionDone:make(chan *HttpSession,1),httpRouter:httpService.httpRouter,statusCode:http.StatusOK}
|
||||
session := &HttpSession{sessionDone: make(chan *HttpSession, 1), httpRouter: httpService.httpRouter, statusCode: http.StatusOK}
|
||||
session.r = r
|
||||
session.w = w
|
||||
|
||||
defer r.Body.Close()
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
session.WriteStatusCode(http.StatusGatewayTimeout)
|
||||
session.flush()
|
||||
@@ -504,19 +502,19 @@ func (httpService *HttpService) ServeHTTP(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
session.body = body
|
||||
|
||||
httpService.GetEventHandler().NotifyEvent(&event.Event{Type:event.Sys_Event_Http_Event,Data:session})
|
||||
httpService.GetEventHandler().NotifyEvent(&event.Event{Type: event.Sys_Event_Http_Event, Data: session})
|
||||
ticker := time.NewTicker(httpService.processTimeout)
|
||||
select {
|
||||
case <-ticker.C:
|
||||
session.WriteStatusCode(http.StatusGatewayTimeout)
|
||||
session.flush()
|
||||
break
|
||||
case <- session.sessionDone:
|
||||
if session.fileData!=nil {
|
||||
case <-session.sessionDone:
|
||||
if session.fileData != nil {
|
||||
httpService.ProcessFile(session)
|
||||
}else if session.redirectData!=nil {
|
||||
} else if session.redirectData != nil {
|
||||
session.redirects()
|
||||
}else{
|
||||
} else {
|
||||
session.flush()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ type Element[ValueType NumberType] interface {
|
||||
}
|
||||
|
||||
//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
|
||||
if high == -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 &&
|
||||
(mid+1 < len(sElement)-1) {
|
||||
return mid + 1
|
||||
}
|
||||
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
|
||||
|
||||
61
util/algorithms/BitwiseOperation.go
Normal file
61
util/algorithms/BitwiseOperation.go
Normal 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)
|
||||
}
|
||||
37
util/algorithms/BitwiseOperation_test.go
Normal file
37
util/algorithms/BitwiseOperation_test.go
Normal 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")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 -X github.com/duanhf2012/origin/util/buildtime.BuildTag=debug"
|
||||
*/
|
||||
var BuildTime string
|
||||
|
||||
var BuildTag string
|
||||
|
||||
func GetBuildDateTime() string {
|
||||
return BuildTime
|
||||
}
|
||||
|
||||
func GetBuildTag() string {
|
||||
return BuildTag
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package coroutine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/origin/log"
|
||||
"reflect"
|
||||
"runtime/debug"
|
||||
)
|
||||
@@ -12,10 +13,11 @@ func F(callback interface{},recoverNum int, args ...interface{}) {
|
||||
var coreInfo string
|
||||
coreInfo = string(debug.Stack())
|
||||
coreInfo += "\n" + fmt.Sprintf("Core information is %v\n", r)
|
||||
fmt.Print(coreInfo)
|
||||
|
||||
if recoverNum==-1 ||recoverNum-1 >= 0 {
|
||||
log.SError(coreInfo)
|
||||
if recoverNum > 0{
|
||||
recoverNum -= 1
|
||||
}
|
||||
if recoverNum == -1 || recoverNum > 0 {
|
||||
go F(callback,recoverNum, args...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package math
|
||||
|
||||
import "github.com/duanhf2012/origin/log"
|
||||
|
||||
type NumberType interface {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user