muon-protocol / muon-node-js

9 stars 18 forks source link

Improve connection-prunner #365

Closed bakhshandeh closed 8 months ago

bakhshandeh commented 10 months ago

The priority of keeping the connections should be this:

1- being in the same subnet 2- deployer 3- connections that are not used recently

Current code:

const sorted = connections.sort((a, b) => {
                /** rank connection a */
                const nodeA = allNodes[a.remotePeer];
                const aRank = (nodeA.isDeployer ? 4 : 0) 
                    + (isInCommonSubnet[nodeA.peerId] ? 2 : 0)
                    + (a.timeline.upgraded > b.timeline.upgraded ? 1 : -1)

                /** rank connection b */
                const nodeB = allNodes[b.remotePeer];
                const bRank = (nodeB.isDeployer ? 4 : 0) 
                    + (isInCommonSubnet[nodeB.peerId] ? 2 : 0)
                    + (b.timeline.upgraded > a.timeline.upgraded ? 1 : -1)

                return aRank - bRank;
            })

If timeline.upgraded is the time of connection establishment, then it is not the same as the last time that the connection is used.

bakhshandeh commented 9 months ago

Let's improve it in this way:

1- Save usage time for each connection in the memory and prune the old ones. It seems that timeline.upgraded is the time of connection establishment not the time of last usage

2- Add pruneBatchSize to the connection-pruner to prune the connection in batch. For example, when max is 1000 and pruneBatchSize = 200, if number of open connections is more than 1000, connection-pruner will close 200. It is more effiecient and the pruner does not need to close the connections on each run.