@@ -7,6 +7,7 @@ package translator
77
88import (
99 "bytes"
10+ "net/url"
1011 "strconv"
1112 "time"
1213
@@ -29,9 +30,9 @@ import (
2930
3031// patchHCMWithRateLimit builds and appends the Rate Limit Filter to the HTTP connection manager
3132// if applicable and it does not already exist.
32- func patchHCMWithRateLimit (mgr * hcm.HttpConnectionManager , irListener * ir.HTTPListener ) {
33+ func ( t * Translator ) patchHCMWithRateLimit (mgr * hcm.HttpConnectionManager , irListener * ir.HTTPListener ) {
3334 // Return early if rate limits dont exist
34- if ! isRateLimitPresent (irListener ) {
35+ if ! t . isRateLimitPresent (irListener ) {
3536 return
3637 }
3738
@@ -48,7 +49,11 @@ func patchHCMWithRateLimit(mgr *hcm.HttpConnectionManager, irListener *ir.HTTPLi
4849}
4950
5051// isRateLimitPresent returns true if rate limit config exists for the listener.
51- func isRateLimitPresent (irListener * ir.HTTPListener ) bool {
52+ func (t * Translator ) isRateLimitPresent (irListener * ir.HTTPListener ) bool {
53+ // Return false if global ratelimiting is disabled.
54+ if t .GlobalRateLimit == nil {
55+ return false
56+ }
5257 // Return true if rate limit config exists.
5358 for _ , route := range irListener .Routes {
5459 if route .RateLimit != nil && route .RateLimit .Global != nil {
@@ -256,14 +261,14 @@ func buildRateLimitServiceDescriptors(descriptorPrefix string, global *ir.Global
256261 return yamlDescs
257262}
258263
259- func buildRateLimitServiceCluster (irListener * ir.HTTPListener ) * cluster.Cluster {
264+ func ( t * Translator ) buildRateLimitServiceCluster (irListener * ir.HTTPListener ) * cluster.Cluster {
260265 // Return early if rate limits dont exist.
261- if ! isRateLimitPresent (irListener ) {
266+ if ! t . isRateLimitPresent (irListener ) {
262267 return nil
263268 }
264269
265270 clusterName := getRateLimitServiceClusterName ()
266- host , port := getRateLimitServiceGrpcHostPort ()
271+ host , port := t . getRateLimitServiceGrpcHostPort ()
267272 rateLimitServerCluster := & cluster.Cluster {
268273 Name : clusterName ,
269274 ClusterDiscoveryType : & cluster.Cluster_Type {Type : cluster .Cluster_STRICT_DNS },
@@ -317,6 +322,14 @@ func getRateLimitDomain(irListener *ir.HTTPListener) string {
317322 return irListener .Name
318323}
319324
320- func getRateLimitServiceGrpcHostPort () (string , int ) {
321- return "TODO" , 0
325+ func (t * Translator ) getRateLimitServiceGrpcHostPort () (string , int ) {
326+ u , err := url .Parse (t .GlobalRateLimit .ServiceURL )
327+ if err != nil {
328+ panic (err )
329+ }
330+ p , err := strconv .Atoi (u .Port ())
331+ if err != nil {
332+ panic (err )
333+ }
334+ return u .Hostname (), p
322335}
0 commit comments