You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-gateway.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -180,7 +180,7 @@ The endpoint expects the following request format:
180
180
### Wait for response
181
181
182
182
Sometimes it makes sense to provide a simple request-response API to something that runs asynchronously on the backend. For example, let's say there's a ticket reservation process that takes 10 seconds in total and involves three different services that communicate via message passing. For an external client, it may be simpler to wait 10 seconds for the response instead of polling for a response every other second.
183
-
A behavior like this can be configured using an endpoints'`response_from` property. When set to `kafka`, the response to the request is not taken from the `target` (e.g., for `target` = `http` this means the backend's HTTP response is ignored), but instead it's read from a Kafka topic. In order to enable RIG to correlate the response from the topic with the original request, RIG adds a correlation ID to the request (using a query parameter in case of `target` = `http`, or backed into the produced CloudEvent otherwise). **Backend services that work with the request need to include that correlation ID in their response; otherwise, RIG won't be able to forward it to the client (and times out).**
183
+
A behavior like this can be configured using an endpoints' `response_from` property. When set to `kafka`, the response to the request is not necessarily taken from the `target`, e.g., for `target` = `http` this means the backend's HTTP response might be ignored - it's the responsibility of the backend service where to read the response from: If the backend returns with the HTTP code `202 Accepted`, the response will be read from a Kafka topic. If the backend returns a different HTTP code (can be `200` or `400` or whatever makes sense), only then the response will be read synchronously from the http target directly (this allows the backend to return cached responses). In order to enable RIG to correlate the response from the kafka topic with the original request, RIG adds a correlation ID to the request (using a query parameter in case of `target` = `http`, or baked into the produced CloudEvent otherwise). Backend services that work with the request need to include that correlation ID in their response; otherwise, RIG won't be able to forward it to the client (and times out).
184
184
185
185
Configuration of such API endpoint might look like this:
Copy file name to clipboardExpand all lines: test/rig_tests/proxy/response_from/async_http_test.exs
+146-4Lines changed: 146 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -27,8 +27,10 @@ defmodule RigTests.Proxy.ResponseFrom.AsyncHttpTest do
27
27
end)
28
28
end
29
29
30
-
test_with_server"Given response_from is set to http_async and response is in binary mode, the http response is taken from the internal HTTP endpoint."do
@@ -153,4 +155,144 @@ defmodule RigTests.Proxy.ResponseFrom.AsyncHttpTest do
153
155
# ...the fake backend service has been called:
154
156
assertFakeServer.hits()==1
155
157
end
158
+
159
+
test_with_server"Given response_from=http_async, when the backend responds with 200, RIG forwards this 200-response (and does not expect an asynchronous response for this request)."do
160
+
test_name="proxy-http-response-synchronous"
161
+
162
+
api_id="mock-#{test_name}-api"
163
+
endpoint_id="mock-#{test_name}-endpoint"
164
+
endpoint_path="/#{endpoint_id}"
165
+
sync_response=%{"message"=>"this is the sync response that reaches the client"}
166
+
async_response=%{"this response"=>"the client never sees this response"}
# ...the connection is closed and the status is OK:
224
+
assertres_status==200
225
+
# ...the client receives the synchronous http response:
226
+
assertJason.decode!(res_body)==sync_response
227
+
end
228
+
229
+
test_with_server"Given response_from=http_async, when the backend responds with 400, RIG forwards this 400-response (and does not expect an asynchronous response for this request)."do
230
+
test_name="proxy-http-no-response"
231
+
232
+
api_id="mock-#{test_name}-api"
233
+
endpoint_id="mock-#{test_name}-endpoint"
234
+
endpoint_path="/#{endpoint_id}"
235
+
sync_response="Bad request from the test endpoint"
236
+
async_response=%{"this response"=>"the client never sees this response"}
0 commit comments