2424
2525
2626def get_weight (invoker : Invoker , invocation : Invocation ) -> int :
27- """
28- Get the weight of the invoker with warmup capability.
29-
30- This method calculates weight based on URL parameters and warming up time.
31- New services need warmup time to be fully functional, during this period
32- their weight is gradually increased.
33-
34- :param invoker: The service invoker.
35- :type invoker: Invoker
36- :param invocation: The service invocation context
37- :type invocation: Invocation
38- :return: Calculated weight value (minimum 0)
39- :rtype: int
27+ """Get the weight of the invoker with warmup capability.
28+
29+ Calculates weight based on URL parameters and warmup time. New services
30+ need warmup time to be fully functional, during which their weight is
31+ gradually increased from 1 to the configured value.
32+
33+ Args:
34+ invoker: The service invoker containing URL configuration.
35+ invocation: The service invocation context with method information.
36+
37+ Returns:
38+ Calculated weight value, minimum 0.
4039 """
4140 url = invoker .get_url ()
4241 # TODO: Multiple registry scenario, load balance among multiple registries.
4342 is_multiple = False
4443
4544 # Determine weight based on URL parameters
4645 if is_multiple :
47- weight = url .get_param_int (constants .WEIGHT_KEY , constants .DEFAULT_WEIGHT )
46+ weight = url .get_param_int (constants .WEIGHT_KEY , constants .DEFAULT_WEIGHT_VALUE )
4847 else :
49- weight = url .get_method_param_int (invocation .method_name , constants .WEIGHT_KEY , constants .DEFAULT_WEIGHT )
48+ weight = url .get_method_param_int (invocation .method_name , constants .WEIGHT_KEY , constants .DEFAULT_WEIGHT_VALUE )
5049
5150 # Apply warmup adjustment for positive weights only
5251 if weight > 0 :
5352 timestamp = url .get_param_int (constants .TIMESTAMP_KEY , 0 )
5453 if timestamp > 0 :
5554 # Calculate service uptime in milliseconds
5655 uptime = max (int (time .time () * 1000 ) - timestamp , 1 )
57- warmup = url .get_param_int (constants .WARMUP_KEY , constants .DEFAULT_WARMUP )
56+ warmup = url .get_param_int (constants .WARMUP_KEY , constants .DEFAULT_WARMUP_VALUE )
5857
5958 # Adjust weight during warmup period
6059 if 0 < uptime < warmup :
@@ -67,22 +66,22 @@ def get_weight(invoker: Invoker, invocation: Invocation) -> int:
6766
6867
6968class LoadBalance (abc .ABC ):
70- """
71- The load balance interface.
69+ """Base class for load balancing strategies.
7270
71+ Defines the interface for selecting an invoker from a list of available
72+ invokers based on the load balancing algorithm implementation.
7373 """
7474
7575 @abc .abstractmethod
7676 def select (self , invokers : list [Invoker ], url : URL , invocation : Invocation ) -> Optional [Invoker ]:
77- """
78- Select an invoker from the list.
79- :param invokers: The invokers.
80- :type invokers: List[Invoker]
81- :param url: The URL.
82- :type url: URL
83- :param invocation: The invocation.
84- :type invocation: Invocation
85- :return: The selected invoker. If no invoker is selected, return None.
86- :rtype: Optional[Invoker]
77+ """Select an invoker from the available invokers list.
78+
79+ Args:
80+ invokers: List of available service invokers.
81+ url: The request URL with configuration parameters.
82+ invocation: The service invocation context.
83+
84+ Returns:
85+ The selected invoker, or None if no suitable invoker is found.
8786 """
8887 raise NotImplementedError ()
0 commit comments