Documentation ¶
Overview ¶
Package sesspool provides a "pool" of ssh sessions. You can try to claim one from the pool, closing it when done. The pool has a configurable maximum size.
Each session, when released, is actually closed, and a new claim creates a new session over the provided connection.
Example:
// Create an ssh.Client however you want, then... pool := New(ctx, sshClient, WithMaxSessions(10)) sess, err := pool.Claim(ctx) if err != nil { log.Fatalf("Error claiming: %v", err) } defer sess.Close() sftpCli, err := sess.SFTPClient() if err != nil { log.Fatalf("Error getting sftp client: %v", err) } defer sftpCli.Close() // Use the sftpCli over the session until done.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // PoolExhausted is returned when the pool is "empty", meaning no more // session can be claimed. PoolExhausted = errors.New("session pool exhausted") // SessionNotFound is returned when attempting to close a session not in the pool. SessionNotFound = errors.New("not found") )
Functions ¶
func AsSFTPClient ¶
AsSFTPClient can wrap a Claim method to produce an SFTP client.
Example:
cli, cleanup, err := AsSFTPClient(pool.Claim(ctx)) if err != nil { // handle err } defer cleanup() // use cli
Types ¶
type Option ¶
type Option func(*Pool)
Option is used to modify how pools are created.
func WithMaxSessions ¶
WithMaxSessions sets the maximum allowed simultaneous sessions for a connection.
type Pool ¶
Pool provides convenient ways to create new sessions within an existing SSH connection.
func New ¶
New creates a new Pool given an already-created ssh client. It does not take ownership of the client: callers should clean it up on their own when finished.
func (*Pool) Claim ¶
Claim blocks on the pool until a session can be claimed. Returns immediately for unlimited pools.
func (*Pool) Close ¶
Close cleans up the sessions in this pool (but does not clean up the underlying connection). This should always be called when finished.
type SFTPClientCloser ¶
type SFTPClientCloser interface { SFTPClient(opts ...sftp.ClientOption) (*sftp.Client, error) Close() error }
SFTPClientCloser is the type required by AsSFTPClient, allowing it to accept a session from here or elsewhere so long as it can be used to create an SFTPClient and can be closed. The clientpool module's Session type satisfies this, as does the Session type found here.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session is an abstraction around ssh.Session that is pool-aware. When finished, callers should invoke Close on it to return it to the pool.
func (*Session) SFTPClient ¶
SFTPClient returns an sftp.Client for this session. Be sure to close it when finished.