[FEATURE] Improve Peer2Peer (P2P) package asynchronicity
Describe the feature you'd like
Implement controlled asynchronicity for the P2P package using counting semaphores to manage the maximum number of outgoing/incoming TCP requests. This would include:
- A bounded limit for parallel writes/reads
- A buffered queue for flow control
- Proper logging for dropped messages
- Non-blocking operations for the controller goroutine
Motivation
Currently, P2P operations block the goroutines where they are invoked (especially the controller goroutine), which makes the node unusable during network issues (slow nodes, network partitions, etc.). This can lead to:
- Complete node unresponsiveness
- Potential indefinite hangs
- Poor user experience during network instability
The proposed solution would:
- Prevent node unresponsiveness during P2P issues
- Provide better control over resource usage
- Enable graceful handling of network problems
- Improve overall system reliability
Additional context
Current problems:
- Blocking operations in controller goroutine
- No bounds on parallel requests
- No proper queue management for pending messages
- Difficult to diagnose network-related issues
Expected improvements:
- Bounded resource usage during sync operations
- Better failure isolation
- More predictable behavior under load
- Improved debugging capabilities
Possible implementation
- Implement counting semaphores for:
- Outgoing connections
- Incoming connections
- Create prioritized buffered queues for message processing (nice to have)
- Add timeout mechanisms for stuck operations
- Implement proper logging for:
- Dropped messages
- Queue saturation events
- Timeout events
- Add metrics to monitor:
- Queue sizes
- Wait times
- Success/failure rates
some of the implementations above are similar to the buffered channel queue where write tcp requests are ultimately sent to but this would allow them to be parallelized before reaching that final queue without blocking the controller
Required Tags
- Priority: Low
- Module: P2P
Some clarifying questions to zero-in on some of the problem areas:
- Blocking operations in controller goroutine
This happens when Stream.sendQueue is full and the controller blocks when trying to send messages? Are there other conditions?
- No proper queue management for pending messages
What are pending messages defined as? Are they for both incoming and outgoing?