walmartlabs / lacinia

GraphQL implementation in pure Clojure
http://lacinia.readthedocs.io/en/latest/
Other
1.81k stars 162 forks source link

Subscriptions do not maintain ordering #460

Open jkxyz opened 1 month ago

jkxyz commented 1 month ago

I've been using Lacinia subscriptions to handle AWS event stream responses, to stream those events back to the client.

It seems that the values passed to the subscription callback function are not sent to the WebSocket in the same order that the callback is called. I can log the order on the server, and then by observing the WebSocket messages I can see that they're being sent in a different order.

Here's a stripped down version of the subscription handler:

(defn subscription-handler [_context _args callback]
  (let [cancel-ch (async/chan)
        events-ch (async/chan)

        fut
        (future
          (invoke-model-stream events-ch ,,,))]
    (async/go-loop []
      (async/alt!
        cancel-ch nil
        events-ch ([event]
                   (callback event)
                   (when event
                     (recur)))))
    (fn []
      (async/close! cancel-ch)
      @fut)))

It doesn't seem to be documented anywhere that Lacinia subscriptions don't provide ordering guarantees. For now I've managed to work around this by providing an index in the responses and re-ordering on the client.

hlship commented 3 weeks ago

I'm surprised that order is not maintained, I'll have to look over the code and the guarantees made by core.async. How close are the events from AWS coming in?

jkxyz commented 3 weeks ago

How close are the events from AWS coming in?

I've been testing this with the InvokeModelWithResponseStream operation on the AWS Bedrock API. So the events are coming in milliseconds apart. The rate is not constant and most events do arrive on the client in the correct order.

I can provide more surrounding code to try and get a minimal reproduction, if it helps.

hlship commented 1 week ago

I'm putting this on a milestone just to keep track of it, I haven't looked at it at all (to verify, to reproduce it, etc.).