17
17
package io .grpc .xds .internal .rlqs ;
18
18
19
19
import com .google .auto .value .AutoValue ;
20
- import com .google .common .base .Function ;
21
20
import com .google .common .collect .ImmutableMap ;
22
21
import com .google .protobuf .Duration ;
23
22
import com .google .protobuf .util .Durations ;
24
23
import io .grpc .xds .internal .datatype .RateLimitStrategy ;
25
24
import io .grpc .xds .internal .matchers .HttpMatchInput ;
26
25
import io .grpc .xds .internal .rlqs .RlqsRateLimitResult .DenyResponse ;
26
+ import java .util .function .Function ;
27
+ import javax .annotation .Nullable ;
27
28
28
29
@ AutoValue
29
30
public abstract class RlqsBucketSettings {
30
31
// TODO(sergiitk): [IMPL] this misses most of the parsing and implementation.
31
32
33
+ @ Nullable
32
34
public abstract ImmutableMap <String , Function <HttpMatchInput , String >> bucketIdBuilder ();
33
35
34
- public RlqsBucketId toBucketId (HttpMatchInput input ) {
35
- return null ;
36
+ abstract RlqsBucketId staticBucketId ();
37
+
38
+ public abstract long reportingIntervalMillis ();
39
+
40
+ public final RlqsBucketId toBucketId (HttpMatchInput input ) {
41
+ if (bucketIdBuilder () == null ) {
42
+ return staticBucketId ();
43
+ }
44
+ ImmutableMap <String , String > bucketMap = processBucketBuilder (bucketIdBuilder (), input );
45
+ return bucketMap != null ? RlqsBucketId .create (bucketMap ) : RlqsBucketId .EMPTY ;
36
46
}
37
47
38
48
public RateLimitStrategy noAssignmentStrategy () {
@@ -47,11 +57,40 @@ public RateLimitStrategy expiredAssignmentStrategy() {
47
57
return null ;
48
58
}
49
59
50
- public abstract long reportingIntervalMillis ();
51
60
52
61
public static RlqsBucketSettings create (
53
62
ImmutableMap <String , Function <HttpMatchInput , String >> bucketIdBuilder ,
54
63
Duration reportingInterval ) {
55
- return new AutoValue_RlqsBucketSettings (bucketIdBuilder , Durations .toMillis (reportingInterval ));
64
+ // TODO(sergiitk): instead of create, use Builder pattern.
65
+ RlqsBucketId staticBucketId = generateStaticId (bucketIdBuilder );
66
+ return new AutoValue_RlqsBucketSettings (
67
+ staticBucketId .isEmpty () ? bucketIdBuilder : null ,
68
+ staticBucketId ,
69
+ Durations .toMillis (reportingInterval ));
70
+ }
71
+
72
+ private static RlqsBucketId generateStaticId (
73
+ ImmutableMap <String , Function <HttpMatchInput , String >> bucketIdBuilder ) {
74
+ ImmutableMap <String , String > bucketIdMap = processBucketBuilder (bucketIdBuilder , null );
75
+ return bucketIdMap .isEmpty () ? RlqsBucketId .EMPTY : RlqsBucketId .create (bucketIdMap );
76
+ }
77
+
78
+ private static ImmutableMap <String , String > processBucketBuilder (
79
+ ImmutableMap <String , Function <HttpMatchInput , String >> bucketIdBuilder ,
80
+ HttpMatchInput input ) {
81
+ ImmutableMap .Builder <String , String > result = ImmutableMap .builder ();
82
+ if (input == null ) {
83
+ // TODO(sergiitk): [IMPL] calculate static map
84
+ return result .build ();
85
+ }
86
+ for (String key : bucketIdBuilder .keySet ()) {
87
+ Function <HttpMatchInput , String > fn = bucketIdBuilder .get (key );
88
+ String value = null ;
89
+ if (fn != null ) {
90
+ value = fn .apply (input );
91
+ }
92
+ result .put (key , value != null ? value : "" );
93
+ }
94
+ return result .build ();
56
95
}
57
96
}
0 commit comments