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

Counters are not incremented before async producers are triggered #175

Open
danielsitnik opened this issue Aug 24, 2022 · 1 comment
Open

Comments

@danielsitnik
Copy link

danielsitnik commented Aug 24, 2022

Hello guys.

I have a use case where the client calls an HTTP endpoint, receives a 202 status code, and the real response payload is delivered asynchronously through a queue. So far I have been able to model this use case in mockintosh by using an async producer and triggering it in my endpoint with triggerAsyncProducer.

My problem right now is that the full use case requires the endpoint to return a correlation-id to the caller, and the async message delivered later must reference this same correlation-id.

I almost made it work by using the counters feature, with a config like this:

services:
- name: Correlation Service
  endpoints:
  - path: /correlation
    response:
      status: 202
      body: '{"correlationId": "{{counter ''correlationIdCounter''}}"}'
      triggerAsyncProducer: rabbit-producer
- name: Rabbit Service
  type: amqp
  address: localhost:5672
  actors:
  - name: rabbit-producer
    produce:
      queue: mockintosh
      key: mockintosh
      value: '{"message": "ok", "correlationId": "{{correlationIdCounter}}"}'

My problem right now is that it seems that the async message is produced BEFORE the counter has been incremented.
So in the first execution, the http endpoint correctly gives me a correlation-id of 1, but I receive this in my queue:

{"message": "ok", "correlationId": "{{correlationIdCounter}}"}

For the next executions, the http endpoint correctly returns the incremented counter, but the produced message is always one step behind.

I don't know if this is a bug or the expected behavior.
If this is expected, is there any other way to make my use case work?

One idea that came to my mind is this: mockintosh keeps a count of the requests as we can see on the Statistics tab. If there's a way to reference this count in the templated responses (something like {{requestCount}}), then I could also use this count to model my correlation-id requirement.

I was thinking about adding some conditionals to check if the counter exists (first execution) and then always adding 1 to it's value but I haven't tried it yet, don't even know if it's possible.

Thanks!

@danielsitnik
Copy link
Author

danielsitnik commented Aug 24, 2022

Ok, small update to my first post.
I think this might really be a bug.

I changed the configuration so that the endpoint returns the correlation-id in a header, instead of the body:

services:
- name: Correlation Service
  endpoints:
  - path: /correlation
    response:
      status: 202
      headers:
        x-correlation-id: {{counter 'correlationIdCounter'}}
      triggerAsyncProducer: rabbit-producer
- name: Rabbit Service
  type: amqp
  address: localhost:5672
  actors:
  - name: rabbit-producer
    produce:
      queue: mockintosh
      key: mockintosh
      value: '{"message": "ok", "correlationId": "{{correlationIdCounter}}"}'

AND IT WORKS! 😄
First execution returns 1 in the http response and also in the produced message!

In summary, when counter is used in the headers, the value is incremented before the async producer is triggered. When it's used in the body, it seems to be the other way around.

Since this is not a deterministic behavior, I'd say this is a bug.

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

No branches or pull requests

1 participant