|
@@ -4,6 +4,21 @@ import (
|
|
"context"
|
|
"context"
|
|
"crypto/md5"
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"encoding/hex"
|
|
|
|
+ "io/ioutil"
|
|
|
|
+ "math"
|
|
|
|
+ "math/rand"
|
|
|
|
+ "net"
|
|
|
|
+ httppkg "net/http"
|
|
|
|
+ "net/http/pprof"
|
|
|
|
+ "os"
|
|
|
|
+ "os/signal"
|
|
|
|
+ "path"
|
|
|
|
+ "strings"
|
|
|
|
+ "sync"
|
|
|
|
+ "sync/atomic"
|
|
|
|
+ "syscall"
|
|
|
|
+ "time"
|
|
|
|
+
|
|
"git.nspix.com/golang/micro/broker"
|
|
"git.nspix.com/golang/micro/broker"
|
|
"git.nspix.com/golang/micro/gateway"
|
|
"git.nspix.com/golang/micro/gateway"
|
|
"git.nspix.com/golang/micro/gateway/cli"
|
|
"git.nspix.com/golang/micro/gateway/cli"
|
|
@@ -18,20 +33,6 @@ import (
|
|
"git.nspix.com/golang/micro/log"
|
|
"git.nspix.com/golang/micro/log"
|
|
"git.nspix.com/golang/micro/registry"
|
|
"git.nspix.com/golang/micro/registry"
|
|
"github.com/google/btree"
|
|
"github.com/google/btree"
|
|
- "io/ioutil"
|
|
|
|
- "math"
|
|
|
|
- "math/rand"
|
|
|
|
- "net"
|
|
|
|
- hp "net/http"
|
|
|
|
- "net/http/pprof"
|
|
|
|
- "os"
|
|
|
|
- "os/signal"
|
|
|
|
- "path"
|
|
|
|
- "strings"
|
|
|
|
- "sync"
|
|
|
|
- "sync/atomic"
|
|
|
|
- "syscall"
|
|
|
|
- "time"
|
|
|
|
)
|
|
)
|
|
|
|
|
|
var (
|
|
var (
|
|
@@ -129,7 +130,7 @@ func (svr *Service) worker() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-//DeferTick 定时执行一个任务
|
|
|
|
|
|
+// DeferTick 定时执行一个任务
|
|
func (svr *Service) DeferTick(duration time.Duration, callback HandleTickerFunc, opts ...TickOption) int64 {
|
|
func (svr *Service) DeferTick(duration time.Duration, callback HandleTickerFunc, opts ...TickOption) int64 {
|
|
tick := &tickPtr{
|
|
tick := &tickPtr{
|
|
sequence: atomic.AddInt64(&tickSequence, 1),
|
|
sequence: atomic.AddInt64(&tickSequence, 1),
|
|
@@ -150,7 +151,7 @@ func (svr *Service) DeferTick(duration time.Duration, callback HandleTickerFunc,
|
|
return tick.sequence
|
|
return tick.sequence
|
|
}
|
|
}
|
|
|
|
|
|
-//Handle 处理函数
|
|
|
|
|
|
+// Handle 处理函数
|
|
func (svr *Service) Handle(method string, cb HandleFunc, opts ...HandleOption) {
|
|
func (svr *Service) Handle(method string, cb HandleFunc, opts ...HandleOption) {
|
|
if atomic.LoadInt32(&svr.readyFlag) != 1 {
|
|
if atomic.LoadInt32(&svr.readyFlag) != 1 {
|
|
svr.deferHandles = append(svr.deferHandles, handleEntry{
|
|
svr.deferHandles = append(svr.deferHandles, handleEntry{
|
|
@@ -161,7 +162,7 @@ func (svr *Service) Handle(method string, cb HandleFunc, opts ...HandleOption) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
//disable cli default
|
|
//disable cli default
|
|
- opt := &HandleOptions{HttpMethod: hp.MethodPost, DisableCli: true}
|
|
|
|
|
|
+ opt := &HandleOptions{HttpMethod: httppkg.MethodPost, DisableCli: true}
|
|
for _, f := range opts {
|
|
for _, f := range opts {
|
|
f(opt)
|
|
f(opt)
|
|
}
|
|
}
|
|
@@ -192,7 +193,7 @@ func (svr *Service) Handle(method string, cb HandleFunc, opts ...HandleOption) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-//NewRequest 创建一个请求
|
|
|
|
|
|
+// NewRequest 创建一个请求
|
|
func (svr *Service) NewRequest(name, method string, body interface{}) (req *Request, err error) {
|
|
func (svr *Service) NewRequest(name, method string, body interface{}) (req *Request, err error) {
|
|
return &Request{
|
|
return &Request{
|
|
ServiceName: name,
|
|
ServiceName: name,
|
|
@@ -202,7 +203,7 @@ func (svr *Service) NewRequest(name, method string, body interface{}) (req *Requ
|
|
}, nil
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
|
|
-//PeekService 选取一个可靠的服务
|
|
|
|
|
|
+// PeekService 选取一个可靠的服务
|
|
func (svr *Service) PeekService(name string) ([]*registry.ServiceNode, error) {
|
|
func (svr *Service) PeekService(name string) ([]*registry.ServiceNode, error) {
|
|
return svr.registry.Get(svr.ctx, name)
|
|
return svr.registry.Get(svr.ctx, name)
|
|
}
|
|
}
|
|
@@ -313,7 +314,7 @@ func (svr *Service) instance() *registry.ServiceNode {
|
|
func (svr *Service) startHTTPServe() (err error) {
|
|
func (svr *Service) startHTTPServe() (err error) {
|
|
svr.httpSvr = http.New(svr.ctx)
|
|
svr.httpSvr = http.New(svr.ctx)
|
|
l := gateway.NewListener(svr.listener.Addr())
|
|
l := gateway.NewListener(svr.listener.Addr())
|
|
- if err = svr.gateway.Attaches([][]byte{[]byte(hp.MethodGet), []byte(hp.MethodPost), []byte(hp.MethodPut), []byte(hp.MethodDelete), []byte(hp.MethodOptions)}, l); err == nil {
|
|
|
|
|
|
+ if err = svr.gateway.Attaches([][]byte{[]byte(httppkg.MethodGet), []byte(httppkg.MethodPost), []byte(httppkg.MethodPut), []byte(httppkg.MethodDelete), []byte(httppkg.MethodOptions)}, l); err == nil {
|
|
svr.async(func() {
|
|
svr.async(func() {
|
|
if err = svr.httpSvr.Serve(l); err != nil && atomic.LoadInt32(&svr.exitFlag) == 0 {
|
|
if err = svr.httpSvr.Serve(l); err != nil && atomic.LoadInt32(&svr.exitFlag) == 0 {
|
|
log.Warnf("http serve error: %s", err.Error())
|
|
log.Warnf("http serve error: %s", err.Error())
|
|
@@ -321,7 +322,7 @@ func (svr *Service) startHTTPServe() (err error) {
|
|
log.Infof("http server stopped")
|
|
log.Infof("http server stopped")
|
|
})
|
|
})
|
|
|
|
|
|
- svr.httpSvr.Handle(hp.MethodGet, "/healthy", func(ctx *http.Context) (err error) {
|
|
|
|
|
|
+ svr.httpSvr.Handle(httppkg.MethodGet, "/healthy", func(ctx *http.Context) (err error) {
|
|
return ctx.Success(map[string]interface{}{
|
|
return ctx.Success(map[string]interface{}{
|
|
"id": svr.node.ID,
|
|
"id": svr.node.ID,
|
|
"healthy": "Healthy",
|
|
"healthy": "Healthy",
|
|
@@ -331,15 +332,15 @@ func (svr *Service) startHTTPServe() (err error) {
|
|
})
|
|
})
|
|
|
|
|
|
if svr.opts.EnableHttpPProf {
|
|
if svr.opts.EnableHttpPProf {
|
|
- svr.httpSvr.Handler("GET", "/debug/pprof/", hp.HandlerFunc(pprof.Index))
|
|
|
|
- svr.httpSvr.Handler("GET", "/debug/pprof/goroutine", hp.HandlerFunc(pprof.Index))
|
|
|
|
- svr.httpSvr.Handler("GET", "/debug/pprof/heap", hp.HandlerFunc(pprof.Index))
|
|
|
|
- svr.httpSvr.Handler("GET", "/debug/pprof/mutex", hp.HandlerFunc(pprof.Index))
|
|
|
|
- svr.httpSvr.Handler("GET", "/debug/pprof/threadcreate", hp.HandlerFunc(pprof.Index))
|
|
|
|
- svr.httpSvr.Handler("GET", "/debug/pprof/cmdline", hp.HandlerFunc(pprof.Cmdline))
|
|
|
|
- svr.httpSvr.Handler("GET", "/debug/pprof/profile", hp.HandlerFunc(pprof.Profile))
|
|
|
|
- svr.httpSvr.Handler("GET", "/debug/pprof/symbol", hp.HandlerFunc(pprof.Symbol))
|
|
|
|
- svr.httpSvr.Handler("GET", "/debug/pprof/trace", hp.HandlerFunc(pprof.Trace))
|
|
|
|
|
|
+ svr.httpSvr.Handler("GET", "/debug/pprof/", httppkg.HandlerFunc(pprof.Index))
|
|
|
|
+ svr.httpSvr.Handler("GET", "/debug/pprof/goroutine", httppkg.HandlerFunc(pprof.Index))
|
|
|
|
+ svr.httpSvr.Handler("GET", "/debug/pprof/heap", httppkg.HandlerFunc(pprof.Index))
|
|
|
|
+ svr.httpSvr.Handler("GET", "/debug/pprof/mutex", httppkg.HandlerFunc(pprof.Index))
|
|
|
|
+ svr.httpSvr.Handler("GET", "/debug/pprof/threadcreate", httppkg.HandlerFunc(pprof.Index))
|
|
|
|
+ svr.httpSvr.Handler("GET", "/debug/pprof/cmdline", httppkg.HandlerFunc(pprof.Cmdline))
|
|
|
|
+ svr.httpSvr.Handler("GET", "/debug/pprof/profile", httppkg.HandlerFunc(pprof.Profile))
|
|
|
|
+ svr.httpSvr.Handler("GET", "/debug/pprof/symbol", httppkg.HandlerFunc(pprof.Symbol))
|
|
|
|
+ svr.httpSvr.Handler("GET", "/debug/pprof/trace", httppkg.HandlerFunc(pprof.Trace))
|
|
}
|
|
}
|
|
log.Infof("http server listen on %s", svr.listener.Addr())
|
|
log.Infof("http server listen on %s", svr.listener.Addr())
|
|
} else {
|
|
} else {
|
|
@@ -451,7 +452,7 @@ func (svr *Service) prepare() (err error) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-//destroy stop and destroy service
|
|
|
|
|
|
+// destroy stop and destroy service
|
|
func (svr *Service) destroy() (err error) {
|
|
func (svr *Service) destroy() (err error) {
|
|
if !atomic.CompareAndSwapInt32(&svr.exitFlag, 0, 1) {
|
|
if !atomic.CompareAndSwapInt32(&svr.exitFlag, 0, 1) {
|
|
return
|
|
return
|
|
@@ -499,7 +500,7 @@ func (svr *Service) destroy() (err error) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-//Reload reload server
|
|
|
|
|
|
+// Reload reload server
|
|
func (svr *Service) Reload() (err error) {
|
|
func (svr *Service) Reload() (err error) {
|
|
if svr.opts.Server == nil {
|
|
if svr.opts.Server == nil {
|
|
return
|
|
return
|
|
@@ -511,7 +512,7 @@ func (svr *Service) Reload() (err error) {
|
|
return svr.opts.Server.Start(svr.ctx)
|
|
return svr.opts.Server.Start(svr.ctx)
|
|
}
|
|
}
|
|
|
|
|
|
-//Run setup service
|
|
|
|
|
|
+// Run setup service
|
|
func (svr *Service) Run() (err error) {
|
|
func (svr *Service) Run() (err error) {
|
|
if svr.opts.EnableLogPrefix {
|
|
if svr.opts.EnableLogPrefix {
|
|
log.Prefix(svr.opts.Name)
|
|
log.Prefix(svr.opts.Name)
|
|
@@ -546,14 +547,14 @@ func (svr *Service) Run() (err error) {
|
|
return svr.destroy()
|
|
return svr.destroy()
|
|
}
|
|
}
|
|
|
|
|
|
-//Shutdown close service
|
|
|
|
|
|
+// Shutdown close service
|
|
func (svr *Service) Shutdown() {
|
|
func (svr *Service) Shutdown() {
|
|
if atomic.LoadInt32(&svr.exitFlag) == 0 {
|
|
if atomic.LoadInt32(&svr.exitFlag) == 0 {
|
|
svr.cancelFunc()
|
|
svr.cancelFunc()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-//Context return the service context
|
|
|
|
|
|
+// Context return the service context
|
|
func (svr *Service) Context() context.Context {
|
|
func (svr *Service) Context() context.Context {
|
|
return svr.ctx
|
|
return svr.ctx
|
|
}
|
|
}
|