Skip to content

Commit

Permalink
better fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Sep 22, 2024
1 parent 6cb0830 commit 17e58bd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 31 deletions.
2 changes: 1 addition & 1 deletion adapter/provider/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func ParseProxyProvider(name string, mapping map[string]any, healthCheckLazyDefa
return nil, fmt.Errorf("%w: %s", errSubPath, path)
}
}
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, schema.Header)
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, schema.Header, resource.DefaultHttpTimeout)
default:
return nil, fmt.Errorf("%w: %s", ErrVehicleType, schema.Type)
}
Expand Down
20 changes: 1 addition & 19 deletions component/resource/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package resource
import (
"context"
"os"
"path/filepath"
"time"

types "github.com/metacubex/mihomo/constant/provider"
Expand All @@ -13,11 +12,6 @@ import (
"github.com/samber/lo"
)

var (
fileMode os.FileMode = 0o666
dirMode os.FileMode = 0o755
)

type Parser[V any] func([]byte) (V, error)

type Fetcher[V any] struct {
Expand Down Expand Up @@ -118,7 +112,7 @@ func (f *Fetcher[V]) loadBuf(buf []byte, hash types.HashType, updateFile bool) (
}

if updateFile {
if err = safeWrite(f.vehicle.Path(), buf); err != nil {
if err = f.vehicle.Write(buf); err != nil {
return lo.Empty[V](), false, err
}
}
Expand Down Expand Up @@ -205,18 +199,6 @@ func (f *Fetcher[V]) updateWithLog() {
return
}

func safeWrite(path string, buf []byte) error {
dir := filepath.Dir(path)

if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, dirMode); err != nil {
return err
}
}

return os.WriteFile(path, buf, fileMode)
}

func NewFetcher[V any](name string, interval time.Duration, vehicle types.Vehicle, parser Parser[V], onUpdate func(V)) *Fetcher[V] {
ctx, cancel := context.WithCancel(context.Background())
return &Fetcher[V]{
Expand Down
50 changes: 40 additions & 10 deletions component/resource/vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,33 @@ import (
"io"
"net/http"
"os"
"path/filepath"
"time"

mihomoHttp "github.com/metacubex/mihomo/component/http"
"github.com/metacubex/mihomo/component/profile/cachefile"
types "github.com/metacubex/mihomo/constant/provider"
)

const (
DefaultHttpTimeout = time.Second * 20

fileMode os.FileMode = 0o666
dirMode os.FileMode = 0o755
)

func safeWrite(path string, buf []byte) error {
dir := filepath.Dir(path)

if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, dirMode); err != nil {
return err
}
}

return os.WriteFile(path, buf, fileMode)
}

type FileVehicle struct {
path string
}
Expand Down Expand Up @@ -42,15 +62,20 @@ func (f *FileVehicle) Proxy() string {
return ""
}

func (f *FileVehicle) Write(buf []byte) error {
return safeWrite(f.path, buf)
}

func NewFileVehicle(path string) *FileVehicle {
return &FileVehicle{path: path}
}

type HTTPVehicle struct {
url string
path string
proxy string
header http.Header
url string
path string
proxy string
header http.Header
timeout time.Duration
}

func (h *HTTPVehicle) Url() string {
Expand All @@ -69,8 +94,12 @@ func (h *HTTPVehicle) Proxy() string {
return h.proxy
}

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) {
ctx, cancel := context.WithTimeout(ctx, time.Second*20)
ctx, cancel := context.WithTimeout(ctx, h.timeout)
defer cancel()
header := h.header
setIfNoneMatch := false
Expand Down Expand Up @@ -107,11 +136,12 @@ func (h *HTTPVehicle) Read(ctx context.Context, oldHash types.HashType) (buf []b
return
}

func NewHTTPVehicle(url string, path string, proxy string, header http.Header) *HTTPVehicle {
func NewHTTPVehicle(url string, path string, proxy string, header http.Header, timeout time.Duration) *HTTPVehicle {
return &HTTPVehicle{
url: url,
path: path,
proxy: proxy,
header: header,
url: url,
path: path,
proxy: proxy,
header: header,
timeout: timeout,
}
}
1 change: 1 addition & 0 deletions constant/provider/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (v VehicleType) String() string {

type Vehicle interface {
Read(ctx context.Context, oldHash HashType) (buf []byte, hash HashType, err error)
Write(buf []byte) error
Path() string
Url() string
Proxy() string
Expand Down
2 changes: 1 addition & 1 deletion rules/provider/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(t
return nil, fmt.Errorf("%w: %s", errSubPath, path)
}
}
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, nil)
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, nil, resource.DefaultHttpTimeout)
default:
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
}
Expand Down

0 comments on commit 17e58bd

Please sign in to comment.