@startuml Responding To A Request participant "GraphSync\nTop Level\nInterface" as TLI participant ResponseManager participant "Query Workers" as QW participant PeerTaskQueue participant PeerTracker participant PeerResponseManager participant PeerResponseSender participant LinkTracker participant ResponseBuilder participant IPLDBridge participant "Intercepted Loader" as ILoader participant Loader participant "Message Sending\nLayer" as Message == Initialization == TLI -> ResponseManager ** : Setup ResponseManager -> QW ** : Create activate QW TLI -> PeerTaskQueue ** : Setup TLI -> PeerResponseManager ** : Setup == Responding To Request == par loop until shutdown note over TLI : Request Queueing Loop TLI -> ResponseManager : Process requests alt new request ResponseManager -> PeerTaskQueue : Push Request PeerTaskQueue -> PeerTracker ** : Create for peer\n as neccesary PeerTaskQueue -> PeerTracker : Push Request ResponseManager -> ResponseManager : Create Request Context else cancel request ResponseManager -> ResponseManager : Cancel Request Context end end else par loop until shutdown note over QW: Request Processing Loop QW -> PeerTaskQueue : Pop Request PeerTaskQueue -> PeerTracker : Pop Request PeerTracker -> PeerTaskQueue : Next Request\nTo Process PeerTaskQueue -> QW : Next Request\nTo Process QW -> IPLDBridge : DecodeNode IPLDBridge -> QW : Selector Spec Node QW -> IPLDBridge : DecodeSelectorSpec IPLDBridge -> QW : Root Node, IPLD Selector QW -> PeerResponseManager : SenderForPeer PeerResponseManager -> PeerResponseSender ** : Create for peer\nas neccesary PeerResponseSender -> LinkTracker ** : Create PeerResponseSender -> QW : PeerResponseSender activate PeerResponseSender QW -> ILoader ** : Create w/ RequestID, PeerResponseSender, Loader QW -> IPLDBridge : Start Traversal Of Selector loop until traversal complete or request context cancelled note over PeerResponseSender: Selector Traversal Loop IPLDBridge -> ILoader : Request to load blocks\nto perform traversal ILoader -> Loader : Load blocks\nfrom local storage Loader -> ILoader : Blocks From\nlocal storage or error ILoader -> IPLDBridge : Blocks to continue\n traversal or error ILoader -> PeerResponseSender : Block or error to Send Back activate PeerResponseSender PeerResponseSender -> LinkTracker : Notify block or\n error, ask whether\n block is duplicate LinkTracker -> PeerResponseSender : Whether to\n send block PeerResponseSender -> ResponseBuilder ** : Create New As Neccesary PeerResponseSender -> ResponseBuilder : Aggregate Response Metadata & Block PeerResponseSender -> PeerResponseSender : Signal Work To Do deactivate PeerResponseSender end IPLDBridge -> QW : Traversal Complete QW -> PeerResponseSender : Request Finished activate PeerResponseSender PeerResponseSender -> LinkTracker : Query If Errors\n Were Present LinkTracker -> PeerResponseSender : True/False\n if errors present PeerResponseSender -> ResponseBuilder : Aggregate request finishing PeerResponseSender -> PeerResponseSender : Signal Work To Do deactivate PeerResponseSender end else loop until shutdown / disconnect note over PeerResponseSender: Message Sending\nLoop PeerResponseSender -> PeerResponseSender : Wait For Work Signal ... PeerResponseSender -> ResponseBuilder : build response ResponseBuilder -> PeerResponseSender : Response message data to send PeerResponseSender -> Message : Send response message data activate Message Message -> PeerResponseSender : Channel For When Message Processed ... Message -> PeerResponseSender : Notification on channel deactivate Message end deactivate PeerResponseSender end deactivate QW end @enduml