Skip to content

Commit 62567d3

Browse files
committed
deps: Upgrade github.com/bep/lazycache v0.6.0 => v0.7.0
1 parent e10915f commit 62567d3

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

cache/dynacache/dynacache.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,25 @@ func (p *Partition[K, V]) doGetOrCreateWitTimeout(key K, duration time.Duration,
430430
errch := make(chan error, 1)
431431

432432
go func() {
433-
v, _, err := p.c.GetOrCreate(key, create)
434-
if err != nil {
435-
errch <- err
436-
return
437-
}
438-
resultch <- v
433+
var (
434+
v V
435+
err error
436+
)
437+
defer func() {
438+
if r := recover(); r != nil {
439+
if rerr, ok := r.(error); ok {
440+
err = rerr
441+
} else {
442+
err = fmt.Errorf("panic: %v", r)
443+
}
444+
}
445+
if err != nil {
446+
errch <- err
447+
} else {
448+
resultch <- v
449+
}
450+
}()
451+
v, _, err = p.c.GetOrCreate(key, create)
439452
}()
440453

441454
select {

cache/dynacache/dynacache_test.go

+37-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package dynacache
1515

1616
import (
17+
"errors"
1718
"fmt"
1819
"path/filepath"
1920
"testing"
@@ -174,18 +175,47 @@ func TestPanicInCreate(t *testing.T) {
174175

175176
p1 := GetOrCreatePartition[string, testItem](cache, "/aaaa/bbbb", OptionsPartition{Weight: 30, ClearWhen: ClearOnRebuild})
176177

178+
willPanic := func(i int) func() {
179+
return func() {
180+
p1.GetOrCreate(fmt.Sprintf("panic-%d", i), func(key string) (testItem, error) {
181+
panic(errors.New(key))
182+
})
183+
}
184+
}
185+
186+
// GetOrCreateWitTimeout needs to recover from panics in the create func.
187+
willErr := func(i int) error {
188+
_, err := p1.GetOrCreateWitTimeout(fmt.Sprintf("error-%d", i), 10*time.Second, func(key string) (testItem, error) {
189+
return testItem{}, errors.New(key)
190+
})
191+
return err
192+
}
193+
177194
for i := 0; i < 3; i++ {
178195
for j := 0; j < 3; j++ {
179-
_, err := p1.GetOrCreate(fmt.Sprintf("panic1-%d", i), func(string) (testItem, error) {
180-
panic("failed")
181-
})
196+
c.Assert(willPanic(i), qt.PanicMatches, fmt.Sprintf("panic-%d", i))
197+
c.Assert(willErr(i), qt.ErrorMatches, fmt.Sprintf("error-%d", i))
198+
}
199+
}
182200

183-
c.Assert(err, qt.Not(qt.IsNil))
201+
// Test the same keys again without the panic.
202+
for i := 0; i < 3; i++ {
203+
for j := 0; j < 3; j++ {
204+
v, err := p1.GetOrCreate(fmt.Sprintf("panic-%d", i), func(key string) (testItem, error) {
205+
return testItem{
206+
name: key,
207+
}, nil
208+
})
209+
c.Assert(err, qt.IsNil)
210+
c.Assert(v.name, qt.Equals, fmt.Sprintf("panic-%d", i))
184211

185-
_, err = p1.GetOrCreateWitTimeout(fmt.Sprintf("panic2-%d", i), 10*time.Second, func(string) (testItem, error) {
186-
panic("failed")
212+
v, err = p1.GetOrCreateWitTimeout(fmt.Sprintf("error-%d", i), 10*time.Second, func(key string) (testItem, error) {
213+
return testItem{
214+
name: key,
215+
}, nil
187216
})
188-
c.Assert(err, qt.Not(qt.IsNil))
217+
c.Assert(err, qt.IsNil)
218+
c.Assert(v.name, qt.Equals, fmt.Sprintf("error-%d", i))
189219
}
190220
}
191221
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/bep/gowebp v0.3.0
1717
github.com/bep/helpers v0.5.0
1818
github.com/bep/imagemeta v0.8.1
19-
github.com/bep/lazycache v0.6.0
19+
github.com/bep/lazycache v0.7.0
2020
github.com/bep/logg v0.4.0
2121
github.com/bep/mclib v1.20400.20402
2222
github.com/bep/overlayfs v0.9.2

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ github.com/bep/lazycache v0.4.0 h1:X8yVyWNVupPd4e1jV7efi3zb7ZV/qcjKQgIQ5aPbkYI=
145145
github.com/bep/lazycache v0.4.0/go.mod h1:NmRm7Dexh3pmR1EignYR8PjO2cWybFQ68+QgY3VMCSc=
146146
github.com/bep/lazycache v0.6.0 h1:0vCgFo7TBtMQpSx64jnH1sagmw0ZougIFRpsqPHTa5U=
147147
github.com/bep/lazycache v0.6.0/go.mod h1:NmRm7Dexh3pmR1EignYR8PjO2cWybFQ68+QgY3VMCSc=
148+
github.com/bep/lazycache v0.7.0 h1:VM257SkkjcR9z55eslXTkUIX8QMNKoqQRNKV/4xIkCY=
149+
github.com/bep/lazycache v0.7.0/go.mod h1:NmRm7Dexh3pmR1EignYR8PjO2cWybFQ68+QgY3VMCSc=
148150
github.com/bep/logg v0.4.0 h1:luAo5mO4ZkhA5M1iDVDqDqnBBnlHjmtZF6VAyTp+nCQ=
149151
github.com/bep/logg v0.4.0/go.mod h1:Ccp9yP3wbR1mm++Kpxet91hAZBEQgmWgFgnXX3GkIV0=
150152
github.com/bep/mclib v1.20400.20402 h1:olpCE2WSPpOAbFE1R4hnftSEmQ34+xzy2HRzd0m69rA=

0 commit comments

Comments
 (0)