mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-22 09:53:02 +08:00
fix(store,home): handle repository index and packfile corruption and wrap Redis connections with cancelable dialer
- Expand repository corruption checks in gitstore to recognize missing index files and packhandle errors. - Wrap Redis client dialers in cluster latency and balance probes to return cancelable plugin sync connections.
This commit is contained in:
@@ -59,7 +59,14 @@ func (c *Client) rebalanceCommand(ctx context.Context, address string, args ...a
|
||||
}
|
||||
options.ReadTimeout = -1
|
||||
options.WriteTimeout = -1
|
||||
options.Dialer = redis.NewDialer(options)
|
||||
baseDialer := redis.NewDialer(options)
|
||||
options.Dialer = func(dialCtx context.Context, network, dialAddress string) (net.Conn, error) {
|
||||
conn, errDial := baseDialer(dialCtx, network, dialAddress)
|
||||
if errDial != nil {
|
||||
return nil, errDial
|
||||
}
|
||||
return newPluginSyncCancelableConn(ctx, conn), nil
|
||||
}
|
||||
command := redis.NewClient(options)
|
||||
stopCancel := context.AfterFunc(ctx, func() { _ = command.Close() })
|
||||
defer func() {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -284,7 +285,14 @@ func (c *Client) probeClusterLatency(ctx context.Context, address string) (time.
|
||||
options.TLSConfig = tlsConfig
|
||||
options.ReadTimeout, options.WriteTimeout, options.MaxRetries = -1, -1, -1
|
||||
options.PoolSize = 1
|
||||
options.Dialer = redis.NewDialer(options)
|
||||
baseDialer := redis.NewDialer(options)
|
||||
options.Dialer = func(dialCtx context.Context, network, dialAddress string) (net.Conn, error) {
|
||||
conn, errDial := baseDialer(dialCtx, network, dialAddress)
|
||||
if errDial != nil {
|
||||
return nil, errDial
|
||||
}
|
||||
return newPluginSyncCancelableConn(ctx, conn), nil
|
||||
}
|
||||
probe := redis.NewClient(options)
|
||||
stopCancel := context.AfterFunc(ctx, func() { _ = probe.Close() })
|
||||
defer func() {
|
||||
|
||||
@@ -1552,7 +1552,12 @@ func rollbackRecoveredGitDirectory(gitDir, backupGitDir string) error {
|
||||
}
|
||||
|
||||
func isRepositoryCorruptionError(err error) bool {
|
||||
return errors.Is(err, dotgit.ErrPackfileNotFound) || errors.Is(err, plumbing.ErrObjectNotFound)
|
||||
if errors.Is(err, dotgit.ErrPackfileNotFound) ||
|
||||
errors.Is(err, dotgit.ErrIdxNotFound) ||
|
||||
errors.Is(err, plumbing.ErrObjectNotFound) {
|
||||
return true
|
||||
}
|
||||
return errors.Is(err, fs.ErrNotExist) && err != nil && strings.Contains(err.Error(), "packhandle:")
|
||||
}
|
||||
|
||||
func verifyRepositoryHead(repo *git.Repository) error {
|
||||
|
||||
@@ -1798,14 +1798,22 @@ func corruptGitRepository(t *testing.T, repoDir string) {
|
||||
if errOpen != nil {
|
||||
t.Fatalf("open repository before corruption: %v", errOpen)
|
||||
}
|
||||
repoClosed := false
|
||||
defer func() {
|
||||
if errClose := repo.Close(); errClose != nil {
|
||||
t.Errorf("close corrupted repository: %v", errClose)
|
||||
if !repoClosed {
|
||||
if errClose := repo.Close(); errClose != nil {
|
||||
t.Errorf("close repository before corruption: %v", errClose)
|
||||
}
|
||||
}
|
||||
}()
|
||||
if errRepack := repo.RepackObjects(&git.RepackConfig{}); errRepack != nil {
|
||||
t.Fatalf("repack repository objects: %v", errRepack)
|
||||
}
|
||||
errClose := repo.Close()
|
||||
repoClosed = true
|
||||
if errClose != nil {
|
||||
t.Fatalf("close repacked repository: %v", errClose)
|
||||
}
|
||||
objectsDir := filepath.Join(repoDir, ".git", "objects")
|
||||
objectEntries, errReadDir := os.ReadDir(objectsDir)
|
||||
if errReadDir != nil {
|
||||
@@ -1830,7 +1838,16 @@ func corruptGitRepository(t *testing.T, repoDir string) {
|
||||
t.Fatalf("remove packfile %s: %v", filepath.Base(packfile), errRemove)
|
||||
}
|
||||
}
|
||||
if errVerify := verifyRepositoryHead(repo); !isRepositoryCorruptionError(errVerify) {
|
||||
corruptedRepo, errOpenCorrupted := git.PlainOpen(repoDir)
|
||||
if errOpenCorrupted != nil {
|
||||
t.Fatalf("open corrupted repository: %v", errOpenCorrupted)
|
||||
}
|
||||
defer func() {
|
||||
if errCloseCorrupted := corruptedRepo.Close(); errCloseCorrupted != nil {
|
||||
t.Errorf("close corrupted repository: %v", errCloseCorrupted)
|
||||
}
|
||||
}()
|
||||
if errVerify := verifyRepositoryHead(corruptedRepo); !isRepositoryCorruptionError(errVerify) {
|
||||
t.Fatalf("verifyRepositoryHead error = %v, want repository corruption", errVerify)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user