From dfd93346a9335b3df389dc6b0e66b27f68d62b98 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Fri, 4 Oct 2019 16:40:21 +0100 Subject: [PATCH] Fix race in cache --- cache.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cache.go b/cache.go index 46460f7..a17960e 100644 --- a/cache.go +++ b/cache.go @@ -119,12 +119,10 @@ func (c *cache) get(service string) ([]*registry.Service, error) { // make a copy cp := registry.Copy(services) - // unlock the read lock - c.RUnlock() - // got services && within ttl so return cache if c.isValid(cp, ttl) { - // return servics + c.RUnlock() + // return services return cp, nil } @@ -163,8 +161,11 @@ func (c *cache) get(service string) ([]*registry.Service, error) { go c.run(service) } + // unlock the read lock + c.RUnlock() + // get and return services - return get(service, services) + return get(service, cp) } func (c *cache) set(service string, services []*registry.Service) {