mirror of
https://github.com/duanhf2012/origin.git
synced 2026-03-01 10:44:50 +08:00
提交origin2.0版本
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
package sysservice
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/service"
|
||||
"github.com/duanhf2012/origin/sysmodule"
|
||||
)
|
||||
|
||||
type LogService struct {
|
||||
service.BaseService
|
||||
logmodule *sysmodule.LogModule
|
||||
}
|
||||
|
||||
func (slf *LogService) InitLog(logservicename string, logFilePrefix string, openLevel uint) {
|
||||
slf.SetServiceName(logservicename)
|
||||
slf.logmodule = &sysmodule.LogModule{}
|
||||
slf.logmodule.Init(logFilePrefix, openLevel)
|
||||
}
|
||||
|
||||
func (slf *LogService) Printf(level uint, format string, v ...interface{}) {
|
||||
slf.logmodule.Printf(level, format, v...)
|
||||
}
|
||||
|
||||
func (slf *LogService) Print(level uint, v ...interface{}) {
|
||||
slf.logmodule.Print(level, v...)
|
||||
}
|
||||
|
||||
func (slf *LogService) AppendCallDepth(calldepth int) {
|
||||
slf.logmodule.AppendCallDepth(calldepth)
|
||||
}
|
||||
|
||||
func (slf *LogService) SetLogLevel(level uint) {
|
||||
slf.logmodule.SetLogLevel(level)
|
||||
}
|
||||
|
||||
func (slf *LogService) SetListenLogFunc(listenFun sysmodule.FunListenLog) {
|
||||
slf.logmodule.SetListenLogFunc(listenFun)
|
||||
}
|
||||
@@ -1,511 +0,0 @@
|
||||
package originhttp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/duanhf2012/origin/sysmodule"
|
||||
|
||||
"github.com/duanhf2012/origin/rpc"
|
||||
|
||||
"github.com/duanhf2012/origin/cluster"
|
||||
"github.com/duanhf2012/origin/network"
|
||||
"github.com/duanhf2012/origin/service"
|
||||
"github.com/duanhf2012/origin/util/uuid"
|
||||
)
|
||||
|
||||
type HttpRedirectData struct {
|
||||
Url string
|
||||
//Cookies map[string]string
|
||||
|
||||
CookieList []*http.Cookie
|
||||
}
|
||||
|
||||
type HttpRequest struct {
|
||||
Header http.Header
|
||||
Body string
|
||||
|
||||
ParamStr string //http://127.0.0.1:7001/aaa/bbb?aa=1中的aa=1部分
|
||||
mapParam map[string]string
|
||||
URL string
|
||||
//Req http.Request
|
||||
}
|
||||
|
||||
type HttpRespone struct {
|
||||
Respone []byte
|
||||
RedirectData HttpRedirectData
|
||||
//Resp http.ResponseWriter
|
||||
}
|
||||
|
||||
type ServeHTTPRouterMux struct {
|
||||
httpfiltrateList []HttpFiltrate
|
||||
allowOrigin bool
|
||||
}
|
||||
type ControllerMapsType map[string]reflect.Value
|
||||
|
||||
type HttpServerService struct {
|
||||
service.BaseService
|
||||
httpserver network.HttpServer
|
||||
port uint16
|
||||
|
||||
controllerMaps ControllerMapsType
|
||||
certfile string
|
||||
keyfile string
|
||||
ishttps bool
|
||||
serverHTTPMux ServeHTTPRouterMux
|
||||
}
|
||||
|
||||
type RouterMatchData struct {
|
||||
callpath string
|
||||
matchURL string
|
||||
routerType int8 //0表示函数调用 1表示静态资源
|
||||
}
|
||||
|
||||
type RouterStaticResoutceData struct {
|
||||
localpath string
|
||||
method string
|
||||
}
|
||||
|
||||
type HTTP_METHOD int
|
||||
|
||||
const (
|
||||
METHOD_GET HTTP_METHOD = iota
|
||||
METHOD_POST HTTP_METHOD = 1
|
||||
//METHOD_PUT HTTP_METHOD = 2
|
||||
)
|
||||
|
||||
var bPrintRequestTime bool
|
||||
|
||||
var postAliasUrl map[string]map[string]RouterMatchData //url地址,对应本service地址
|
||||
var staticRouterResource map[string]RouterStaticResoutceData
|
||||
|
||||
func init() {
|
||||
postAliasUrl = make(map[string]map[string]RouterMatchData)
|
||||
postAliasUrl["GET"] = make(map[string]RouterMatchData)
|
||||
postAliasUrl["POST"] = make(map[string]RouterMatchData)
|
||||
|
||||
staticRouterResource = make(map[string]RouterStaticResoutceData)
|
||||
}
|
||||
|
||||
type HttpHandle func(request *HttpRequest, resp *HttpRespone) error
|
||||
|
||||
func AnalysisRouterUrl(url string) (string, error) {
|
||||
|
||||
//替换所有空格
|
||||
url = strings.ReplaceAll(url, " ", "")
|
||||
if len(url) <= 1 || url[0] != '/' {
|
||||
return "", fmt.Errorf("url %s format is error!", url)
|
||||
}
|
||||
|
||||
//去掉尾部的/
|
||||
return strings.Trim(url, "/"), nil
|
||||
}
|
||||
|
||||
func (slf *HttpRequest) Query(key string) (string, bool) {
|
||||
if slf.mapParam == nil {
|
||||
slf.mapParam = make(map[string]string)
|
||||
//分析字符串
|
||||
slf.ParamStr = strings.Trim(slf.ParamStr, "/")
|
||||
paramStrList := strings.Split(slf.ParamStr, "&")
|
||||
for _, val := range paramStrList {
|
||||
Index := strings.Index(val, "=")
|
||||
if Index >= 0 {
|
||||
slf.mapParam[val[0:Index]] = val[Index+1:]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ret, ok := slf.mapParam[key]
|
||||
return ret, ok
|
||||
}
|
||||
|
||||
func Request(method HTTP_METHOD, url string, handle HttpHandle) error {
|
||||
fnpath := runtime.FuncForPC(reflect.ValueOf(handle).Pointer()).Name()
|
||||
|
||||
sidx := strings.LastIndex(fnpath, "*")
|
||||
if sidx == -1 {
|
||||
return errors.New(fmt.Sprintf("http post func path is error, %s\n", fnpath))
|
||||
}
|
||||
|
||||
eidx := strings.LastIndex(fnpath, "-fm")
|
||||
if sidx == -1 {
|
||||
return errors.New(fmt.Sprintf("http post func path is error, %s\n", fnpath))
|
||||
}
|
||||
callpath := fnpath[sidx+1 : eidx]
|
||||
ridx := strings.LastIndex(callpath, ")")
|
||||
if ridx == -1 {
|
||||
return errors.New(fmt.Sprintf("http post func path is error, %s\n", fnpath))
|
||||
}
|
||||
|
||||
hidx := strings.LastIndex(callpath, "HTTP_")
|
||||
if hidx == -1 {
|
||||
return errors.New(fmt.Sprintf("http post func not contain HTTP_, %s\n", fnpath))
|
||||
}
|
||||
|
||||
callpath = strings.ReplaceAll(callpath, ")", "")
|
||||
|
||||
var r RouterMatchData
|
||||
var matchURL string
|
||||
var err error
|
||||
r.routerType = 0
|
||||
r.callpath = "_" + callpath
|
||||
matchURL, err = AnalysisRouterUrl(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var strMethod string
|
||||
if method == METHOD_GET {
|
||||
strMethod = "GET"
|
||||
} else if method == METHOD_POST {
|
||||
strMethod = "POST"
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
postAliasUrl[strMethod][matchURL] = r
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Post(url string, handle HttpHandle) error {
|
||||
return Request(METHOD_POST, url, handle)
|
||||
}
|
||||
|
||||
func Get(url string, handle HttpHandle) error {
|
||||
return Request(METHOD_GET, url, handle)
|
||||
}
|
||||
|
||||
func (slf *HttpServerService) OnInit() error {
|
||||
slf.serverHTTPMux = ServeHTTPRouterMux{}
|
||||
slf.httpserver.Init(slf.port, &slf.serverHTTPMux, 10*time.Second, 10*time.Second)
|
||||
if slf.ishttps == true {
|
||||
slf.httpserver.SetHttps(slf.certfile, slf.keyfile)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *ServeHTTPRouterMux) SetAlowOrigin(allowOrigin bool) {
|
||||
slf.allowOrigin = allowOrigin
|
||||
}
|
||||
|
||||
func (slf *ServeHTTPRouterMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if slf.allowOrigin == true {
|
||||
if origin := r.Header.Get("Origin"); origin != "" {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
||||
w.Header().Set("Access-Control-Allow-Headers",
|
||||
"Action, Module") //有使用自定义头 需要这个,Action, Module是例子
|
||||
}
|
||||
}
|
||||
|
||||
if r.Method == "OPTIONS" {
|
||||
return
|
||||
}
|
||||
|
||||
methodRouter, bok := postAliasUrl[r.Method]
|
||||
if bok == false {
|
||||
writeRespone(w, http.StatusNotFound, fmt.Sprint("Can not support method."))
|
||||
return
|
||||
}
|
||||
|
||||
//权限验证
|
||||
var errRet error
|
||||
for _, filter := range slf.httpfiltrateList {
|
||||
ret := filter(r.URL.Path, w, r)
|
||||
if ret == nil {
|
||||
errRet = nil
|
||||
break
|
||||
} else {
|
||||
errRet = ret
|
||||
}
|
||||
}
|
||||
if errRet != nil {
|
||||
writeRespone(w, http.StatusOK, errRet.Error())
|
||||
return
|
||||
}
|
||||
|
||||
url := strings.Trim(r.URL.Path, "/")
|
||||
var strCallPath string
|
||||
matchData, ok := methodRouter[url]
|
||||
if ok == true {
|
||||
strCallPath = matchData.callpath
|
||||
} else {
|
||||
//如果是资源处理
|
||||
for k, v := range staticRouterResource {
|
||||
idx := strings.Index(url, k)
|
||||
if idx != -1 {
|
||||
staticServer(k, v, w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 拼接得到rpc服务的名称
|
||||
vstr := strings.Split(url, "/")
|
||||
if len(vstr) < 2 {
|
||||
writeRespone(w, http.StatusNotFound, "Cannot find path.")
|
||||
return
|
||||
}
|
||||
strCallPath = "_" + vstr[0] + ".HTTP_" + vstr[1]
|
||||
}
|
||||
|
||||
defer r.Body.Close()
|
||||
msg, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
writeRespone(w, http.StatusBadRequest, "")
|
||||
return
|
||||
}
|
||||
|
||||
request := HttpRequest{r.Header, string(msg), r.URL.RawQuery, nil, r.URL.Path}
|
||||
var resp HttpRespone
|
||||
//resp.Resp = w
|
||||
timeFuncStart := time.Now()
|
||||
err = cluster.InstanceClusterMgr().Call(strCallPath, &request, &resp)
|
||||
|
||||
timeFuncPass := time.Since(timeFuncStart)
|
||||
if bPrintRequestTime {
|
||||
service.GetLogger().Printf(service.LEVER_INFO, "HttpServer Time : %s url : %s\n", timeFuncPass, strCallPath)
|
||||
}
|
||||
if err != nil {
|
||||
writeRespone(w, http.StatusBadRequest, fmt.Sprint(err))
|
||||
} else {
|
||||
if resp.RedirectData.Url != "" {
|
||||
resp.redirects(&w, r)
|
||||
} else {
|
||||
writeRespone(w, http.StatusOK, string(resp.Respone))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// CkResourceDir 检查静态资源文件夹路径
|
||||
func SetStaticResource(method HTTP_METHOD, urlpath string, dirname string) error {
|
||||
_, err := os.Stat(dirname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
matchURL, berr := AnalysisRouterUrl(urlpath)
|
||||
if berr != nil {
|
||||
return berr
|
||||
}
|
||||
|
||||
var routerData RouterStaticResoutceData
|
||||
if method == METHOD_GET {
|
||||
routerData.method = "GET"
|
||||
} else if method == METHOD_POST {
|
||||
routerData.method = "POST"
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
routerData.localpath = dirname
|
||||
|
||||
staticRouterResource[matchURL] = routerData
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeRespone(w http.ResponseWriter, status int, msg string) {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
w.Write([]byte(msg))
|
||||
}
|
||||
|
||||
type HttpFiltrate func(path string, w http.ResponseWriter, r *http.Request) error
|
||||
|
||||
func (slf *HttpServerService) AppendHttpFiltrate(fun HttpFiltrate) bool {
|
||||
slf.serverHTTPMux.httpfiltrateList = append(slf.serverHTTPMux.httpfiltrateList, fun)
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (slf *HttpServerService) OnRun() bool {
|
||||
|
||||
slf.httpserver.Start()
|
||||
return false
|
||||
}
|
||||
|
||||
func NewHttpServerService(port uint16) *HttpServerService {
|
||||
http := new(HttpServerService)
|
||||
|
||||
http.port = port
|
||||
return http
|
||||
}
|
||||
|
||||
func (slf *HttpServerService) OnDestory() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *HttpServerService) OnSetupService(iservice service.IService) {
|
||||
rpc.RegisterName(iservice.GetServiceName(), "HTTP_", iservice)
|
||||
}
|
||||
|
||||
func (slf *HttpServerService) OnRemoveService(iservice service.IService) {
|
||||
return
|
||||
}
|
||||
|
||||
func (slf *HttpServerService) SetPrintRequestTime(isPrint bool) {
|
||||
bPrintRequestTime = isPrint
|
||||
}
|
||||
|
||||
func staticServer(routerUrl string, routerData RouterStaticResoutceData, w http.ResponseWriter, r *http.Request) {
|
||||
upath := r.URL.Path
|
||||
idx := strings.Index(upath, routerUrl)
|
||||
subPath := strings.Trim(upath[idx+len(routerUrl):], "/")
|
||||
|
||||
destLocalPath := routerData.localpath + subPath
|
||||
|
||||
writeResp := func(status int, msg string) {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
w.Write([]byte(msg))
|
||||
}
|
||||
|
||||
switch r.Method {
|
||||
//获取资源
|
||||
case "GET":
|
||||
//判断文件夹是否存在
|
||||
_, err := os.Stat(destLocalPath)
|
||||
if err == nil {
|
||||
http.ServeFile(w, r, destLocalPath)
|
||||
} else {
|
||||
writeResp(http.StatusNotFound, "")
|
||||
return
|
||||
}
|
||||
//上传资源
|
||||
case "POST":
|
||||
// 在这儿处理例外路由接口
|
||||
/*
|
||||
var errRet error
|
||||
for _, filter := range slf.httpfiltrateList {
|
||||
ret := filter(r.URL.Path, w, r)
|
||||
if ret == nil {
|
||||
errRet = nil
|
||||
break
|
||||
} else {
|
||||
errRet = ret
|
||||
}
|
||||
}
|
||||
if errRet != nil {
|
||||
w.Write([]byte(errRet.Error()))
|
||||
return
|
||||
}*/
|
||||
r.ParseMultipartForm(32 << 20) // max memory is set to 32MB
|
||||
resourceFile, resourceFileHeader, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
writeResp(http.StatusNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
defer resourceFile.Close()
|
||||
//重新拼接文件名
|
||||
imgFormat := strings.Split(resourceFileHeader.Filename, ".")
|
||||
if len(imgFormat) < 2 {
|
||||
writeResp(http.StatusNotFound, "not a file")
|
||||
return
|
||||
}
|
||||
filePrefixName := uuid.Rand().HexEx()
|
||||
fileName := filePrefixName + "." + imgFormat[len(imgFormat)-1]
|
||||
//创建文件
|
||||
localpath := fmt.Sprintf("%s%s", destLocalPath, fileName)
|
||||
localfd, err := os.OpenFile(localpath, os.O_WRONLY|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
writeResp(http.StatusNotFound, "upload fail")
|
||||
return
|
||||
}
|
||||
defer localfd.Close()
|
||||
io.Copy(localfd, resourceFile)
|
||||
writeResp(http.StatusOK, upath+"/"+fileName)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (slf *HttpServerService) GetMethod(strCallPath string) (*reflect.Value, error) {
|
||||
value, ok := slf.controllerMaps[strCallPath]
|
||||
if ok == false {
|
||||
err := fmt.Errorf("not find api")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &value, nil
|
||||
}
|
||||
|
||||
func (slf *HttpServerService) SetHttps(certfile string, keyfile string) bool {
|
||||
if certfile == "" || keyfile == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
slf.ishttps = true
|
||||
slf.certfile = certfile
|
||||
slf.keyfile = keyfile
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
//序列化后写入Respone
|
||||
func (slf *HttpRespone) WriteRespne(v interface{}) error {
|
||||
StrRet, retErr := json.Marshal(v)
|
||||
if retErr != nil {
|
||||
slf.Respone = []byte(`{"Code": 2,"Message":"service error"}`)
|
||||
service.GetLogger().Printf(sysmodule.LEVER_ERROR, "Json Marshal Error:%v\n", retErr)
|
||||
} else {
|
||||
slf.Respone = StrRet
|
||||
}
|
||||
|
||||
return retErr
|
||||
}
|
||||
|
||||
func (slf *HttpRespone) WriteRespones(Code int32, Msg string, Data interface{}) {
|
||||
|
||||
var StrRet string
|
||||
//判断是否有错误码
|
||||
if Code > 0 {
|
||||
StrRet = fmt.Sprintf(`{"RCode": %d,"RMsg":"%s"}`, Code, Msg)
|
||||
} else {
|
||||
if Data == nil {
|
||||
if Msg != "" {
|
||||
StrRet = fmt.Sprintf(`{"RCode": 0,"RMsg":"%s"}`, Msg)
|
||||
} else {
|
||||
StrRet = `{"RCode": 0}`
|
||||
}
|
||||
} else {
|
||||
if reflect.TypeOf(Data).Kind() == reflect.String {
|
||||
StrRet = fmt.Sprintf(`{"RCode": %d , "Data": "%s"}`, Code, Data)
|
||||
} else {
|
||||
JsonRet, Err := json.Marshal(Data)
|
||||
if Err != nil {
|
||||
service.GetLogger().Printf(sysmodule.LEVER_ERROR, "common WriteRespone Json Marshal Err %+v", Data)
|
||||
} else {
|
||||
StrRet = fmt.Sprintf(`{"RCode": %d , "Data": %s}`, Code, JsonRet)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
slf.Respone = []byte(StrRet)
|
||||
}
|
||||
|
||||
func (slf *HttpRespone) Redirect(url string, cookieList []*http.Cookie) {
|
||||
slf.RedirectData.Url = url
|
||||
slf.RedirectData.CookieList = cookieList
|
||||
}
|
||||
|
||||
func (slf *HttpRespone) redirects(w *http.ResponseWriter, req *http.Request) {
|
||||
if slf.RedirectData.CookieList != nil {
|
||||
for _, v := range slf.RedirectData.CookieList {
|
||||
http.SetCookie(*w, v)
|
||||
}
|
||||
}
|
||||
|
||||
http.Redirect(*w, req, slf.RedirectData.Url,
|
||||
// see @andreiavrammsd comment: often 307 > 301
|
||||
http.StatusTemporaryRedirect)
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package sysservice
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"runtime/pprof"
|
||||
"time"
|
||||
|
||||
"github.com/duanhf2012/origin/service"
|
||||
"github.com/duanhf2012/origin/sysservice/originhttp"
|
||||
)
|
||||
|
||||
type PProfService struct {
|
||||
service.BaseService
|
||||
fisttime int
|
||||
}
|
||||
|
||||
type ProfileData struct {
|
||||
Name string
|
||||
Count int
|
||||
}
|
||||
|
||||
type Profilestruct struct {
|
||||
Fisttime int
|
||||
ProfileList []ProfileData
|
||||
}
|
||||
|
||||
func (slf *PProfService) OnInit() error {
|
||||
slf.fisttime = (int)(time.Now().UnixNano())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *PProfService) GetPprof() ([]byte, error) {
|
||||
var pfiles Profilestruct
|
||||
pfiles.Fisttime = slf.fisttime
|
||||
|
||||
for _, p := range pprof.Profiles() {
|
||||
pfiles.ProfileList = append(pfiles.ProfileList, ProfileData{
|
||||
Name: p.Name(),
|
||||
Count: p.Count(),
|
||||
})
|
||||
}
|
||||
|
||||
return json.Marshal(pfiles)
|
||||
}
|
||||
|
||||
func (slf *PProfService) HTTP_DebugPProf(request *originhttp.HttpRequest, resp *originhttp.HttpRespone) error {
|
||||
var err error
|
||||
resp.Respone, err = slf.GetPprof()
|
||||
if err != nil {
|
||||
resp.Respone = []byte(fmt.Sprint(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *PProfService) RPC_DebugPProf(arg *string, ret *Profilestruct) error {
|
||||
|
||||
ret.Fisttime = slf.fisttime
|
||||
for _, p := range pprof.Profiles() {
|
||||
ret.ProfileList = append(ret.ProfileList, ProfileData{
|
||||
Name: p.Name(),
|
||||
Count: p.Count(),
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *PProfService) HTTP_Test(request *originhttp.HttpRequest, resp *originhttp.HttpRespone) error {
|
||||
|
||||
resp.Respone = []byte(request.Body)
|
||||
return nil
|
||||
}
|
||||
@@ -1,167 +0,0 @@
|
||||
package synccacheservice
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/duanhf2012/origin/cluster"
|
||||
"github.com/duanhf2012/origin/service"
|
||||
"github.com/duanhf2012/origin/util"
|
||||
)
|
||||
|
||||
const (
|
||||
MAX_SYNC_DATA_CHAN_NUM = 10000
|
||||
)
|
||||
|
||||
//CReportService ...
|
||||
type CSyncCacheService struct {
|
||||
service.BaseService
|
||||
mapCache util.Map
|
||||
syncQueue *util.SyncQueue
|
||||
|
||||
nodeIdList []int
|
||||
syncDataChanList []chan *SyncCacheData
|
||||
}
|
||||
|
||||
type SyncCacheData struct {
|
||||
OperType int8 //0 表示添加或者更新 1表示删除
|
||||
Key string
|
||||
Val string
|
||||
Wxpire int32 //ms
|
||||
NodeIdList []int
|
||||
reTryCount uint32
|
||||
reTryTime int64
|
||||
}
|
||||
|
||||
//OnInit ...
|
||||
func (slf *CSyncCacheService) OnInit() error {
|
||||
slf.syncQueue = util.NewSyncQueue()
|
||||
var callServiceName string
|
||||
slf.nodeIdList = cluster.InstanceClusterMgr().GetNodeList("CSyncCacheService.RPC_SyncString", &callServiceName, nil)
|
||||
for _, nodeId := range slf.nodeIdList {
|
||||
syncCacheData := make(chan *SyncCacheData, MAX_SYNC_DATA_CHAN_NUM)
|
||||
slf.syncDataChanList = append(slf.syncDataChanList, syncCacheData)
|
||||
go slf.syncRouter(nodeId, syncCacheData)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *CSyncCacheService) syncRouter(nodeId int, syncDataChan chan *SyncCacheData) error {
|
||||
|
||||
tryCount := 0
|
||||
for {
|
||||
select {
|
||||
case <-slf.ExitChan:
|
||||
break
|
||||
case data := <-syncDataChan:
|
||||
var ret int
|
||||
cluster.CallNode(nodeId, "CSyncCacheService.RPC_SyncString", data, &ret)
|
||||
if ret == 0 {
|
||||
if tryCount < 3 {
|
||||
time.Sleep(800 * time.Millisecond)
|
||||
} else {
|
||||
time.Sleep(1500 * time.Millisecond)
|
||||
}
|
||||
|
||||
slf.tryPushSyncData(syncDataChan, data)
|
||||
tryCount++
|
||||
} else {
|
||||
tryCount = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *CSyncCacheService) tryPushSyncData(syncDataChan chan *SyncCacheData, syncData *SyncCacheData) bool {
|
||||
if len(syncDataChan) >= MAX_SYNC_DATA_CHAN_NUM {
|
||||
return false
|
||||
}
|
||||
syncDataChan <- syncData
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (slf *CSyncCacheService) RPC_SyncString(request *SyncCacheData, ret *int) error {
|
||||
|
||||
if request.OperType == 0 {
|
||||
slf.mapCache.Set(request.Key, request.Val)
|
||||
} else {
|
||||
slf.mapCache.Del(request.Key)
|
||||
}
|
||||
|
||||
*ret = 1
|
||||
return nil
|
||||
}
|
||||
|
||||
func SetStringJson(key string, val interface{}) error {
|
||||
|
||||
byteBuf, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
SetString(key, string(byteBuf))
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetStringJson(key string, val interface{}) error {
|
||||
ret, err := GetString(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal([]byte(ret), val)
|
||||
return err
|
||||
}
|
||||
|
||||
func DelString(key string) error {
|
||||
pubcacheservice := service.InstanceServiceMgr().FindService("CSyncCacheService")
|
||||
if pubcacheservice == nil {
|
||||
return errors.New("Cannot find CSyncCacheService")
|
||||
}
|
||||
|
||||
pPubCacheService := pubcacheservice.(*CSyncCacheService)
|
||||
syncCacheData := SyncCacheData{1, key, "", 0, pPubCacheService.nodeIdList[:], 0, 0}
|
||||
|
||||
for _, syncChan := range pPubCacheService.syncDataChanList {
|
||||
pPubCacheService.tryPushSyncData(syncChan, &syncCacheData)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SetString(key string, val string) error {
|
||||
pubcacheservice := service.InstanceServiceMgr().FindService("CSyncCacheService")
|
||||
if pubcacheservice == nil {
|
||||
return errors.New("Cannot find CSyncCacheService")
|
||||
}
|
||||
|
||||
//同步所有远程结点
|
||||
pPubCacheService := pubcacheservice.(*CSyncCacheService)
|
||||
syncCacheData := SyncCacheData{0, key, val, 0, pPubCacheService.nodeIdList[:], 0, 0}
|
||||
for _, syncChan := range pPubCacheService.syncDataChanList {
|
||||
pPubCacheService.tryPushSyncData(syncChan, &syncCacheData)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetString(key string) (string, error) {
|
||||
pubcacheservice := service.InstanceServiceMgr().FindService("CSyncCacheService")
|
||||
if pubcacheservice == nil {
|
||||
return "", errors.New("Cannot find CSyncCacheService")
|
||||
}
|
||||
|
||||
pPubCacheService := pubcacheservice.(*CSyncCacheService)
|
||||
ret := pPubCacheService.mapCache.Get(key)
|
||||
if ret == nil {
|
||||
return "", errors.New(fmt.Sprintf("Cannot find key :%s", key))
|
||||
}
|
||||
|
||||
return ret.(string), nil
|
||||
}
|
||||
137
sysservice/tcpservice.go
Normal file
137
sysservice/tcpservice.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package sysservice
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duanhf2012/originnet/event"
|
||||
"github.com/duanhf2012/originnet/log"
|
||||
"github.com/duanhf2012/originnet/network"
|
||||
"github.com/duanhf2012/originnet/service"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type TcpService struct {
|
||||
tcpServer network.TCPServer
|
||||
service.Service
|
||||
|
||||
tcpService *TcpService
|
||||
mapClientLocker sync.RWMutex
|
||||
mapClient map[uint64] *Client
|
||||
initClientId uint64
|
||||
process network.Processor
|
||||
}
|
||||
|
||||
const Default_MaxConnNum = 3000
|
||||
const Default_PendingWriteNum = 10000
|
||||
const Default_LittleEndian = false
|
||||
const Default_MinMsgLen = 2
|
||||
const Default_MaxMsgLen = 65535
|
||||
|
||||
|
||||
|
||||
func (slf *TcpService) OnInit() error{
|
||||
iConfig := slf.GetServiceCfg()
|
||||
if iConfig == nil {
|
||||
return fmt.Errorf("%s service config is error!",slf.GetName())
|
||||
}
|
||||
tcpCfg := iConfig.(map[string]interface{})
|
||||
addr,ok := tcpCfg["ListenAddr"]
|
||||
if ok == false {
|
||||
return fmt.Errorf("%s service config is error!",slf.GetName())
|
||||
}
|
||||
slf.tcpServer.Addr = addr.(string)
|
||||
slf.tcpServer.MaxConnNum = Default_MaxConnNum
|
||||
slf.tcpServer.PendingWriteNum = Default_PendingWriteNum
|
||||
slf.tcpServer.LittleEndian = Default_LittleEndian
|
||||
slf.tcpServer.MinMsgLen = Default_MinMsgLen
|
||||
slf.tcpServer.MaxMsgLen = Default_MaxMsgLen
|
||||
MaxConnNum,ok := tcpCfg["MaxConnNum"]
|
||||
if ok == true {
|
||||
slf.tcpServer.MaxConnNum = int(MaxConnNum.(float64))
|
||||
}
|
||||
PendingWriteNum,ok := tcpCfg["PendingWriteNum"]
|
||||
if ok == true {
|
||||
slf.tcpServer.PendingWriteNum = int(PendingWriteNum.(float64))
|
||||
}
|
||||
LittleEndian,ok := tcpCfg["LittleEndian"]
|
||||
if ok == true {
|
||||
slf.tcpServer.LittleEndian = LittleEndian.(bool)
|
||||
}
|
||||
MinMsgLen,ok := tcpCfg["MinMsgLen"]
|
||||
if ok == true {
|
||||
slf.tcpServer.MinMsgLen = uint32(MinMsgLen.(float64))
|
||||
}
|
||||
MaxMsgLen,ok := tcpCfg["MaxMsgLen"]
|
||||
if ok == true {
|
||||
slf.tcpServer.MaxMsgLen = uint32(MaxMsgLen.(float64))
|
||||
}
|
||||
slf.tcpServer.NewAgent =slf.NewClient
|
||||
slf.tcpServer.Start()
|
||||
//加载配置
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *TcpService) SetProcessor(process network.Processor){
|
||||
slf.process = process
|
||||
}
|
||||
|
||||
func (slf *TcpService) NewClient(conn *network.TCPConn) network.Agent {
|
||||
slf.mapClientLocker.Lock()
|
||||
defer slf.mapClientLocker.Unlock()
|
||||
|
||||
for {
|
||||
slf.initClientId+=1
|
||||
_,ok := slf.mapClient[slf.initClientId]
|
||||
if ok == true {
|
||||
continue
|
||||
}
|
||||
|
||||
pClient := &Client{tcpConn:conn}
|
||||
slf.mapClient[slf.initClientId] = pClient
|
||||
return pClient
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
type Client struct {
|
||||
id uint64
|
||||
tcpConn *network.TCPConn
|
||||
tcpService *TcpService
|
||||
}
|
||||
|
||||
type TcpPack struct {
|
||||
ClientId uint64
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
func (slf *Client) GetId() uint64 {
|
||||
return slf.id
|
||||
}
|
||||
|
||||
func (slf *Client) Run() {
|
||||
slf.tcpService.GetEventReciver().NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp_Connected,Data:&TcpPack{ClientId:slf.id}})
|
||||
for{
|
||||
bytes,err := slf.tcpConn.ReadMsg()
|
||||
if err != nil {
|
||||
log.Debug("read client id %d is error",err)
|
||||
break
|
||||
}
|
||||
data,err:=slf.tcpService.process.Unmarshal(bytes)
|
||||
if err != nil {
|
||||
log.Debug("process.Unmarshal is error:%+v",err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
slf.tcpService.GetEventReciver().NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp_RecvPack,Data:&TcpPack{ClientId:slf.id,Data:data}})
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *Client) OnClose(){
|
||||
slf.tcpService.GetEventReciver().NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp_DisConnected,Data:&TcpPack{ClientId:slf.id}})
|
||||
slf.tcpService.mapClientLocker.Lock()
|
||||
defer slf.tcpService.mapClientLocker.Unlock()
|
||||
delete (slf.tcpService.mapClient,slf.GetId())
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
package sysservice
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/duanhf2012/origin/network"
|
||||
"github.com/duanhf2012/origin/service"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type TcpSocketPbService struct {
|
||||
service.BaseService
|
||||
listenaddr string
|
||||
tcpsocketserver network.TcpSocketServer
|
||||
MsgProcessor
|
||||
}
|
||||
|
||||
|
||||
type MessageHandler func(clientid uint64,msgtype uint16,msg proto.Message)
|
||||
type MessageRecvHandler func(pClient *network.SClient,pPack *network.MsgBasePack)
|
||||
type EventHandler func(clientid uint64)
|
||||
type ExceptMsgHandler func(clientid uint64,pPack *network.MsgBasePack,err error)
|
||||
|
||||
type MsgProcessor struct {
|
||||
mapMsg map[uint16]MessageInfo
|
||||
connEvent EventHandler
|
||||
disconnEvent EventHandler
|
||||
exceptMsgHandler ExceptMsgHandler
|
||||
messageRecvHandler MessageRecvHandler
|
||||
}
|
||||
|
||||
|
||||
func (slf *MsgProcessor) InitProcessor(){
|
||||
slf.mapMsg = make(map[uint16]MessageInfo)
|
||||
}
|
||||
|
||||
func (slf *MsgProcessor) RegMessage(msgtype uint16,msg proto.Message,handle MessageHandler){
|
||||
var info MessageInfo
|
||||
|
||||
info.msgType = reflect.TypeOf(msg.(proto.Message))
|
||||
info.msgHandler = handle
|
||||
slf.mapMsg[msgtype] = info
|
||||
}
|
||||
|
||||
func (slf *MsgProcessor) RegConnectEvent(eventHandler EventHandler){
|
||||
slf.connEvent = eventHandler
|
||||
}
|
||||
|
||||
func (slf *MsgProcessor) RegDisconnectEvent(eventHandler EventHandler){
|
||||
slf.disconnEvent = eventHandler
|
||||
}
|
||||
|
||||
func (slf *MsgProcessor) RegExceptMessage(exceptMsgHandler ExceptMsgHandler){
|
||||
slf.exceptMsgHandler = exceptMsgHandler
|
||||
}
|
||||
|
||||
func (slf *MsgProcessor) RegRecvMessage(msgHandler MessageRecvHandler){
|
||||
slf.messageRecvHandler = msgHandler
|
||||
}
|
||||
|
||||
func (slf *MsgProcessor) OnExceptMsg (pClient *network.SClient,pPack *network.MsgBasePack,err error){
|
||||
if slf.exceptMsgHandler!=nil {
|
||||
slf.exceptMsgHandler(pClient.GetId(),pPack,err)
|
||||
}else{
|
||||
pClient.Close()
|
||||
//记录日志
|
||||
service.GetLogger().Printf(service.LEVER_WARN, "OnExceptMsg packtype %d,error %+v",pPack.PackType,err)
|
||||
}
|
||||
}
|
||||
|
||||
func NewTcpSocketPbService(listenaddr string) *TcpSocketPbService {
|
||||
ts := new(TcpSocketPbService)
|
||||
|
||||
ts.listenaddr = listenaddr
|
||||
ts.mapMsg = make(map[uint16]MessageInfo,1)
|
||||
ts.tcpsocketserver.Register(listenaddr,ts)
|
||||
return ts
|
||||
}
|
||||
|
||||
func (slf *TcpSocketPbService) OnInit() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *TcpSocketPbService) OnRun() bool {
|
||||
slf.tcpsocketserver.Start()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
type MessageInfo struct {
|
||||
msgType reflect.Type
|
||||
msgHandler MessageHandler
|
||||
}
|
||||
|
||||
|
||||
func (slf *TcpSocketPbService) RegMessage(msgtype uint16,msg proto.Message,handle MessageHandler){
|
||||
var info MessageInfo
|
||||
|
||||
info.msgType = reflect.TypeOf(msg.(proto.Message))
|
||||
info.msgHandler = handle
|
||||
slf.mapMsg[msgtype] = info
|
||||
}
|
||||
|
||||
|
||||
func (slf *TcpSocketPbService) OnConnected(pClient *network.SClient){
|
||||
if slf.connEvent!=nil {
|
||||
slf.connEvent(pClient.GetId())
|
||||
}
|
||||
}
|
||||
|
||||
func (slf *TcpSocketPbService) OnDisconnect(pClient *network.SClient){
|
||||
if slf.disconnEvent!=nil {
|
||||
slf.disconnEvent(pClient.GetId())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (slf *MsgProcessor) Handle(pClient *network.SClient,pPack *network.MsgBasePack){
|
||||
if info, ok := slf.mapMsg[pPack.PackType]; ok {
|
||||
msg := reflect.New(info.msgType.Elem()).Interface()
|
||||
tmp := msg.(proto.Message)
|
||||
err := proto.Unmarshal(pPack.Body, tmp)
|
||||
if err != nil {
|
||||
slf.OnExceptMsg(pClient,pPack,err)
|
||||
return
|
||||
}
|
||||
|
||||
info.msgHandler(pClient.GetId(),pPack.PackType, msg.(proto.Message))
|
||||
return
|
||||
}else if slf.messageRecvHandler!=nil {
|
||||
slf.messageRecvHandler(pClient,pPack)
|
||||
return
|
||||
}
|
||||
|
||||
slf.OnExceptMsg(pClient,pPack,errors.New("not found PackType"))
|
||||
}
|
||||
|
||||
|
||||
func (slf *TcpSocketPbService) OnRecvMsg(pClient *network.SClient, pPack *network.MsgBasePack){
|
||||
slf.Handle(pClient,pPack)
|
||||
}
|
||||
|
||||
func DefaultTSPbService() *TcpSocketPbService{
|
||||
iservice := service.InstanceServiceMgr().FindService("TcpSocketPbService")
|
||||
if iservice == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return iservice.(*TcpSocketPbService)
|
||||
}
|
||||
|
||||
func GetTcpSocketPbService(serviceName string) *TcpSocketPbService{
|
||||
iservice := service.InstanceServiceMgr().FindService(serviceName)
|
||||
if iservice == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return iservice.(*TcpSocketPbService)
|
||||
}
|
||||
|
||||
func (slf *TcpSocketPbService) SendMsg(clientid uint64,packtype uint16,message proto.Message) error{
|
||||
return slf.tcpsocketserver.SendMsg(clientid,packtype,message)
|
||||
}
|
||||
|
||||
func (slf *TcpSocketPbService) Close(clientid uint64) error{
|
||||
return slf.tcpsocketserver.Close(clientid)
|
||||
}
|
||||
|
||||
func (slf *TcpSocketPbService) Send(clientid uint64,pack *network.MsgBasePack) error {
|
||||
return slf.tcpsocketserver.Send(clientid,pack)
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package sysservice
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/network"
|
||||
"github.com/duanhf2012/origin/service"
|
||||
)
|
||||
|
||||
type WSAgentService struct {
|
||||
service.BaseService
|
||||
agentserver network.WSAgentServer
|
||||
pattern string
|
||||
port uint16
|
||||
bEnableCompression bool
|
||||
}
|
||||
|
||||
func (ws *WSAgentService) OnInit() error {
|
||||
ws.AddModule(&ws.agentserver)
|
||||
ws.agentserver.Init(ws.port)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ws *WSAgentService) OnRun() bool {
|
||||
ws.agentserver.Start()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func NewWSAgentService(port uint16) *WSAgentService {
|
||||
wss := new(WSAgentService)
|
||||
|
||||
wss.port = port
|
||||
return wss
|
||||
}
|
||||
|
||||
func (ws *WSAgentService) OnDestory() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ws *WSAgentService) SetupAgent(pattern string, agent network.IAgent, bEnableCompression bool) {
|
||||
ws.agentserver.SetupAgent(pattern, agent, bEnableCompression)
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package sysservice
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/network"
|
||||
"github.com/duanhf2012/origin/service"
|
||||
)
|
||||
|
||||
type WSServerService struct {
|
||||
service.BaseService
|
||||
wsserver network.WebsocketServer
|
||||
|
||||
pattern string
|
||||
port uint16
|
||||
messageReciver network.IMessageReceiver
|
||||
bEnableCompression bool
|
||||
}
|
||||
|
||||
func (ws *WSServerService) OnInit() error {
|
||||
|
||||
ws.wsserver.Init(ws.port)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ws *WSServerService) OnRun() bool {
|
||||
ws.wsserver.Start()
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func NewWSServerService(port uint16) *WSServerService {
|
||||
wss := new(WSServerService)
|
||||
|
||||
wss.port = port
|
||||
return wss
|
||||
}
|
||||
|
||||
func (ws *WSServerService) OnDestory() error {
|
||||
return nil
|
||||
}
|
||||
func (ws *WSServerService) SetupReciver(pattern string, messageReciver network.IMessageReceiver, bEnableCompression bool) {
|
||||
ws.wsserver.SetupReciver(pattern, messageReciver, bEnableCompression)
|
||||
}
|
||||
|
||||
func (slf *WSServerService) SetWSS(certfile string, keyfile string) bool {
|
||||
slf.wsserver.SetWSS(certfile, keyfile)
|
||||
return true
|
||||
}
|
||||
4
sysservice/wsservice.go
Normal file
4
sysservice/wsservice.go
Normal file
@@ -0,0 +1,4 @@
|
||||
package sysservice
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user