karlseguin / ccache

A golang LRU Cache for high concurrency
MIT License
1.29k stars 120 forks source link

Fetch() is not atomic #56

Closed Scratch-net closed 3 years ago

Scratch-net commented 3 years ago

If I call Fetch() simultaneously from multiple goroutines and the fetch func is rather slow, then it keeps being called until one of the calls returns. So, it looks like Fetch() is not thread-safe

karlseguin commented 3 years ago

Fetch is thread-safe in that, you aren't going to deadlock or corrupt anything within the ccache library, but the fetch func that's passed in will be called concurrently. I'm not 100% sure if this is something the library itself should handle, because it isn't clear if there's 1 correct way to handle this for all cases.

But, it should be pretty easy to handle in the app (using something like https://pkg.go.dev/golang.org/x/sync/singleflight)

Scratch-net commented 3 years ago

Thanks!