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
+ return processBucketBuilder (bucketIdBuilder (), input );
36
45
}
37
46
38
47
public RateLimitStrategy noAssignmentStrategy () {
@@ -47,11 +56,34 @@ public RateLimitStrategy expiredAssignmentStrategy() {
47
56
return null ;
48
57
}
49
58
50
- public abstract long reportingIntervalMillis ();
51
-
52
59
public static RlqsBucketSettings create (
53
60
ImmutableMap <String , Function <HttpMatchInput , String >> bucketIdBuilder ,
54
61
Duration reportingInterval ) {
55
- return new AutoValue_RlqsBucketSettings (bucketIdBuilder , Durations .toMillis (reportingInterval ));
62
+ // TODO(sergiitk): instead of create, use Builder pattern.
63
+ RlqsBucketId staticBucketId = processBucketBuilder (bucketIdBuilder , null );
64
+ return new AutoValue_RlqsBucketSettings (
65
+ staticBucketId .isEmpty () ? bucketIdBuilder : null ,
66
+ staticBucketId ,
67
+ Durations .toMillis (reportingInterval ));
68
+ }
69
+
70
+ private static RlqsBucketId processBucketBuilder (
71
+ ImmutableMap <String , Function <HttpMatchInput , String >> bucketIdBuilder ,
72
+ HttpMatchInput input ) {
73
+ ImmutableMap .Builder <String , String > bucketIdMapBuilder = ImmutableMap .builder ();
74
+ if (input == null ) {
75
+ // TODO(sergiitk): [IMPL] calculate static map
76
+ return RlqsBucketId .EMPTY ;
77
+ }
78
+ for (String key : bucketIdBuilder .keySet ()) {
79
+ Function <HttpMatchInput , String > fn = bucketIdBuilder .get (key );
80
+ String value = null ;
81
+ if (fn != null ) {
82
+ value = fn .apply (input );
83
+ }
84
+ bucketIdMapBuilder .put (key , value != null ? value : "" );
85
+ }
86
+ ImmutableMap <String , String > bucketIdMap = bucketIdMapBuilder .build ();
87
+ return bucketIdMap .isEmpty () ? RlqsBucketId .EMPTY : RlqsBucketId .create (bucketIdMap );
56
88
}
57
89
}
0 commit comments