40
40
ID_PUSH = TProtocol ("/ipfs/id/push/1.0.0" )
41
41
PROTOCOL_VERSION = "ipfs/0.1.0"
42
42
AGENT_VERSION = get_agent_version ()
43
+ LIMIT = trio .CapacityLimiter (10 )
43
44
44
45
45
46
def identify_push_handler_for (host : IHost ) -> StreamHandlerFn :
@@ -146,25 +147,26 @@ async def push_identify_to_peer(
146
147
True if the push was successful, False otherwise.
147
148
148
149
"""
149
- try :
150
- # Create a new stream to the peer using the identify/push protocol
151
- stream = await host .new_stream (peer_id , [ID_PUSH ])
150
+ async with LIMIT :
151
+ try :
152
+ # Create a new stream to the peer using the identify/push protocol
153
+ stream = await host .new_stream (peer_id , [ID_PUSH ])
152
154
153
- # Create the identify message
154
- identify_msg = _mk_identify_protobuf (host , observed_multiaddr )
155
- response = identify_msg .SerializeToString ()
155
+ # Create the identify message
156
+ identify_msg = _mk_identify_protobuf (host , observed_multiaddr )
157
+ response = identify_msg .SerializeToString ()
156
158
157
- # Send the identify message
158
- await stream .write (response )
159
+ # Send the identify message
160
+ await stream .write (response )
159
161
160
- # Close the stream
161
- await stream .close ()
162
+ # Close the stream
163
+ await stream .close ()
162
164
163
- logger .debug ("Successfully pushed identify to peer %s" , peer_id )
164
- return True
165
- except Exception as e :
166
- logger .error ("Error pushing identify to peer %s: %s" , peer_id , e )
167
- return False
165
+ logger .debug ("Successfully pushed identify to peer %s" , peer_id )
166
+ return True
167
+ except Exception as e :
168
+ logger .error ("Error pushing identify to peer %s: %s" , peer_id , e )
169
+ return False
168
170
169
171
170
172
async def push_identify_to_peers (
@@ -179,13 +181,10 @@ async def push_identify_to_peers(
179
181
"""
180
182
if peer_ids is None :
181
183
# Get all connected peers
182
- peer_ids = set (host .get_peerstore (). peer_ids ())
184
+ peer_ids = set (host .get_connected_peers ())
183
185
184
186
# Push to each peer in parallel using a trio.Nursery
185
- # TODO: Consider using a bounded nursery to limit concurrency
186
- # and avoid overwhelming the network. This can be done by using
187
- # trio.open_nursery(max_concurrent=10) or similar.
188
- # For now, we will use an unbounded nursery for simplicity.
187
+ # limiting concurrent connections to 10
189
188
async with trio .open_nursery () as nursery :
190
189
for peer_id in peer_ids :
191
190
nursery .start_soon (push_identify_to_peer , host , peer_id , observed_multiaddr )
0 commit comments