retry 和 timeout存在httpHandler,并使用ctx传

This commit is contained in:
chenjiekun
2023-03-02 16:35:52 +08:00
parent 44678168a4
commit bb7d82daca
3 changed files with 32 additions and 8 deletions

View File

@@ -12,17 +12,20 @@ import (
"github.com/eolinker/eosc/log" "github.com/eolinker/eosc/log"
) )
const (
KeyHttpRetry = "http_retry"
KeyHttpTimeout = "http_timeout"
)
var ( var (
ErrorTimeoutComplete = errors.New("complete timeout") ErrorTimeoutComplete = errors.New("complete timeout")
) )
type HttpComplete struct { type HttpComplete struct {
retry int
timeOut time.Duration
} }
func NewHttpComplete(retry int, timeOut time.Duration) *HttpComplete { func NewHttpComplete() *HttpComplete {
return &HttpComplete{retry: retry, timeOut: timeOut} return &HttpComplete{}
} }
func (h *HttpComplete) Complete(org eocontext.EoContext) error { func (h *HttpComplete) Complete(org eocontext.EoContext) error {
@@ -56,9 +59,22 @@ func (h *HttpComplete) Complete(org eocontext.EoContext) error {
} }
timeOut := app.TimeOut() 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 return ErrorTimeoutComplete
} }
node, err := balance.Select(ctx) node, err := balance.Select(ctx)

View File

@@ -2,6 +2,7 @@ package http_router
import ( import (
"net/http" "net/http"
"time"
http_service "github.com/eolinker/apinto/node/http-context" http_service "github.com/eolinker/apinto/node/http-context"
@@ -26,10 +27,12 @@ type httpHandler struct {
filters eocontext.IChainPro filters eocontext.IChainPro
disable bool disable bool
websocket bool websocket bool
retry int
timeout time.Duration
} }
func (h *httpHandler) ServeHTTP(ctx eocontext.EoContext) { func (h *httpHandler) ServeHTTP(ctx eocontext.EoContext) {
httpContext, err := http_context.Assert(ctx) httpContext, err := http_context.Assert(ctx)
if err != nil { if err != nil {
return return
@@ -50,6 +53,9 @@ func (h *httpHandler) ServeHTTP(ctx eocontext.EoContext) {
} }
ctx = wsCtx ctx = wsCtx
} }
//set retry timeout
ctx.WithValue(http_complete.KeyHttpRetry, h.retry)
ctx.WithValue(http_complete.KeyHttpTimeout, h.timeout)
//Set Label //Set Label
ctx.SetLabel("api", h.routerName) ctx.SetLabel("api", h.routerName)

View File

@@ -60,6 +60,8 @@ func (h *HttpRouter) reset(cfg *Config, workers map[eosc.RequireId]eosc.IWorker)
finisher: defaultFinisher, finisher: defaultFinisher,
disable: cfg.Disable, disable: cfg.Disable,
websocket: cfg.Websocket, websocket: cfg.Websocket,
retry: cfg.Retry,
timeout: time.Duration(cfg.TimeOut) * time.Millisecond,
} }
if !cfg.Disable { 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) handler.completeHandler = websocket.NewComplete(cfg.Retry, time.Duration(cfg.TimeOut)*time.Millisecond)
methods = []string{http.MethodGet} methods = []string{http.MethodGet}
} else { } else {
handler.completeHandler = http_complete.NewHttpComplete(cfg.Retry, time.Duration(cfg.TimeOut)*time.Millisecond) handler.completeHandler = http_complete.NewHttpComplete()
} }
} }
} }