-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Bugfix: Make the Put on the SimpleDnsCache idempotent #1222
Conversation
Bugfix for the exception: itemwith the same key has already been added. Make the Put on the SimpleDnsCache idempotent when a same host is resolved.
Is this really thread safe? cc @akkadotnet/developers |
This is related but not part of the actual PR: internal void Put(Dns.Resolved r, long ttl)
{
var c = _cache.Value;
if (!_cache.CompareAndSet(c, c.Put(r, ttl)))
Put(r, ttl);
} the Put method use an atomic reference to compare and set.. but the values that are compared are the same instance, it's the same public Cache Put(Dns.Resolved answer, long ttl)
{
var until = _clock() + ttl;
_queue.Add(new ExpiryEntry(answer.Name, until));
_cache.Add(answer.Name, new CacheEntry(answer, until));
return this; //<----------------------- returns itself.
}
The code consuming this is based on immutability and the SimpleDnsCache is based on mutability.. IMO, SimpleDnsCache needs to be rewritten or the consuming code will be subject to race conditions... |
The corresponding method in Scala looks like this: def put(answer: Resolved, ttlMillis: Long): Cache = {
val until = clock() + ttlMillis
new Cache(
queue + new ExpiryEntry(answer.name, until),
cache + (answer.name -> CacheEntry(answer, until)),
clock)
} Which clearly returns a new instance of the cache. |
It seems that |
Yes the code does not look Thread Safe and the implementation looks a bit different than the Scala version. Also the Datetime.Now should use the UtcNow I think. Perhaps some more people can have a look at this class. |
Bugfix: Make the Put on the SimpleDnsCache idempotent
As this PR is better than the current code, I'm merging this. |
Bugfix for the exception: itemwith the same key has already been added.
Make the Put on the SimpleDnsCache idempotent when a same host is
resolved.