1717package io .grpc .xds .internal .rlqs ;
1818
1919import com .google .auto .value .AutoValue ;
20- import com .google .common .base .Function ;
2120import com .google .common .collect .ImmutableMap ;
2221import com .google .protobuf .Duration ;
2322import com .google .protobuf .util .Durations ;
2423import io .grpc .xds .internal .datatype .RateLimitStrategy ;
2524import io .grpc .xds .internal .matchers .HttpMatchInput ;
2625import io .grpc .xds .internal .rlqs .RlqsRateLimitResult .DenyResponse ;
26+ import java .util .function .Function ;
27+ import javax .annotation .Nullable ;
2728
2829@ AutoValue
2930public abstract class RlqsBucketSettings {
3031 // TODO(sergiitk): [IMPL] this misses most of the parsing and implementation.
3132
33+ @ Nullable
3234 public abstract ImmutableMap <String , Function <HttpMatchInput , String >> bucketIdBuilder ();
3335
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 );
3645 }
3746
3847 public RateLimitStrategy noAssignmentStrategy () {
@@ -47,11 +56,34 @@ public RateLimitStrategy expiredAssignmentStrategy() {
4756 return null ;
4857 }
4958
50- public abstract long reportingIntervalMillis ();
51-
5259 public static RlqsBucketSettings create (
5360 ImmutableMap <String , Function <HttpMatchInput , String >> bucketIdBuilder ,
5461 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 );
5688 }
5789}
0 commit comments