cache: Fix panics
Cette révision appartient à :
Parent
bcaac0961f
révision
1cb4613b7e
3 fichiers modifiés avec 41 ajouts et 8 suppressions
4
cache/cache.go
externe
4
cache/cache.go
externe
|
@ -7,8 +7,6 @@ import (
|
||||||
"github.com/ewhal/nyaa/common"
|
"github.com/ewhal/nyaa/common"
|
||||||
"github.com/ewhal/nyaa/config"
|
"github.com/ewhal/nyaa/config"
|
||||||
"github.com/ewhal/nyaa/model"
|
"github.com/ewhal/nyaa/model"
|
||||||
|
|
||||||
"errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Cache defines interface for caching search results
|
// Cache defines interface for caching search results
|
||||||
|
@ -17,8 +15,6 @@ type Cache interface {
|
||||||
ClearAll()
|
ClearAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrInvalidCacheDialect = errors.New("invalid cache dialect")
|
|
||||||
|
|
||||||
// Impl cache implementation instance
|
// Impl cache implementation instance
|
||||||
var Impl Cache
|
var Impl Cache
|
||||||
|
|
||||||
|
|
8
cache/native/native.go
externe
8
cache/native/native.go
externe
|
@ -111,7 +111,9 @@ func (n *NativeCache) updateUsedSize(delta int) {
|
||||||
}
|
}
|
||||||
s := n.ll.Remove(e).(*store)
|
s := n.ll.Remove(e).(*store)
|
||||||
delete(n.cache, s.key)
|
delete(n.cache, s.key)
|
||||||
|
s.Lock()
|
||||||
n.totalUsed -= s.size
|
n.totalUsed -= s.size
|
||||||
|
s.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,8 +138,6 @@ func (s *store) update(data []model.Torrent, count int) {
|
||||||
s.size = newSize
|
s.size = newSize
|
||||||
s.lastFetched = time.Now()
|
s.lastFetched = time.Now()
|
||||||
|
|
||||||
// Technically it is possible to update the size even when the store is
|
// In a separate goroutine, to ensure there is never any lock intersection
|
||||||
// already evicted, but that should never happen, unless you have a very
|
go s.n.updateUsedSize(delta)
|
||||||
// small cache, very large stored datasets and a lot of traffic.
|
|
||||||
s.n.updateUsedSize(delta)
|
|
||||||
}
|
}
|
||||||
|
|
37
cache/native/native_test.go
externe
Fichier normal
37
cache/native/native_test.go
externe
Fichier normal
|
@ -0,0 +1,37 @@
|
||||||
|
package native
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/ewhal/nyaa/common"
|
||||||
|
"github.com/ewhal/nyaa/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Basic test for deadlocks and race conditions
|
||||||
|
func TestConcurrency(t *testing.T) {
|
||||||
|
c := New(0.000001)
|
||||||
|
|
||||||
|
fn := func() ([]model.Torrent, int, error) {
|
||||||
|
return []model.Torrent{{}, {}, {}}, 10, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(300)
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
go func() {
|
||||||
|
for j := 0; j < 100; j++ {
|
||||||
|
go func(j int) {
|
||||||
|
defer wg.Done()
|
||||||
|
k := common.SearchParam{
|
||||||
|
Page: j,
|
||||||
|
}
|
||||||
|
if _, _, err := c.Get(k, fn); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}(j)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
}
|
Référencer dans un nouveau ticket