Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 582c9e8

Browse files
authored
Sqs/remove dynamic attrs for avro (#13655)
1 parent 0073e19 commit 582c9e8

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

localstack-core/localstack/services/sqs/constants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919

2020
# the default maximum message size in SQS
2121
DEFAULT_MAXIMUM_MESSAGE_SIZE = 1048576
22+
DYNAMIC_ATTRIBUTES = [
23+
# these attributes are not set once, but calculated dynamically, therefore we do not to store them
24+
# internally with the other attributes, and only include them in the necessary responses.
25+
QueueAttributeName.ApproximateNumberOfMessages,
26+
QueueAttributeName.ApproximateNumberOfMessagesDelayed,
27+
QueueAttributeName.ApproximateNumberOfMessagesNotVisible,
28+
]
2229
INTERNAL_QUEUE_ATTRIBUTES = [
2330
# these attributes cannot be changed by set_queue_attributes and should
2431
# therefore be ignored when comparing queue attributes for create_queue

localstack-core/localstack/services/sqs/models.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
TagMap,
2525
)
2626
from localstack.services.sqs import constants as sqs_constants
27+
from localstack.services.sqs.constants import DYNAMIC_ATTRIBUTES
2728
from localstack.services.sqs.exceptions import (
2829
InvalidAttributeValue,
2930
InvalidParameterValueException,
@@ -346,15 +347,6 @@ def shutdown(self):
346347

347348
def default_attributes(self) -> QueueAttributeMap:
348349
return {
349-
QueueAttributeName.ApproximateNumberOfMessages: lambda: str(
350-
self.approx_number_of_messages
351-
),
352-
QueueAttributeName.ApproximateNumberOfMessagesNotVisible: lambda: str(
353-
self.approx_number_of_messages_not_visible
354-
),
355-
QueueAttributeName.ApproximateNumberOfMessagesDelayed: lambda: str(
356-
self.approx_number_of_messages_delayed
357-
),
358350
QueueAttributeName.CreatedTimestamp: str(now()),
359351
QueueAttributeName.DelaySeconds: "0",
360352
QueueAttributeName.LastModifiedTimestamp: str(now()),
@@ -473,15 +465,15 @@ def maximum_message_size(self) -> int:
473465
return int(self.attributes[QueueAttributeName.MaximumMessageSize])
474466

475467
@property
476-
def approx_number_of_messages(self) -> int:
468+
def approximate_number_of_messages(self) -> int:
477469
raise NotImplementedError
478470

479471
@property
480-
def approx_number_of_messages_not_visible(self) -> int:
472+
def approximate_number_of_messages_not_visible(self) -> int:
481473
return len(self.inflight)
482474

483475
@property
484-
def approx_number_of_messages_delayed(self) -> int:
476+
def approximate_number_of_messages_delayed(self) -> int:
485477
return len(self.delayed)
486478

487479
def validate_receipt_handle(self, receipt_handle: str):
@@ -724,7 +716,7 @@ def get_queue_attributes(self, attribute_names: AttributeNameList = None) -> dic
724716
return {}
725717

726718
if QueueAttributeName.All in attribute_names:
727-
attribute_names = self.attributes.keys()
719+
attribute_names = list(self.attributes.keys()) + DYNAMIC_ATTRIBUTES
728720

729721
result: dict[QueueAttributeName, str] = {}
730722

@@ -734,13 +726,18 @@ def get_queue_attributes(self, attribute_names: AttributeNameList = None) -> dic
734726
except AttributeError:
735727
raise InvalidAttributeName(f"Unknown Attribute {attr}.")
736728

737-
value = self.attributes.get(attr)
738-
if callable(value):
739-
func = value
740-
value = func()
741-
if value is not None:
742-
result[attr] = value
743-
elif value == "False" or value == "True":
729+
# The approximate_* attributes are calculated on the spot when accessed.
730+
# We have a @property for each of those which calculates the value.
731+
match attr:
732+
case QueueAttributeName.ApproximateNumberOfMessages:
733+
value = str(self.approximate_number_of_messages)
734+
case QueueAttributeName.ApproximateNumberOfMessagesDelayed:
735+
value = str(self.approximate_number_of_messages_delayed)
736+
case QueueAttributeName.ApproximateNumberOfMessagesNotVisible:
737+
value = str(self.approximate_number_of_messages_not_visible)
738+
case _:
739+
value = self.attributes.get(attr)
740+
if value == "False" or value == "True":
744741
result[attr] = value.lower()
745742
elif value is not None:
746743
result[attr] = value
@@ -799,7 +796,7 @@ def clear(self):
799796
self.visible.queue.clear()
800797

801798
@property
802-
def approx_number_of_messages(self):
799+
def approximate_number_of_messages(self):
803800
return self.visible.qsize()
804801

805802
def shutdown(self):
@@ -1025,7 +1022,7 @@ def __init__(self, name: str, region: str, account_id: str, attributes=None, tag
10251022
self.deduplication_scope = self.attributes[QueueAttributeName.DeduplicationScope]
10261023

10271024
@property
1028-
def approx_number_of_messages(self):
1025+
def approximate_number_of_messages(self):
10291026
n = 0
10301027
for message_group in self.message_groups.values():
10311028
n += len(message_group.messages)

localstack-core/localstack/services/sqs/provider.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,21 +321,21 @@ def publish_approximate_cloudwatch_metrics(self):
321321
SqsMetricBatchData(
322322
QueueName=queue.name,
323323
MetricName="ApproximateNumberOfMessagesVisible",
324-
Value=queue.approx_number_of_messages,
324+
Value=queue.approximate_number_of_messages,
325325
)
326326
)
327327
batch_data.append(
328328
SqsMetricBatchData(
329329
QueueName=queue.name,
330330
MetricName="ApproximateNumberOfMessagesNotVisible",
331-
Value=queue.approx_number_of_messages_not_visible,
331+
Value=queue.approximate_number_of_messages_not_visible,
332332
)
333333
)
334334
batch_data.append(
335335
SqsMetricBatchData(
336336
QueueName=queue.name,
337337
MetricName="ApproximateNumberOfMessagesDelayed",
338-
Value=queue.approx_number_of_messages_delayed,
338+
Value=queue.approximate_number_of_messages_delayed,
339339
)
340340
)
341341

@@ -465,7 +465,7 @@ def submit(self, move_task: MessageMoveTask):
465465
try:
466466
source_queue = self._get_queue_by_arn(move_task.source_arn)
467467
move_task.approximate_number_of_messages_to_move = (
468-
source_queue.approx_number_of_messages
468+
source_queue.approximate_number_of_messages
469469
)
470470
move_task.approximate_number_of_messages_moved = 0
471471
move_task.mark_started()
@@ -1130,7 +1130,7 @@ def receive_message(
11301130
num = override
11311131
elif num == -1:
11321132
# backdoor to get all messages
1133-
num = queue.approx_number_of_messages
1133+
num = queue.approximate_number_of_messages
11341134
elif (
11351135
num < 1 or num > MAX_NUMBER_OF_MESSAGES
11361136
) and not SQS_DISABLE_MAX_NUMBER_OF_MESSAGE_LIMIT:

0 commit comments

Comments
 (0)