@@ -20,33 +20,48 @@ package config
20
20
import (
21
21
"context"
22
22
"fmt"
23
+ "io"
24
+ "strconv"
23
25
24
26
apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
27
+ apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
28
+ "go.uber.org/zap"
29
+ "google.golang.org/grpc"
30
+ "google.golang.org/protobuf/types/known/wrapperspb"
25
31
32
+ api "github.com/polarismesh/polaris/common/api/v1"
33
+ commonlog "github.com/polarismesh/polaris/common/log"
26
34
"github.com/polarismesh/polaris/common/metrics"
27
35
commontime "github.com/polarismesh/polaris/common/time"
28
36
"github.com/polarismesh/polaris/common/utils"
29
37
"github.com/polarismesh/polaris/plugin"
30
38
)
31
39
40
+ var (
41
+ accesslog = commonlog .GetScopeOrDefaultByName (commonlog .APIServerLoggerName )
42
+ )
43
+
32
44
// GetConfigFile 拉取配置
33
45
func (g * ConfigGRPCServer ) GetConfigFile (ctx context.Context ,
34
46
req * apiconfig.ClientConfigFileInfo ) (* apiconfig.ConfigClientResponse , error ) {
35
47
ctx = utils .ConvertGRPCContext (ctx )
36
48
37
49
startTime := commontime .CurrentMillisecond ()
50
+ var ret * apiconfig.ConfigClientResponse
38
51
defer func () {
39
52
plugin .GetStatis ().ReportDiscoverCall (metrics.ClientDiscoverMetric {
53
+ Action : metrics .ActionGetConfigFile ,
40
54
ClientIP : utils .ParseClientAddress (ctx ),
41
55
Namespace : req .GetNamespace ().GetValue (),
42
- Resource : fmt .Sprintf ("CONFIG_FILE:%s|%s|%d" , req .GetGroup ().GetValue (),
43
- req .GetFileName ().GetValue (), req .GetVersion ().GetValue ()),
56
+ Resource : metrics .ResourceOfConfigFile (req .GetGroup ().GetValue (), req .GetFileName ().GetValue ()),
44
57
Timestamp : startTime ,
45
58
CostTime : commontime .CurrentMillisecond () - startTime ,
59
+ Revision : strconv .FormatUint (ret .GetConfigFile ().GetVersion ().GetValue (), 10 ),
60
+ Success : ret .GetCode ().GetValue () > uint32 (apimodel .Code_DataNoChange ),
46
61
})
47
62
}()
48
- response : = g .configServer .GetConfigFileForClient (ctx , req )
49
- return response , nil
63
+ ret = g .configServer .GetConfigFileWithCache (ctx , req )
64
+ return ret , nil
50
65
}
51
66
52
67
// CreateConfigFile 创建或更新配置
@@ -90,17 +105,116 @@ func (g *ConfigGRPCServer) GetConfigFileMetadataList(ctx context.Context,
90
105
req * apiconfig.ConfigFileGroupRequest ) (* apiconfig.ConfigClientListResponse , error ) {
91
106
92
107
startTime := commontime .CurrentMillisecond ()
108
+ var ret * apiconfig.ConfigClientListResponse
93
109
defer func () {
94
110
plugin .GetStatis ().ReportDiscoverCall (metrics.ClientDiscoverMetric {
111
+ Action : metrics .ActionListConfigFiles ,
95
112
ClientIP : utils .ParseClientAddress (ctx ),
96
113
Namespace : req .GetConfigFileGroup ().GetNamespace ().GetValue (),
97
- Resource : fmt .Sprintf ("CONFIG_FILE_LIST:%s|%s" , req .GetConfigFileGroup ().GetName ().GetValue (),
98
- req .GetRevision ().GetValue ()),
114
+ Resource : metrics .ResourceOfConfigFileList (req .GetConfigFileGroup ().GetName ().GetValue ()),
99
115
Timestamp : startTime ,
100
116
CostTime : commontime .CurrentMillisecond () - startTime ,
117
+ Revision : ret .GetRevision ().GetValue (),
118
+ Success : ret .GetCode ().GetValue () > uint32 (apimodel .Code_DataNoChange ),
101
119
})
102
120
}()
103
121
104
122
ctx = utils .ConvertGRPCContext (ctx )
105
- return g .configServer .GetConfigFileNamesWithCache (ctx , req ), nil
123
+ ret = g .configServer .GetConfigFileNamesWithCache (ctx , req )
124
+ return ret , nil
125
+ }
126
+
127
+ func (g * ConfigGRPCServer ) Discover (svr apiconfig.PolarisConfigGRPC_DiscoverServer ) error {
128
+ ctx := utils .ConvertGRPCContext (svr .Context ())
129
+ clientIP , _ := ctx .Value (utils .StringContext ("client-ip" )).(string )
130
+ clientAddress , _ := ctx .Value (utils .StringContext ("client-address" )).(string )
131
+ requestID , _ := ctx .Value (utils .StringContext ("request-id" )).(string )
132
+ userAgent , _ := ctx .Value (utils .StringContext ("user-agent" )).(string )
133
+ method , _ := grpc .MethodFromServerStream (svr )
134
+
135
+ for {
136
+ in , err := svr .Recv ()
137
+ if err != nil {
138
+ if io .EOF == err {
139
+ return nil
140
+ }
141
+ return err
142
+ }
143
+
144
+ msg := fmt .Sprintf ("receive grpc discover request: %s" , in .String ())
145
+ accesslog .Info (msg ,
146
+ zap .String ("type" , apiconfig .ConfigDiscoverRequest_ConfigDiscoverRequestType_name [int32 (in .Type )]),
147
+ zap .String ("client-address" , clientAddress ),
148
+ zap .String ("user-agent" , userAgent ),
149
+ utils .ZapRequestID (requestID ),
150
+ )
151
+
152
+ // 是否允许访问
153
+ if ok := g .allowAccess (method ); ! ok {
154
+ resp := api .NewConfigDiscoverResponse (apimodel .Code_ClientAPINotOpen )
155
+ if sendErr := svr .Send (resp ); sendErr != nil {
156
+ return sendErr
157
+ }
158
+ continue
159
+ }
160
+
161
+ // stream模式,需要对每个包进行检测
162
+ if code := g .enterRateLimit (clientIP , method ); code != uint32 (apimodel .Code_ExecuteSuccess ) {
163
+ resp := api .NewConfigDiscoverResponse (apimodel .Code (code ))
164
+ if err = svr .Send (resp ); err != nil {
165
+ return err
166
+ }
167
+ continue
168
+ }
169
+
170
+ var out * apiconfig.ConfigDiscoverResponse
171
+ var action string
172
+ startTime := commontime .CurrentMillisecond ()
173
+ defer func () {
174
+ plugin .GetStatis ().ReportDiscoverCall (metrics.ClientDiscoverMetric {
175
+ Action : action ,
176
+ ClientIP : utils .ParseClientAddress (ctx ),
177
+ Namespace : in .GetConfigFile ().GetNamespace ().GetValue (),
178
+ Resource : metrics .ResourceOfConfigFile (in .GetConfigFile ().GetGroup ().GetValue (), in .GetConfigFile ().GetFileName ().GetValue ()),
179
+ Timestamp : startTime ,
180
+ CostTime : commontime .CurrentMillisecond () - startTime ,
181
+ Revision : out .GetRevision (),
182
+ Success : out .GetCode () > uint32 (apimodel .Code_DataNoChange ),
183
+ })
184
+ }()
185
+
186
+ switch in .Type {
187
+ case apiconfig .ConfigDiscoverRequest_CONFIG_FILE :
188
+ action = metrics .ActionGetConfigFile
189
+ ret := g .configServer .GetConfigFileWithCache (ctx , & apiconfig.ClientConfigFileInfo {})
190
+ out = api .NewConfigDiscoverResponse (apimodel .Code (ret .GetCode ().GetValue ()))
191
+ out .ConfigFile = ret .GetConfigFile ()
192
+ out .Type = apiconfig .ConfigDiscoverResponse_CONFIG_FILE
193
+ out .Revision = strconv .Itoa (int (out .GetConfigFile ().GetVersion ().GetValue ()))
194
+ case apiconfig .ConfigDiscoverRequest_CONFIG_FILE_Names :
195
+ action = metrics .ActionListConfigFiles
196
+ ret := g .configServer .GetConfigFileNamesWithCache (ctx , & apiconfig.ConfigFileGroupRequest {
197
+ Revision : wrapperspb .String (in .GetRevision ()),
198
+ ConfigFileGroup : & apiconfig.ConfigFileGroup {
199
+ Namespace : in .GetConfigFile ().GetNamespace (),
200
+ Name : in .GetConfigFile ().GetGroup (),
201
+ },
202
+ })
203
+ out = api .NewConfigDiscoverResponse (apimodel .Code (ret .GetCode ().GetValue ()))
204
+ out .ConfigFileNames = ret .GetConfigFileInfos ()
205
+ out .Type = apiconfig .ConfigDiscoverResponse_CONFIG_FILE_Names
206
+ out .Revision = ret .GetRevision ().GetValue ()
207
+ case apiconfig .ConfigDiscoverRequest_CONFIG_FILE_GROUPS :
208
+ action = metrics .ActionListConfigGroups
209
+ req := in .GetConfigFile ()
210
+ req .Md5 = wrapperspb .String (in .GetRevision ())
211
+ out = g .configServer .GetConfigGroupsWithCache (ctx , req )
212
+ default :
213
+ out = api .NewConfigDiscoverResponse (apimodel .Code_InvalidDiscoverResource )
214
+ }
215
+
216
+ if err := svr .Send (out ); err != nil {
217
+ return err
218
+ }
219
+ }
106
220
}
0 commit comments