Skip to content

Commit

Permalink
PAUSED: Use git resume to continue working.
Browse files Browse the repository at this point in the history
  • Loading branch information
ibash committed Dec 14, 2022
1 parent a74e63c commit 5cb1ffb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions internal/backend/sftp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ type Config struct {
Command string `option:"command" help:"specify command to create sftp connection"`

Connections uint `option:"connections" help:"set a limit for the number of concurrent connections (default: 5)"`
MaxReconnects uint `option:"max-reconnects" help:"set a limit for the number of reconnects when the connection is dropped (default: 0)"`
}

// NewConfig returns a new config with default options applied.
func NewConfig() Config {
return Config{
Connections: 5,
MaxReconnects: 0
}
}

Expand Down
15 changes: 13 additions & 2 deletions internal/backend/sftp/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type SFTP struct {
Config
*Connection
backend.Modes
mutex sync.Mutex
reconnectsLeft uint
}

var _ restic.Backend = &SFTP{}
Expand All @@ -46,11 +48,21 @@ func startClient(cfg Config) (*SFTP, error) {

func (r *SFTP) reconnect() error {
debug.Log("RECONNECT")
r.mutex.Lock()
defer r.mutex.Unlock()
debug.Log("after mutext")

r.reconnectsLeft--

if r.reconnectsLeft < 0 {
debug.Log("LIMIT REACHED!!!!")
err := errors.Wrapf(r.Connection.clientError(), "max reconnections limit reached")
return backoff.Permanent(err)
}
if r.isClosing {
return nil
}

// TODO(ibash) make this thread safe
// TODO(ibash) error handling here...?
r.Connection.Close()
connection, err := NewConnection(r.Config)
Expand All @@ -60,7 +72,6 @@ func (r *SFTP) reconnect() error {
}

r.Connection = connection
// TODO(ibash) if already connected, clear out the old connection
return nil
}

Expand Down

0 comments on commit 5cb1ffb

Please sign in to comment.