@@ -92,8 +92,12 @@ func NewStreamHandler(ctx context.Context,
9292func (h * StreamHandler ) handleIncomingWireMessage (
9393 wireMsg rfqmsg.WireMessage ) error {
9494
95- // Parse the wire message as an RFQ message.
96- msg , err := rfqmsg .NewIncomingMsgFromWire (wireMsg )
95+ // Parse the wire message as an RFQ message. The session cache load
96+ // function is provided to associate incoming wire messages with their
97+ // corresponding outgoing requests during parsing.
98+ msg , err := rfqmsg .NewIncomingMsgFromWire (
99+ wireMsg , h .outgoingRequests .Load ,
100+ )
97101 if err != nil {
98102 if errors .Is (err , rfqmsg .ErrUnknownMessageType ) {
99103 // Silently disregard the message if we don't recognise
@@ -109,66 +113,13 @@ func (h *StreamHandler) handleIncomingWireMessage(
109113
110114 log .Debugf ("Stream handling incoming message: %s" , msg )
111115
112- // If the incoming message is an accept message, lookup the
113- // corresponding outgoing request message. Assign the outgoing request
114- // to a field on the accept message. This step allows us to easily
115- // access the request that the accept message is responding to. Some of
116- // the request fields are not present in the accept message.
117- //
118- // If the incoming message is a reject message, remove the corresponding
119- // outgoing request from the store.
120- switch typedMsg := msg .(type ) {
121- case * rfqmsg.Reject :
122- // Delete the corresponding outgoing request from the store.
123- h .outgoingRequests .Delete (typedMsg .ID )
124-
125- case * rfqmsg.BuyAccept :
126- // Load and delete the corresponding outgoing request from the
127- // store.
128- outgoingRequest , found := h .outgoingRequests .LoadAndDelete (
129- typedMsg .ID ,
130- )
131-
132- // Ensure that we have an outgoing request to match the incoming
133- // accept message.
134- if ! found {
135- return fmt .Errorf ("no outgoing request found for " +
136- "incoming accept message: %s" , typedMsg .ID )
137- }
138-
139- // Type cast the outgoing message to a BuyRequest (the request
140- // type that corresponds to a buy accept message).
141- buyReq , ok := outgoingRequest .(* rfqmsg.BuyRequest )
142- if ! ok {
143- return fmt .Errorf ("expected BuyRequest, got %T" ,
144- outgoingRequest )
145- }
146-
147- typedMsg .Request = * buyReq
148-
149- case * rfqmsg.SellAccept :
150- // Load and delete the corresponding outgoing request from the
151- // store.
152- outgoingRequest , found := h .outgoingRequests .LoadAndDelete (
153- typedMsg .ID ,
154- )
155-
156- // Ensure that we have an outgoing request to match the incoming
157- // accept message.
158- if ! found {
159- return fmt .Errorf ("no outgoing request found for " +
160- "incoming accept message: %s" , typedMsg .ID )
161- }
162-
163- // Type cast the outgoing message to a SellRequest (the request
164- // type that corresponds to a sell accept message).
165- req , ok := outgoingRequest .(* rfqmsg.SellRequest )
166- if ! ok {
167- return fmt .Errorf ("expected SellRequest, got %T" ,
168- outgoingRequest )
169- }
170-
171- typedMsg .Request = * req
116+ // If the incoming message is a response to an outgoing request, we
117+ // will remove the corresponding session from the store. We can safely
118+ // remove the session at this point because we have received the only
119+ // response we expect for this session.
120+ switch msg .(type ) {
121+ case * rfqmsg.BuyAccept , * rfqmsg.SellAccept , * rfqmsg.Reject :
122+ h .outgoingRequests .Delete (msg .MsgID ())
172123 }
173124
174125 // Send the incoming message to the RFQ manager.
0 commit comments