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

Wrap callbacks in newtype to make wrong usage harder #161

Merged
merged 1 commit into from
Dec 3, 2020

Conversation

phile314
Copy link
Contributor

@phile314 phile314 commented Nov 18, 2020

I was at first struggling to use the callbacks correctly. Granted, this was mostly my fault as I looked at the wrong hackage version of hw-client which did not yet have the examples in the doc on how to use the callbacks. But anyway, I find having the ... -> k -> IO () in the type signature a bit confusing. I was basically doing:

producer <- newProducer ...
statsCallback cb producer

instead of using setCallback and the producer properties.

I think just wrapping the callbacks in a newtype makes it hard (impossible?) to make this mistake and improves readability. What do you think?

PS: Backward-compatiblity is only affected if people gave explicity type signatures for any variable containing the callback. If one just directly passes the callback to setCallback no change is required. E.g.:

  -- Global producer properties
  producerProps :: ProducerProperties
  producerProps = brokersList [BrokerAddress "localhost:9092"]
               <> sendTimeout (Timeout 10000)
               <> setCallback (deliveryCallback print)
               <> logLevel KafkaLogDebug

stil compiles.

Copy link
Contributor

@sir4ur0n sir4ur0n left a comment

Choose a reason for hiding this comment

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

This is a great idea, thank you for this PR!

I put some comments where I think we can improve the API.
Other than that, I am 100% onboard with these changes (though I'm not the project owner so I can't merge it 😄 )

where
realCb k err pl = do
realCb kc k err pl = do
Copy link
Contributor

Choose a reason for hiding this comment

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

Same remark

where
realCb k err pl = do
realCb kc k err pl = do
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason why you also renamed this variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue here is that because the KafkaConf argument moves into the newtype, the variable is now bound in the lambda instead of the function definition:

   Callback $ \kc@(KafkaConf con _ _) -> rdKafkaConfSetRebalanceCb con (realCb kc)

This means kc is no longer available in the where-clause, hence I added it as an extra argument to realCb. The other obvious alternative would be to move realCb into a let, but I am not sure that is better.

Copy link
Contributor

Choose a reason for hiding this comment

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

My apologies, I misread, I thought k was renamed to kc but it's another argument

src/Kafka/Consumer/ConsumerProperties.hs Show resolved Hide resolved
src/Kafka/Internal/Setup.hs Show resolved Hide resolved
@phile314 phile314 force-pushed the wrapper branch 3 times, most recently from 0368fe0 to 2a29065 Compare November 25, 2020 06:40
Copy link
Contributor

@sir4ur0n sir4ur0n left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@AlexeyRaga what do you think? Keep in mind this is a breaking change, so this should probably be documented somewhere (like a changelog) to help users migrate

@AlexeyRaga
Copy link
Member

Thanks guys, I will review it soon!

@AlexeyRaga
Copy link
Member

Looks good to me! Thanks @phile314, I like it! 👍

If you could rebase (I merged a couple of your other PRs) and resolve conflicts then we can hit the green button and make a release!

@phile314
Copy link
Contributor Author

phile314 commented Dec 3, 2020

Rebased on current master.

And thanks already for making a release with these changes, it is greatly appreciated 👍

@AlexeyRaga AlexeyRaga merged commit abd5807 into haskell-works:master Dec 3, 2020
phile314 added a commit to phile314/hw-kafka-client that referenced this pull request Jan 21, 2021
AlexeyRaga added a commit that referenced this pull request Jan 21, 2021
Fix delivery callback (fallout from #161)
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.

3 participants