diff --git a/internal/caches/errors.go b/internal/caches/errors.go index 0fb8e75..4656117 100644 --- a/internal/caches/errors.go +++ b/internal/caches/errors.go @@ -34,16 +34,14 @@ func CanIgnoreErr(err error) bool { if err == nil { return true } - if err == ErrFileIsWriting || - err == ErrEntityTooLarge || - err == ErrWritingUnavailable || - err == ErrWritingQueueFull || - err == ErrServerIsBusy { + if errors.Is(err, ErrFileIsWriting) || + errors.Is(err, ErrEntityTooLarge) || + errors.Is(err, ErrWritingUnavailable) || + errors.Is(err, ErrWritingQueueFull) || + errors.Is(err, ErrServerIsBusy) { return true } - _, ok := err.(*CapacityError) - if ok { - return true - } - return false + + var capacityErr *CapacityError + return errors.As(err, &capacityErr) } diff --git a/internal/caches/errros_test.go b/internal/caches/errros_test.go index 92bcbc4..29474ee 100644 --- a/internal/caches/errros_test.go +++ b/internal/caches/errros_test.go @@ -1,16 +1,24 @@ // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. -package caches +package caches_test import ( + "errors" + "fmt" + "github.com/TeaOSLab/EdgeNode/internal/caches" "github.com/iwind/TeaGo/assert" "testing" ) func TestCanIgnoreErr(t *testing.T) { - a := assert.NewAssertion(t) + var a = assert.NewAssertion(t) - a.IsTrue(CanIgnoreErr(ErrFileIsWriting)) - a.IsTrue(CanIgnoreErr(NewCapacityError("over capcity"))) - a.IsFalse(CanIgnoreErr(ErrNotFound)) + a.IsTrue(caches.CanIgnoreErr(caches.ErrFileIsWriting)) + a.IsTrue(caches.CanIgnoreErr(fmt.Errorf("error: %w", caches.ErrFileIsWriting))) + a.IsTrue(errors.Is(fmt.Errorf("error: %w", caches.ErrFileIsWriting), caches.ErrFileIsWriting)) + a.IsTrue(errors.Is(caches.ErrFileIsWriting, caches.ErrFileIsWriting)) + a.IsTrue(caches.CanIgnoreErr(caches.NewCapacityError("over capacity"))) + a.IsTrue(caches.CanIgnoreErr(fmt.Errorf("error: %w", caches.NewCapacityError("over capacity")))) + a.IsFalse(caches.CanIgnoreErr(caches.ErrNotFound)) + a.IsFalse(caches.CanIgnoreErr(errors.New("test error"))) } diff --git a/internal/caches/storage_file.go b/internal/caches/storage_file.go index 74fa32c..52358b4 100644 --- a/internal/caches/storage_file.go +++ b/internal/caches/storage_file.go @@ -464,7 +464,7 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, hea _, ok := sharedWritingFileKeyMap[key] if ok { sharedWritingFileKeyLocker.Unlock() - return nil, ErrFileIsWriting + return nil, fmt.Errorf("%w(001)", ErrFileIsWriting) } if !isFlushing && !fsutils.WriteReady() { @@ -505,7 +505,7 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, hea // 检查两次写入缓存的时间是否过于相近,分片内容不受此限制 if err == nil && !isPartial && time.Since(stat.ModTime()) <= 1*time.Second { // 防止并发连续写入 - return nil, ErrFileIsWriting + return nil, fmt.Errorf("%w(002)", ErrFileIsWriting) } // 构造文件名 @@ -606,7 +606,7 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, hea err = syscall.Flock(int(writer.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) if err != nil { removeOnFailure = false - return nil, ErrFileIsWriting + return nil, fmt.Errorf("%w (003)", ErrFileIsWriting) } var metaBodySize int64 = -1 diff --git a/internal/caches/storage_file_test.go b/internal/caches/storage_file_test.go index 69288d7..bae507d 100644 --- a/internal/caches/storage_file_test.go +++ b/internal/caches/storage_file_test.go @@ -231,7 +231,7 @@ func TestFileStorage_Concurrent_Open_DifferentFile(t *testing.T) { writer, err := storage.OpenWriter("abc"+strconv.Itoa(i), time.Now().Unix()+3600, 200, -1, -1, -1, false) if err != nil { - if err != ErrFileIsWriting { + if errors.Is(err, ErrFileIsWriting) { t.Error(err) return } @@ -289,7 +289,7 @@ func TestFileStorage_Concurrent_Open_SameFile(t *testing.T) { writer, err := storage.OpenWriter("abc"+strconv.Itoa(0), time.Now().Unix()+3600, 200, -1, -1, -1, false) if err != nil { - if err != ErrFileIsWriting { + if errors.Is(err, ErrFileIsWriting) { t.Error(err) return } diff --git a/internal/caches/storage_memory.go b/internal/caches/storage_memory.go index 1b15765..137f5c3 100644 --- a/internal/caches/storage_memory.go +++ b/internal/caches/storage_memory.go @@ -1,6 +1,7 @@ package caches import ( + "fmt" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeNode/internal/goman" "github.com/TeaOSLab/EdgeNode/internal/remotelogs" @@ -161,7 +162,7 @@ func (this *MemoryStorage) OpenWriter(key string, expiredAt int64, status int, h // TODO 内存缓存暂时不支持分块内容存储 if isPartial { - return nil, ErrFileIsWriting + return nil, fmt.Errorf("%w (004)", ErrFileIsWriting) } return this.openWriter(key, expiredAt, status, headerSize, bodySize, maxSize, true) } @@ -187,7 +188,7 @@ func (this *MemoryStorage) openWriter(key string, expiresAt int64, status int, h var isWriting = false _, ok := this.writingKeyMap[key] if ok { - return nil, ErrFileIsWriting + return nil, fmt.Errorf("%w (005)", ErrFileIsWriting) } this.writingKeyMap[key] = zero.New() defer func() { @@ -208,7 +209,7 @@ func (this *MemoryStorage) openWriter(key string, expiresAt int64, status int, h _ = this.list.Remove(hashString) item = nil } else { - return nil, ErrFileIsWriting + return nil, fmt.Errorf("%w (006)", ErrFileIsWriting) } }