/
call.go
143 lines (122 loc) · 3.51 KB
/
call.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package remote
import (
"errors"
"os"
"strconv"
"strings"
)
import (
_ "github.com/apache/dubbo-go/cluster/cluster_impl"
_ "github.com/apache/dubbo-go/cluster/loadbalance"
_ "github.com/apache/dubbo-go/filter/filter_impl"
_ "github.com/apache/dubbo-go/registry/protocol"
_ "github.com/apache/dubbo-go/registry/zookeeper"
"github.com/dubbogo/dubbo-go-pixiu-filter/pkg/api/config"
fc "github.com/dubbogo/dubbo-go-pixiu-filter/pkg/context"
"github.com/dubbogo/dubbo-go-pixiu-filter/pkg/filter"
)
import (
"github.com/apache/dubbo-go-pixiu/pkg/client"
"github.com/apache/dubbo-go-pixiu/pkg/client/dubbo"
clienthttp "github.com/apache/dubbo-go-pixiu/pkg/client/http"
"github.com/apache/dubbo-go-pixiu/pkg/common/constant"
"github.com/apache/dubbo-go-pixiu/pkg/common/extension"
contexthttp "github.com/apache/dubbo-go-pixiu/pkg/context/http"
"github.com/apache/dubbo-go-pixiu/pkg/logger"
)
// nolint
func Init() {
extension.SetFilterFunc(constant.RemoteCallFilter, remoteFilterFunc())
}
func remoteFilterFunc() fc.FilterFunc {
return New(defaultNewParams()).Do()
}
func defaultNewParams() mockLevel {
mock := 1
mockStr := os.Getenv(constant.EnvMock)
if len(mockStr) > 0 {
i, err := strconv.Atoi(mockStr)
if err == nil {
mock = i
}
}
return mockLevel(mock)
}
type mockLevel int8
const (
open = iota
close
all
)
// clientFilter is a filter for recover.
type clientFilter struct {
level mockLevel
}
// New create timeout filter.
func New(level mockLevel) filter.Filter {
if level < 0 || level > 2 {
level = close
}
return &clientFilter{
level: level,
}
}
// Do execute clientFilter filter logic
// support: 1 http 2 dubbo 2 http 2 http
func (f clientFilter) Do() fc.FilterFunc {
return func(c fc.Context) {
f.doRemoteCall(c.(*contexthttp.HttpContext))
}
}
func (f clientFilter) doRemoteCall(c *contexthttp.HttpContext) {
api := c.GetAPI()
if (f.level == open && api.Mock) || (f.level == all) {
c.SourceResp = &filter.ErrResponse{
Message: "mock success",
}
c.Next()
return
}
typ := api.Method.IntegrationRequest.RequestType
cli, err := matchClient(typ)
if err != nil {
c.Err = err
return
}
resp, err := cli.Call(client.NewReq(c.Ctx, c.Request, *api))
if err != nil {
logger.Errorf("[dubbo-go-pixiu] client call err:%v!", err)
c.Err = err
return
}
logger.Debugf("[dubbo-go-pixiu] client call resp:%v", resp)
c.SourceResp = resp
// response write in response filter.
c.Next()
}
func matchClient(typ config.RequestType) (client.Client, error) {
switch strings.ToLower(string(typ)) {
case string(config.DubboRequest):
return dubbo.SingletonDubboClient(), nil
case string(config.HTTPRequest):
return clienthttp.SingletonHTTPClient(), nil
default:
return nil, errors.New("not support")
}
}