Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a mix of locator and creation configs for SNS #210

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

kibertoad
Copy link
Owner

@kibertoad kibertoad commented Sep 19, 2024

please review carefully, this was coded in a late night session using the scraps of the mental capacity, likely to be ugly and have gaps.

would be great to clean up the logic and split the code in the follow up prs.

implementation rushed to unblock Billinq service :-/

@@ -85,6 +85,8 @@ export abstract class AbstractSnsSqsConsumer<
this.creationConfig.queue,
this.creationConfig.topic,
this.subscriptionConfig,
undefined,
this.locatorConfig,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS is complaining now that initSnsSqs().queueUrl is string|undefined.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kibertoad I solved this by adding if to check if queue url is present. From the code it looks it should be mandatory parameter if subscriptionArn is provided, we must provide queueUrl too. What do you think?

Copy link
Owner Author

@kibertoad kibertoad Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't there branching after the check you added, where we skip the queueUrl-related logic if it's not present?

we should support passing queueName instead of queueUrl, no? or we always resolve url based on name earlier?

Copy link
Collaborator

@kamilwylegala kamilwylegala Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is branch with check, but it didn't guarantee that queueUrl is present after that:

  let queueName: string
  if ((locatorConfig as SQSQueueLocatorType).queueUrl) {
    const splitUrl = (locatorConfig as SQSQueueLocatorType).queueUrl.split('/')
    queueName = splitUrl[splitUrl.length - 1]
  } else {
    queueName = creationConfig!.queue.QueueName!
  }

  return {
    subscriptionArn: locatorConfig.subscriptionArn,
    topicArn: subscriptionTopicArn,
    queueUrl: locatorConfig.queueUrl,
    queueName,
  }

From this code it looks that queueUrl is mandatory.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see it. if queueUrl is not present here, we will just return queueUrl: undefined

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, but queueUrl cannot be null. It's required in AbstractSnsSqsConsumer, see line: 104

export type SNSSQSQueueLocatorType = SQSQueueLocatorType &
SNSQueueLocatorType & {
export type SNSSQSQueueLocatorType = Partial<SQSQueueLocatorType> &
SNSTopicLocatorType & {
Copy link
Collaborator

@kamilwylegala kamilwylegala Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How configuration in billing service could look like?

Arguments passed to constructor should be wrapped with isTest() e.g.:

{
  "creationConfig": isTest() ? { /* legit creation config */ } : undefined, //In production creation of topics will be disabled.
  "locatorConfig": isTest() ? undefined : { /* legit locate config */ } //In production we will do read-only
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say that creationConfig would always have an entry for the queue, and conditionally will include entry for the topic.
locatorConfig will conditionally either have an entry for the topic, or be empty

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me visualize it with the test. I think the rest of the comments will clarify too.


return {
subscriptionArn: locatorConfig.subscriptionArn,
topicArn: locatorConfig.topicArn,
topicArn: subscriptionTopicArn,
queueUrl: locatorConfig.queueUrl,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it mix the configuration of both: locatorConfig and creationConfig? Both can be provided and we return queueName from creation config but queue url comes from locate config.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we only follow this branch in case we ended up using locate config, but I might be wrong, worth checking

subscriptionConfiguration: SNSSubscriptionOptions,
extraParams?: ExtraParams,
topicLocator?: SNSTopicLocatorType,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we rework this method and introduce a breaking change?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potentially. if you see a clear way to do this that would simplify the code, go for it

creationConfig?: SNSCreationConfig,
) {
if (locatorConfig) {
const checkResult = await getTopicAttributes(snsClient, locatorConfig.topicArn)
const topicArn =
locatorConfig.topicArn ?? (await getTopicArnByName(snsClient, locatorConfig.topicName))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it needed? It looks like new API not related to support both creation/locate configs.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is needed to support both having a predefined arn and a lookup by topic name, which new logic relies upon

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so precedence will always be:

  • use ARN if present
  • then resolve by name (async lookup)

@kamilwylegala
Copy link
Collaborator

EOR1

@kibertoad kibertoad marked this pull request as ready for review September 23, 2024 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants