ctxWithValue中的的key改为字符串类型

This commit is contained in:
zhangzeyi
2023-03-15 16:47:12 +08:00
parent 977ae8392e
commit 28ee128a6d
10 changed files with 33 additions and 18 deletions
+3 -2
View File
@@ -2,6 +2,7 @@ package dubbo2_to_http
import (
"github.com/eolinker/apinto/drivers"
"github.com/eolinker/apinto/entries/ctx_key"
"github.com/eolinker/eosc"
"github.com/eolinker/eosc/eocontext"
dubbo2_context "github.com/eolinker/eosc/eocontext/dubbo2-context"
@@ -21,13 +22,13 @@ type ToHttp struct {
func (t *ToHttp) DoDubboFilter(ctx dubbo2_context.IDubbo2Context, next eocontext.IChain) (err error) {
retryValue := ctx.Value(eocontext.CtxKeyRetry)
retryValue := ctx.Value(ctx_key.CtxKeyRetry)
retry, ok := retryValue.(int)
if !ok {
retry = 0
}
timeoutValue := ctx.Value(eocontext.CtxKeyTimeout)
timeoutValue := ctx.Value(ctx_key.CtxKeyTimeout)
timeout, ok := timeoutValue.(time.Duration)
if !ok {
timeout = 3000 * time.Millisecond
+3 -2
View File
@@ -3,6 +3,7 @@ package grpc_to_http
import (
"errors"
"fmt"
"github.com/eolinker/apinto/entries/ctx_key"
"net/url"
"strings"
"time"
@@ -62,13 +63,13 @@ func (h *complete) Complete(org eocontext.EoContext) error {
return err
}
retryValue := ctx.Value(eocontext.CtxKeyRetry)
retryValue := ctx.Value(ctx_key.CtxKeyRetry)
retry, ok := retryValue.(int)
if !ok {
retry = 0
}
timeoutValue := ctx.Value(eocontext.CtxKeyTimeout)
timeoutValue := ctx.Value(ctx_key.CtxKeyTimeout)
timeout, ok := timeoutValue.(time.Duration)
if !ok {
timeout = defaultTimeout
+3 -2
View File
@@ -2,6 +2,7 @@ package http_to_dubbo2
import (
"github.com/eolinker/apinto/drivers"
"github.com/eolinker/apinto/entries/ctx_key"
"github.com/eolinker/eosc"
"github.com/eolinker/eosc/eocontext"
http_context "github.com/eolinker/eosc/eocontext/http-context"
@@ -20,13 +21,13 @@ type ToDubbo2 struct {
func (p *ToDubbo2) DoHttpFilter(ctx http_context.IHttpContext, next eocontext.IChain) error {
retryValue := ctx.Value(eocontext.CtxKeyRetry)
retryValue := ctx.Value(ctx_key.CtxKeyRetry)
retry, ok := retryValue.(int)
if !ok {
retry = 1
}
timeoutValue := ctx.Value(eocontext.CtxKeyTimeout)
timeoutValue := ctx.Value(ctx_key.CtxKeyTimeout)
timeout, ok := timeoutValue.(time.Duration)
if !ok {
timeout = 3000 * time.Millisecond
+3 -2
View File
@@ -5,6 +5,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/eolinker/apinto/entries/ctx_key"
"net/http"
"strings"
"time"
@@ -87,13 +88,13 @@ func (h *complete) Complete(org eocontext.EoContext) error {
return err
}
retryValue := ctx.Value(eocontext.CtxKeyRetry)
retryValue := ctx.Value(ctx_key.CtxKeyRetry)
retry, ok := retryValue.(int)
if !ok {
retry = 1
}
timeoutValue := ctx.Value(eocontext.CtxKeyTimeout)
timeoutValue := ctx.Value(ctx_key.CtxKeyTimeout)
timeout, ok := timeoutValue.(time.Duration)
if !ok {
timeout = defaultTimeout
+3 -2
View File
@@ -2,6 +2,7 @@ package dubbo2_router
import (
"errors"
"github.com/eolinker/apinto/entries/ctx_key"
"time"
"github.com/eolinker/apinto/drivers/router/dubbo2-router/manager"
@@ -41,8 +42,8 @@ func (d *dubboHandler) ServeHTTP(ctx eocontext.EoContext) {
}
//set retry timeout
ctx.WithValue(eocontext.CtxKeyRetry, d.retry)
ctx.WithValue(eocontext.CtxKeyTimeout, d.timeout)
ctx.WithValue(ctx_key.CtxKeyRetry, d.retry)
ctx.WithValue(ctx_key.CtxKeyTimeout, d.timeout)
//Set Label
ctx.SetLabel("api", d.routerName)
+3 -2
View File
@@ -2,6 +2,7 @@ package grpc_router
import (
"github.com/eolinker/apinto/drivers/router/grpc-router/manager"
"github.com/eolinker/apinto/entries/ctx_key"
"github.com/eolinker/apinto/service"
grpc_context "github.com/eolinker/eosc/eocontext/grpc-context"
"google.golang.org/grpc/codes"
@@ -40,8 +41,8 @@ func (h *grpcRouter) ServeHTTP(ctx eocontext.EoContext) {
}
//set retry timeout
ctx.WithValue(eocontext.CtxKeyRetry, h.retry)
ctx.WithValue(eocontext.CtxKeyTimeout, h.timeout)
ctx.WithValue(ctx_key.CtxKeyRetry, h.retry)
ctx.WithValue(ctx_key.CtxKeyTimeout, h.timeout)
//Set Label
ctx.SetLabel("api", h.routerName)
@@ -3,6 +3,7 @@ package http_complete
import (
"errors"
"fmt"
"github.com/eolinker/apinto/entries/ctx_key"
"strconv"
"strings"
"time"
@@ -55,13 +56,13 @@ func (h *HttpComplete) Complete(org eocontext.EoContext) error {
}
timeOut := app.TimeOut()
retryValue := ctx.Value(eocontext.CtxKeyRetry)
retryValue := ctx.Value(ctx_key.CtxKeyRetry)
retry, ok := retryValue.(int)
if !ok {
retry = 1
}
timeoutValue := ctx.Value(eocontext.CtxKeyTimeout)
timeoutValue := ctx.Value(ctx_key.CtxKeyTimeout)
timeout, ok := timeoutValue.(time.Duration)
if !ok {
timeout = 3000 * time.Millisecond
+3 -2
View File
@@ -1,6 +1,7 @@
package http_router
import (
"github.com/eolinker/apinto/entries/ctx_key"
"net/http"
"time"
@@ -54,8 +55,8 @@ func (h *httpHandler) ServeHTTP(ctx eocontext.EoContext) {
ctx = wsCtx
}
//set retry timeout
ctx.WithValue(eocontext.CtxKeyRetry, h.retry)
ctx.WithValue(eocontext.CtxKeyTimeout, h.timeout)
ctx.WithValue(ctx_key.CtxKeyRetry, h.retry)
ctx.WithValue(ctx_key.CtxKeyTimeout, h.timeout)
//Set Label
ctx.SetLabel("api", h.routerName)
+6
View File
@@ -0,0 +1,6 @@
package ctx_key
const (
CtxKeyRetry = "retry"
CtxKeyTimeout = "timeout"
)
+3 -2
View File
@@ -3,6 +3,7 @@ package http_context
import (
"context"
"fmt"
"github.com/eolinker/apinto/entries/ctx_key"
"net"
"strings"
"time"
@@ -211,8 +212,8 @@ func (ctx *HttpContext) Clone() (eoscContext.EoContext, error) {
//记录请求时间
copyContext.ctx = context.WithValue(ctx.Context(), http_service.KeyCloneCtx, true)
copyContext.WithValue(eoscContext.CtxKeyRetry, 0)
copyContext.WithValue(eoscContext.CtxKeyRetry, time.Duration(0))
copyContext.WithValue(ctx_key.CtxKeyRetry, 0)
copyContext.WithValue(ctx_key.CtxKeyRetry, time.Duration(0))
return copyContext, nil
}