Skip to content

Commit

Permalink
Merge remote-tracking branch 'gitlab/hotfix/label'
Browse files Browse the repository at this point in the history
  • Loading branch information
Dot-Liu committed Mar 17, 2023
2 parents 6db3ae7 + 8b0626f commit a2468c9
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 36 deletions.
7 changes: 7 additions & 0 deletions checker/multiple.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func (ls listChecker) Check(v string, has bool) bool {

func (m *multipleChecker) Check(v string, has bool) bool {
if has && m.equals != nil {
//全选逻辑处理
for k, _ := range m.equals {
if k == "ALL" {
return true
}
}

if ok := m.equals[v]; ok {
return true
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/app/manager/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ func (a *AppData) All() []string {
a.locker.RLock()
defer a.locker.RUnlock()
idMap := make(map[string]bool)

for _, v := range a.data {
for _, id := range v {
if _, ok := idMap[id]; !ok {
idMap[id] = true
}
}
}

ids := make([]string, 0, len(idMap))
for key := range idMap {
ids = append(ids, key)
Expand Down
22 changes: 13 additions & 9 deletions drivers/app/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,21 @@ func (m *Manager) Set(app application.IApp, filters []application.IAuth, users m
}
idMap[filter.Driver()] = append(idMap[filter.Driver()], filter.ID())
}
idArr := make([]string, 0, len(idMap))
for driver, ids := range idMap {
old := m.appManager.GetByAppID(app.Id())
m.appManager.Set(app.Id(), driver, ids)
cs := compareArray(old, ids)
for id := range cs {
filter, has := m.Untyped.Get(id)
if has {
filter.Del(app.Id())
if filter.UserCount() == 0 {
m.Untyped.Del(id)
}
idArr = append(idArr, ids...)
}

// 比较新旧id,删除旧配置
old := m.appManager.GetByAppID(app.Id())
cs := compareArray(old, idArr)
for id := range cs {
filter, has := m.Untyped.Get(id)
if has {
filter.Del(app.Id())
if filter.UserCount() == 0 {
m.Untyped.Del(id)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/plugins/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func anonymousAppHandler(ctx http_service.IHttpContext) (bool, error) {
}

func (a *App) auth(ctx http_service.IHttpContext) error {
log.Error("start auth...")
log.Debug("start auth...")
if appManager.Count() < 1 {
_, err := anonymousAppHandler(ctx)
return err
Expand All @@ -99,7 +99,7 @@ func (a *App) auth(ctx http_service.IHttpContext) error {
setLabels(ctx, user.App.Labels())
ctx.SetLabel("application_id", user.App.Id())
ctx.SetLabel("application", user.App.Name())
log.Error("application name is ", user.App.Name())
log.Debug("application name is ", user.App.Name())
if user.HideCredential {
application.HideToken(ctx, user.TokenName, user.Position)
}
Expand Down
16 changes: 15 additions & 1 deletion drivers/plugins/dubbo2-to-http/to-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dubbo2_to_http

import (
"github.com/eolinker/apinto/drivers"
"github.com/eolinker/apinto/entries/ctx_key"
"github.com/eolinker/apinto/entries/router"
"github.com/eolinker/eosc"
"github.com/eolinker/eosc/eocontext"
dubbo2_context "github.com/eolinker/eosc/eocontext/dubbo2-context"
Expand All @@ -21,7 +23,19 @@ type ToHttp struct {

func (t *ToHttp) DoDubboFilter(ctx dubbo2_context.IDubbo2Context, next eocontext.IChain) (err error) {

complete := NewComplete(0, time.Second*30, t.contentType, t.path, t.method, t.params)
retryValue := ctx.Value(ctx_key.CtxKeyRetry)
retry, ok := retryValue.(int)
if !ok {
retry = router.DefaultRetry
}

timeoutValue := ctx.Value(ctx_key.CtxKeyTimeout)
timeout, ok := timeoutValue.(time.Duration)
if !ok {
timeout = router.DefaultTimeout
}

complete := NewComplete(retry, timeout, t.contentType, t.path, t.method, t.params)

ctx.SetCompleteHandler(complete)

Expand Down
23 changes: 17 additions & 6 deletions drivers/plugins/gRPC-to-http/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package grpc_to_http
import (
"errors"
"fmt"
"github.com/eolinker/apinto/entries/ctx_key"
"github.com/eolinker/apinto/entries/router"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -40,19 +42,15 @@ type complete struct {
headers map[string]string
rawQuery string
path string
retry int
timeout time.Duration
}

func newComplete(descriptor grpc_descriptor.IDescriptor, conf *Config) *complete {
query := url.Values{}
for key, value := range conf.Query {
query.Set(key, value)
}
timeout := defaultTimeout
return &complete{
descriptor: descriptor,
timeout: timeout,
rawQuery: query.Encode(),
path: conf.Path,
headers: conf.Headers,
Expand All @@ -65,6 +63,19 @@ func (h *complete) Complete(org eocontext.EoContext) error {
if err != nil {
return err
}

retryValue := ctx.Value(ctx_key.CtxKeyRetry)
retry, ok := retryValue.(int)
if !ok {
retry = router.DefaultRetry
}

timeoutValue := ctx.Value(ctx_key.CtxKeyTimeout)
timeout, ok := timeoutValue.(time.Duration)
if !ok {
timeout = router.DefaultTimeout
}

descriptor, err := h.descriptor.Descriptor().FindSymbol(fmt.Sprintf("%s.%s", ctx.Proxy().Service(), ctx.Proxy().Method()))
if err != nil {
return err
Expand Down Expand Up @@ -96,9 +107,9 @@ func (h *complete) Complete(org eocontext.EoContext) error {
var lastErr error
timeOut := app.TimeOut()
balance := ctx.GetBalance()
for index := 0; index <= h.retry; index++ {
for index := 0; index <= retry; index++ {

if h.timeout > 0 && time.Now().Sub(proxyTime) > h.timeout {
if timeout > 0 && time.Now().Sub(proxyTime) > timeout {
return ErrorTimeoutComplete
}
node, err := balance.Select(ctx)
Expand Down
16 changes: 15 additions & 1 deletion drivers/plugins/http-to-dubbo2/to-dubbo2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package http_to_dubbo2

import (
"github.com/eolinker/apinto/drivers"
"github.com/eolinker/apinto/entries/ctx_key"
"github.com/eolinker/apinto/entries/router"
"github.com/eolinker/eosc"
"github.com/eolinker/eosc/eocontext"
http_context "github.com/eolinker/eosc/eocontext/http-context"
Expand All @@ -20,7 +22,19 @@ type ToDubbo2 struct {

func (p *ToDubbo2) DoHttpFilter(ctx http_context.IHttpContext, next eocontext.IChain) error {

complete := NewComplete(0, time.Second*30, p.service, p.method, p.params)
retryValue := ctx.Value(ctx_key.CtxKeyRetry)
retry, ok := retryValue.(int)
if !ok {
retry = router.DefaultRetry
}

timeoutValue := ctx.Value(ctx_key.CtxKeyTimeout)
timeout, ok := timeoutValue.(time.Duration)
if !ok {
timeout = router.DefaultTimeout
}

complete := NewComplete(retry, timeout, p.service, p.method, p.params)
ctx.SetCompleteHandler(complete)

if next != nil {
Expand Down
24 changes: 17 additions & 7 deletions drivers/plugins/http-to-gRPC/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/eolinker/apinto/entries/ctx_key"
"github.com/eolinker/apinto/entries/router"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -37,27 +39,22 @@ var (
options = grpcurl.FormatOptions{
AllowUnknownFields: true,
}
defaultTimeout = 10 * time.Second
)

type complete struct {
format grpcurl.Format
descriptor grpc_descriptor.IDescriptor
timeout time.Duration
authority string
service string
method string
headers map[string]string
retry int
reflect bool
}

func newComplete(descriptor grpc_descriptor.IDescriptor, conf *Config) *complete {
timeout := defaultTimeout
return &complete{
format: grpcurl.Format(conf.Format),
descriptor: descriptor,
timeout: timeout,
authority: conf.Authority,
service: conf.Service,
method: conf.Method,
Expand Down Expand Up @@ -90,6 +87,19 @@ func (h *complete) Complete(org eocontext.EoContext) error {
if err != nil {
return err
}

retryValue := ctx.Value(ctx_key.CtxKeyRetry)
retry, ok := retryValue.(int)
if !ok {
retry = router.DefaultRetry
}

timeoutValue := ctx.Value(ctx_key.CtxKeyTimeout)
timeout, ok := timeoutValue.(time.Duration)
if !ok {
timeout = router.DefaultTimeout
}

in := strings.NewReader(string(body))

balance := ctx.GetBalance()
Expand All @@ -102,13 +112,13 @@ func (h *complete) Complete(org eocontext.EoContext) error {
symbol := getSymbol(ctx.Proxy().URI().Path(), h.service, h.method)
var lastErr error
var conn *grpc.ClientConn
for i := h.retry + 1; i > 0; i-- {
for i := retry + 1; i > 0; i-- {
node, err := balance.Select(ctx)
if err != nil {
log.Error("select node error: ", err)
return err
}
conn, lastErr = dial(node.Addr(), h.timeout, opts...)
conn, lastErr = dial(node.Addr(), timeout, opts...)
if lastErr != nil {
log.Error("dial error: ", lastErr)
continue
Expand Down
8 changes: 8 additions & 0 deletions drivers/router/dubbo2-router/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dubbo2_router

import (
"errors"
"github.com/eolinker/apinto/entries/ctx_key"
"time"

"github.com/eolinker/apinto/drivers/router/dubbo2-router/manager"
"github.com/eolinker/apinto/router"
Expand All @@ -21,6 +23,8 @@ type dubboHandler struct {
disable bool
service service.IService
filters eocontext.IChainPro
retry int
timeout time.Duration
}

var completeCaller = manager.NewCompleteCaller()
Expand All @@ -37,6 +41,10 @@ func (d *dubboHandler) ServeHTTP(ctx eocontext.EoContext) {
return
}

//set retry timeout
ctx.WithValue(ctx_key.CtxKeyRetry, d.retry)
ctx.WithValue(ctx_key.CtxKeyTimeout, d.timeout)

//Set Label
ctx.SetLabel("api", d.routerName)
ctx.SetLabel("api_id", d.routerId)
Expand Down
2 changes: 2 additions & 0 deletions drivers/router/dubbo2-router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func (h *DubboRouter) reset(cfg *Config, workers map[eosc.RequireId]eosc.IWorker
serviceName: strings.TrimSuffix(string(cfg.Service), "@service"),
disable: cfg.Disable,
filters: nil,
retry: cfg.Retry,
timeout: time.Duration(cfg.TimeOut) * time.Millisecond,
}

if !cfg.Disable {
Expand Down
8 changes: 8 additions & 0 deletions drivers/router/grpc-router/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ 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"
"google.golang.org/grpc/status"
"time"

"github.com/eolinker/eosc/eocontext"
)
Expand All @@ -23,6 +25,8 @@ type grpcRouter struct {
service service.IService
filters eocontext.IChainPro
disable bool
retry int
timeout time.Duration
}

func (h *grpcRouter) ServeHTTP(ctx eocontext.EoContext) {
Expand All @@ -36,6 +40,10 @@ func (h *grpcRouter) ServeHTTP(ctx eocontext.EoContext) {
return
}

//set retry timeout
ctx.WithValue(ctx_key.CtxKeyRetry, h.retry)
ctx.WithValue(ctx_key.CtxKeyTimeout, h.timeout)

//Set Label
ctx.SetLabel("api", h.routerName)
ctx.SetLabel("api_id", h.routerId)
Expand Down
2 changes: 2 additions & 0 deletions drivers/router/grpc-router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func (h *GrpcRouter) reset(cfg *Config, workers map[eosc.RequireId]eosc.IWorker)
finisher: defaultFinisher,
service: nil,
filters: nil,
retry: cfg.Retry,
timeout: time.Duration(cfg.TimeOut) * time.Millisecond,
disable: cfg.Disable,
}

Expand Down
10 changes: 6 additions & 4 deletions drivers/router/http-router/http-complete/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package http_complete
import (
"errors"
"fmt"
"github.com/eolinker/apinto/entries/ctx_key"
"github.com/eolinker/apinto/entries/router"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -55,16 +57,16 @@ func (h *HttpComplete) Complete(org eocontext.EoContext) error {
}
timeOut := app.TimeOut()

retryValue := ctx.Value(http_service.KeyHttpRetry)
retryValue := ctx.Value(ctx_key.CtxKeyRetry)
retry, ok := retryValue.(int)
if !ok {
retry = 1
retry = router.DefaultRetry
}

timeoutValue := ctx.Value(http_service.KeyHttpTimeout)
timeoutValue := ctx.Value(ctx_key.CtxKeyTimeout)
timeout, ok := timeoutValue.(time.Duration)
if !ok {
timeout = 3000 * time.Millisecond
timeout = router.DefaultTimeout
}

for index := 0; index <= retry; index++ {
Expand Down
Loading

0 comments on commit a2468c9

Please sign in to comment.