mirror of
https://github.com/eolinker/apinto
synced 2025-10-05 08:47:04 +08:00
retry 和 timeout存在httpHandler,并使用ctx传
This commit is contained in:
@@ -12,17 +12,20 @@ import (
|
||||
"github.com/eolinker/eosc/log"
|
||||
)
|
||||
|
||||
const (
|
||||
KeyHttpRetry = "http_retry"
|
||||
KeyHttpTimeout = "http_timeout"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrorTimeoutComplete = errors.New("complete timeout")
|
||||
)
|
||||
|
||||
type HttpComplete struct {
|
||||
retry int
|
||||
timeOut time.Duration
|
||||
}
|
||||
|
||||
func NewHttpComplete(retry int, timeOut time.Duration) *HttpComplete {
|
||||
return &HttpComplete{retry: retry, timeOut: timeOut}
|
||||
func NewHttpComplete() *HttpComplete {
|
||||
return &HttpComplete{}
|
||||
}
|
||||
|
||||
func (h *HttpComplete) Complete(org eocontext.EoContext) error {
|
||||
@@ -56,9 +59,22 @@ func (h *HttpComplete) Complete(org eocontext.EoContext) error {
|
||||
|
||||
}
|
||||
timeOut := app.TimeOut()
|
||||
for index := 0; index <= h.retry; index++ {
|
||||
|
||||
if h.timeOut > 0 && time.Now().Sub(proxyTime) > h.timeOut {
|
||||
retryValue := ctx.Value(KeyHttpRetry)
|
||||
retry, ok := retryValue.(int)
|
||||
if !ok {
|
||||
retry = 1
|
||||
}
|
||||
|
||||
timeoutValue := ctx.Value(KeyHttpTimeout)
|
||||
timeout, ok := timeoutValue.(time.Duration)
|
||||
if !ok {
|
||||
timeout = 3000 * time.Millisecond
|
||||
}
|
||||
|
||||
for index := 0; index <= retry; index++ {
|
||||
|
||||
if timeout > 0 && time.Now().Sub(proxyTime) > timeout {
|
||||
return ErrorTimeoutComplete
|
||||
}
|
||||
node, err := balance.Select(ctx)
|
||||
|
@@ -2,6 +2,7 @@ package http_router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
http_service "github.com/eolinker/apinto/node/http-context"
|
||||
|
||||
@@ -26,10 +27,12 @@ type httpHandler struct {
|
||||
filters eocontext.IChainPro
|
||||
disable bool
|
||||
websocket bool
|
||||
|
||||
retry int
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func (h *httpHandler) ServeHTTP(ctx eocontext.EoContext) {
|
||||
|
||||
httpContext, err := http_context.Assert(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -50,6 +53,9 @@ func (h *httpHandler) ServeHTTP(ctx eocontext.EoContext) {
|
||||
}
|
||||
ctx = wsCtx
|
||||
}
|
||||
//set retry timeout
|
||||
ctx.WithValue(http_complete.KeyHttpRetry, h.retry)
|
||||
ctx.WithValue(http_complete.KeyHttpTimeout, h.timeout)
|
||||
|
||||
//Set Label
|
||||
ctx.SetLabel("api", h.routerName)
|
||||
|
@@ -60,6 +60,8 @@ func (h *HttpRouter) reset(cfg *Config, workers map[eosc.RequireId]eosc.IWorker)
|
||||
finisher: defaultFinisher,
|
||||
disable: cfg.Disable,
|
||||
websocket: cfg.Websocket,
|
||||
retry: cfg.Retry,
|
||||
timeout: time.Duration(cfg.TimeOut) * time.Millisecond,
|
||||
}
|
||||
|
||||
if !cfg.Disable {
|
||||
@@ -94,7 +96,7 @@ func (h *HttpRouter) reset(cfg *Config, workers map[eosc.RequireId]eosc.IWorker)
|
||||
handler.completeHandler = websocket.NewComplete(cfg.Retry, time.Duration(cfg.TimeOut)*time.Millisecond)
|
||||
methods = []string{http.MethodGet}
|
||||
} else {
|
||||
handler.completeHandler = http_complete.NewHttpComplete(cfg.Retry, time.Duration(cfg.TimeOut)*time.Millisecond)
|
||||
handler.completeHandler = http_complete.NewHttpComplete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user