Skip to content

Commit

Permalink
Merge pull request #398 from bakape/cache-panics
Browse files Browse the repository at this point in the history
cache: Fix panics
  • Loading branch information
Austin authored May 12, 2017
2 parents 83be78a + 1cb4613 commit 09811cc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
4 changes: 0 additions & 4 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/ewhal/nyaa/common"
"github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/model"

"errors"
)

// Cache defines interface for caching search results
Expand All @@ -17,8 +15,6 @@ type Cache interface {
ClearAll()
}

var ErrInvalidCacheDialect = errors.New("invalid cache dialect")

// Impl cache implementation instance
var Impl Cache

Expand Down
8 changes: 4 additions & 4 deletions cache/native/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ func (n *NativeCache) updateUsedSize(delta int) {
}
s := n.ll.Remove(e).(*store)
delete(n.cache, s.key)
s.Lock()
n.totalUsed -= s.size
s.Unlock()
}
}

Expand All @@ -136,8 +138,6 @@ func (s *store) update(data []model.Torrent, count int) {
s.size = newSize
s.lastFetched = time.Now()

// Technically it is possible to update the size even when the store is
// already evicted, but that should never happen, unless you have a very
// small cache, very large stored datasets and a lot of traffic.
s.n.updateUsedSize(delta)
// In a separate goroutine, to ensure there is never any lock intersection
go s.n.updateUsedSize(delta)
}
37 changes: 37 additions & 0 deletions cache/native/native_test.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 09811cc

Please sign in to comment.