pool

package module
v0.0.0-...-026f90b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2023 License: MIT Imports: 5 Imported by: 0

README

ssh-client-pool

ssh-client-pool is a simple ssh client pool for golang.

Installation

go get github.com/WeiZhixiong/ssh-client-pool

Usage
package main

import (
	"fmt"
	pool "github.com/WeiZhixiong/ssh-client-pool"
	"time"
)

func main() {
	var (
		user     = "root"
		host     = "127.0.0.1"
		port     = 22
		password = "secret"
	)
	sshClientPool := pool.NewPool(time.Minute*10, time.Minute*10, 500, 1)

	// client, err := pool.NewSSHClient(user, host, port, pool.SetPassword(password), pool.SetPrivateKey("xxxxx", ""), pool.SetTimeout(10))
	// client, err := pool.NewSSHClient(user, host, port, pool.SetPrivateKey("xxx", ""), pool.SetTimeout(20))
	client, err := pool.NewSSHClient(user, host, port, pool.SetPassword(password), pool.SetTimeout(20))
	if err != nil {
		fmt.Printf("NewSSHClient error:%v", err)
		return
	}
	// client do something

	cacheKey := fmt.Sprintf("%s@%s:%d", user, host, port)
	err = sshClientPool.Put(cacheKey, client)
	if err != nil {
		fmt.Printf("Put error:%v\n", err)
		_ = client.Close()
		return
	}

	client, ok := sshClientPool.Get(cacheKey)
	if !ok {
		fmt.Printf("from pool get client failed\n")
		return
	}
	client.Close()

	// or use GetWithNew
	client, err = sshClientPool.GetWithNew(cacheKey, user, host, port, pool.SetPassword(password))
	if err != nil {
		fmt.Printf("GetWithNew error:%v\n", err)
		return
	}

	// client do something

	// put back to pool
	err = sshClientPool.Put(cacheKey, client)
	if err != nil {
		fmt.Printf("Put error:%v\n", err)
		_ = client.Close()
		return
	}

	// delete from pool
	sshClientPool.Delete(cacheKey)

	// close pool
	sshClientPool.Close()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func KeepAlive

func KeepAlive(c *ssh.Client) error

func NewSSHClient

func NewSSHClient(user, host string, port int, opts ...ClientCfgOption) (*ssh.Client, error)

Types

type ClientCfgOption

type ClientCfgOption func(c *ssh.ClientConfig)

func SetPassword

func SetPassword(password string) ClientCfgOption

func SetPrivateKey

func SetPrivateKey(privateKey, passPhrase string) ClientCfgOption

func SetTimeout

func SetTimeout(seconds int) ClientCfgOption

type Item

type Item struct {
	Object     *ssh.Client
	Expiration int64
}

func (Item) Expired

func (item Item) Expired() bool

Expired Returns true if the item has expired.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

func NewPool

func NewPool(defaultExpiration, cleanupInterval time.Duration, maxConn, eachKeyMaxConn int) *Pool

NewPool creates a new pool. defaultExpiration: The default expiration time. cleanupInterval: How often the pool is checked for expired items. maxConn: The maximum number of connections that can be held by the pool. eachKeyMaxConn: The maximum number of connections that can be held by the pool for each key.

func (*Pool) Close

func (p *Pool) Close()

func (*Pool) Delete

func (p *Pool) Delete(key string)

func (*Pool) Get

func (p *Pool) Get(key string) (*ssh.Client, bool)

Get return ssh client from the pool by the key, and the ssh client will be removed from the pool.

func (*Pool) GetWithNew

func (p *Pool) GetWithNew(key, user, host string, port int, opts ...ClientCfgOption) (c *ssh.Client, err error)

GetWithNew return ssh client from the pool, if there is no client in the pool, it will create a new one.

func (*Pool) Len

func (p *Pool) Len() int

func (*Pool) Put

func (p *Pool) Put(key string, c *ssh.Client) (err error)

Put adds an ssh Client to the pool key: suggested key is user + host + port

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL