Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Messenger] Make all the dependencies of AmazonSqsTransport injectable#38846

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@jacekwilczynski
Copy link
Contributor

QA
Branch?5.x for features
Bug fix?no
New feature?yes - updated changelog
Deprecations?no
TicketsFix#38640
LicenseMIT
Doc PRsymfony/symfony-docs#...

This is a pure refactoring PR that enables more flexibility with service injection without actually changing any behaviour or breaking backwards compatibility. It satisfies only 1 of 2 acceptance criteria of#38640 but since they're independent, I'm not marking the PR as WIP.

Receiver & sender injection into AmazonSqsTransport

It is now possible to inject your own receiver and sender intoSymfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransport.

Recommended way - AmazonSqsTransport::create

For clean dependency injection, I recommed using thecreate static method, which obliges you to pass all dependencies:

$transport = AmazonSqsTransport::create($connection,$receiver,$sender);

For example, this code fromSymfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory:

returnnewAmazonSqsTransport(Connection::fromDsn($dsn,$options),$serializer);

could be replaced with this:

$connection = Connection::fromDsn($dsn,$options);return AmazonSqsTransport::create($connection,newAmazonSqsReceiver($connection,$serializer),newAmazonSqsSender($connection,$serializer));

I didn't replace that code in the factory because I didn't find it essential but I will certainly do it in my custom factory in my project, passing my own receiver implementation.

Using the main constructor

You can still use the main constructor but it's most suited for backwards compatibility, i.e. when you don't want to inject a receiver or a sender. With the full list of arguments it gets a bit messy due to their optionality.

Minimal call

newAmazonSqsTransport($connection);

As before this PR, a receiver and a sender will be created using the default serializer, i.e.Symfony\Component\Messenger\Transport\Serialization\PhpSerializer.

With a custom serializer

newAmazonSqsTransport($connection,$serializer);

As before this PR, a receiver and a sender will be created using the passed serializer.

With a custom receiver and sender

newAmazonSqsTransport($connection,null,$receiver,$sender);

The injected services will be used. The second parameter (serializer) is unnecessary because it was only ever used while creating a receiver and a sender inside the transport. Because of this, I recommend using the new staticcreate method.

Copy link
Member

@jderussejderusse left a comment

Choose a reason for hiding this comment

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

Thank for this great work.

All transports (amqp,doctrine,redis,sqs...) are currently using the same pattern. And theXxxTransport are identical.

I wonder if it wouldn't make sens to apply this PR to all transports for consistency?

$this->connection =$connection;
$this->serializer =$serializer ??newPhpSerializer();
$serializer =$serializer ??newPhpSerializer();
$this->receiver =$receiver ??newAmazonSqsReceiver($connection,$serializer);
Copy link
Member

Choose a reason for hiding this comment

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

Creation of receiver and sender were lay-loaded on purpose. Let these properties null and revert code in methods below.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

No problem, I can do it, but may I ask why? The default receiver and sender have constructors with just basic assignments so they're extremely cheap to create, I thought it wasn't worth the additional code complexity.

Copy link
Member

@chalasrchalasr left a comment

Choose a reason for hiding this comment

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

Thanks for the PR

@chalasr
Copy link
Member

I wonder if it wouldn't make sens to apply this PR to all transports for consistency?

I think so (let's make sure we are all good on the first one, we have time for 5.3.)

jacekwilczynski reacted with thumbs up emoji

@jacekwilczynski
Copy link
ContributorAuthor

@jderusse ,@chalasr , thanks for the review, I'm really happy to see it so fast :D Forgive me if the question is banal but I couldn't find info in the guidelines: should I push another commit with the fixes or amend the previous one?

@chalasr
Copy link
Member

@jacekwilczynski You can amend. If you don't, we will squash your commits when merging the PR

@jacekwilczynskijacekwilczynskiforce-pushed theamazon-sqs-extensibility branch 3 times, most recently from0291fd5 to828a0f4CompareOctober 28, 2020 16:01
@jacekwilczynskijacekwilczynskiforce-pushed theamazon-sqs-extensibility branch 2 times, most recently fromefd1863 to913d581CompareNovember 3, 2020 13:37
@jacekwilczynski
Copy link
ContributorAuthor

@chalasr , anything I should still change in this PR?

Copy link
Member

@derrabusderrabus left a comment

Choose a reason for hiding this comment

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

What I was wondering: Do we really need two ways to construct aAmazonSqsTransport? If not, can we change the constructor to this?

public function __construct(Connection $connection, ReceiverInterface $receiver = null, SenderInterface $sender = null)

Other than that, the change looks reasonable.

@jderusse
Copy link
Member

What I was wondering: Do we really need two ways to construct aAmazonSqsTransport? If not, can we change the constructor to this?

Hmm aSerializer is required to build aReceiver and aSender.. I'm not sure to get what you mean 🤔

@derrabus
Copy link
Member

Hmm aSerializer is required to build aReceiver and aSender..

Is it? Right now, the constructor is able to build aReceiver and aSender even if I provide a connection only.

And if you want to change the serializer implementation, you could still do that with the signature I proposed by constructingReceiver andSender yourself.

@jderusse
Copy link
Member

And if you want to change the serializer implementation, you could still do that with the signature I proposed by constructing Receiver and Sender yourself.

I mean, today, all bridges lazy build the sender/receiver (I don't know why. As already pointed in this PR, this looks like a micro-optimization).
Anyway, removing the serializer (which is currently injected by the TransportFactory regarding the semantic configuration) would break the initial behavior.
When people provides a custom implementation of Serializer, The factory would have to create a Transport with both a Sender AND a Receiver (which will not be lazy built anymore).

derrabus and jacekwilczynski reacted with thumbs up emoji

@derrabus
Copy link
Member

Let's keep it that way, thanks for the explanation!

jacekwilczynski reacted with thumbs up emoji

Copy link
Member

@derrabusderrabus left a comment

Choose a reason for hiding this comment

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

Let's wait until 5.2 has been branched out and merge to 5.x then.

jacekwilczynski reacted with thumbs up emoji
@derrabusderrabusforce-pushed theamazon-sqs-extensibility branch from913d581 to281af26CompareNovember 15, 2020 22:33
@derrabus
Copy link
Member

Thank you@jacekwilczynski.

jacekwilczynski reacted with hooray emoji

@derrabusderrabus merged commitb77ef9f intosymfony:5.xNov 15, 2020
@jacekwilczynskijacekwilczynski deleted the amazon-sqs-extensibility branchJanuary 26, 2021 08:06
@fabpotfabpot mentioned this pull requestApr 18, 2021
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@jderussejderussejderusse approved these changes

@chalasrchalasrchalasr requested changes

@derrabusderrabusderrabus approved these changes

@srozesrozeAwaiting requested review from sroze

Assignees

No one assigned

Projects

None yet

Milestone

5.4

Development

Successfully merging this pull request may close these issues.

Use DI & SRP to make Amazon SQS Messenger more extensible

5 participants

@jacekwilczynski@chalasr@jderusse@derrabus@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp