-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize decryption key handling #458
base: gnosis
Are you sure you want to change the base?
Conversation
…kip validation if key exists
22525c6
to
6daefbb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks correct to me.
I don't think adding in memory state to the handler is the right way to go though. As far as we know, the bottleneck is computation, not db queries, so this feels like premature optimization. Even if it turns out to be db queries, I think we should look at this more holistically and not start randomly getting rid of this one query among the 10s of others we do during message processing. The downside of adding the cache is increased complexity which makes it harder to reason about the system: Before, we had all state in the DB, now we have to consider the in-memory state as well (which might or might not be in agreement).
One other issue is that the cache is not wide enough. Keys that are created locally and send to the network will pass through the validator, but not through the handler, so they won't make it into the cache, while they would make it into the db.
So all in all I would prefer using only the DB for caching for as long as it's not the bottleneck. But that's just my opinion, it's definitely a significant performance improvement, so I'm ok with this as well.
"github.com/shutter-network/rolling-shutter/rolling-shutter/p2p" | ||
"github.com/shutter-network/rolling-shutter/rolling-shutter/p2pmsg" | ||
"github.com/shutter-network/rolling-shutter/rolling-shutter/shdb" | ||
) | ||
|
||
func NewDecryptionKeyHandler(config Config, dbpool *pgxpool.Pool) p2p.MessageHandler { | ||
return &DecryptionKeyHandler{config: config, dbpool: dbpool} | ||
// Not catching the error as it only can happen if non-positive size was applied | ||
cache, _ := lru.New[shcrypto.EpochSecretKey, []byte](1024) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For unreachable errors I personally prefer if err != nil { panic("msg") }
. It's self documenting and if the error unexpectedly occurs, most likely we would panic later anyways (because cache would be nil
), but at a random a more random line.
Thanks for the review and your opinion. I think you made a valid point regarding a more holistic approach to caching instead of randomly adding caches here and there and that resonates with me very much. I'm actually happy to think about a proper solution once we have the time for it.
I would love to get some benchmarking actually. Do you think the benchmark test would give a valuable output? |
After some discussion with @ulope, we decided to keep the keys in an in-memory cache instead of checking against the db.
The reasons are: