chore: rewrite bbolt cachefile implements

never use returned byte slices outside the transaction, ref:
https://pkg.go.dev/go.etcd.io/bbolt#hdr-Caveats
This commit is contained in:
wwqgtxx
2024-09-23 09:35:48 +08:00
parent 150c6ccd25
commit 966eeae41b
12 changed files with 212 additions and 168 deletions

View File

@@ -5,6 +5,7 @@ import (
"os"
"time"
"github.com/metacubex/mihomo/common/utils"
types "github.com/metacubex/mihomo/constant/provider"
"github.com/metacubex/mihomo/log"
@@ -21,7 +22,7 @@ type Fetcher[V any] struct {
name string
vehicle types.Vehicle
updatedAt time.Time
hash types.HashType
hash utils.HashType
parser Parser[V]
interval time.Duration
onUpdate func(V)
@@ -55,7 +56,7 @@ func (f *Fetcher[V]) Initial() (V, error) {
// local file exists, use it first
buf, err = os.ReadFile(f.vehicle.Path())
modTime := stat.ModTime()
contents, _, err = f.loadBuf(buf, types.MakeHash(buf), false)
contents, _, err = f.loadBuf(buf, utils.MakeHash(buf), false)
f.updatedAt = modTime // reset updatedAt to file's modTime
if err == nil {
@@ -89,10 +90,10 @@ func (f *Fetcher[V]) Update() (V, bool, error) {
}
func (f *Fetcher[V]) SideUpdate(buf []byte) (V, bool, error) {
return f.loadBuf(buf, types.MakeHash(buf), true)
return f.loadBuf(buf, utils.MakeHash(buf), true)
}
func (f *Fetcher[V]) loadBuf(buf []byte, hash types.HashType, updateFile bool) (V, bool, error) {
func (f *Fetcher[V]) loadBuf(buf []byte, hash utils.HashType, updateFile bool) (V, bool, error) {
now := time.Now()
if f.hash.Equal(hash) {
if updateFile {

View File

@@ -9,6 +9,7 @@ import (
"path/filepath"
"time"
"github.com/metacubex/mihomo/common/utils"
mihomoHttp "github.com/metacubex/mihomo/component/http"
"github.com/metacubex/mihomo/component/profile/cachefile"
types "github.com/metacubex/mihomo/constant/provider"
@@ -61,12 +62,12 @@ func (f *FileVehicle) Url() string {
return "file://" + f.path
}
func (f *FileVehicle) Read(ctx context.Context, oldHash types.HashType) (buf []byte, hash types.HashType, err error) {
func (f *FileVehicle) Read(ctx context.Context, oldHash utils.HashType) (buf []byte, hash utils.HashType, err error) {
buf, err = os.ReadFile(f.path)
if err != nil {
return
}
hash = types.MakeHash(buf)
hash = utils.MakeHash(buf)
return
}
@@ -110,14 +111,14 @@ func (h *HTTPVehicle) Write(buf []byte) error {
return safeWrite(h.path, buf)
}
func (h *HTTPVehicle) Read(ctx context.Context, oldHash types.HashType) (buf []byte, hash types.HashType, err error) {
func (h *HTTPVehicle) Read(ctx context.Context, oldHash utils.HashType) (buf []byte, hash utils.HashType, err error) {
ctx, cancel := context.WithTimeout(ctx, h.timeout)
defer cancel()
header := h.header
setIfNoneMatch := false
if etag && oldHash.IsValid() {
hashBytes, etag := cachefile.Cache().GetETagWithHash(h.url)
if oldHash.EqualBytes(hashBytes) && etag != "" {
if oldHash.Equal(hashBytes) && etag != "" {
if header == nil {
header = http.Header{}
} else {
@@ -143,9 +144,9 @@ func (h *HTTPVehicle) Read(ctx context.Context, oldHash types.HashType) (buf []b
if err != nil {
return
}
hash = types.MakeHash(buf)
hash = utils.MakeHash(buf)
if etag {
cachefile.Cache().SetETagWithHash(h.url, hash.Bytes(), resp.Header.Get("ETag"))
cachefile.Cache().SetETagWithHash(h.url, hash, resp.Header.Get("ETag"))
}
return
}