Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
From a brief inspection of the code it seems that the request throttling middleware uses a possibly inefficient underlying cache implementation. The use of STM may be inefficient under contention (although, probably only a problem when a large number of new peers is added in a short amount of time). There is also a tight loop during initialization of new cache items (should that be wrapped into STM retry?).
It is not clear if cache expiration is implemented correctly. It seems that items always expire after a fixed amount of time and the lease isn't renewed if they are queried in-between. With such a behavior 1. items are deleted only when used, 2. the cache would be reinitialized regularly even for keys that are used regularly. With that the current expiration of 5 seconds seems much too short.
It seems that the throttling middleware never purges the cache, which means that it would leak memory (luckily the leak is limited by the number of peers that existed in the network since the node was started).
Overall, throttling of the service API shouldn't be done within chainweb node, but should rather be delegated to a reverse proxy. Throttling of the P2P API should, if needed be implemented in a more efficient way, and should also include throttling of the overall number of open connections.
So, the following should be fixed:
retrieveCache
forCacheStateInitializing
by adding STMretry
.