Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make StdOut buffer size configurable #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ type clientStats struct {

// clientAttr represents client attributes
type clientAttr struct {
addr string
labels map[string]string
config *ssh.ClientConfig
client *ssh.Client
logger *log.Logger
maxSessions uint8
curSessions uint8
lastUpdate time.Time
pty pty
stats clientStats
err error
addr string
labels map[string]string
config *ssh.ClientConfig
client *ssh.Client
logger *log.Logger
maxSessions uint8
curSessions uint8
lastUpdate time.Time
pty pty
stats clientStats
err error
stdOutBufferSize int

sync.RWMutex
}
Expand Down Expand Up @@ -297,6 +298,11 @@ func (c *clientAttr) getScanners(s *ssh.Session, lOut, lErr int64) (*bufio.Scann
scanOut = bufio.NewScanner(readerOut)
}

if c.stdOutBufferSize != 0 {
buf := make([]byte, c.stdOutBufferSize)
scanOut.Buffer(buf, c.stdOutBufferSize)
}

if lErr > 0 {
lReaderErr := io.LimitReader(readerErr, lErr)
scanErr = bufio.NewScanner(lReaderErr)
Expand Down
30 changes: 22 additions & 8 deletions vssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ func SetLabels(labels map[string]string) ClientOption {
}
}

// SetStdOutBufferSize sets stdout buffer size of client
// This should be set properly if expected output size is larger than 64kb
func SetStdOutBufferSize(n int) ClientOption {
return func(c *clientAttr) {
c.stdOutBufferSize = n
}
}

func clientValidation(c *clientAttr) error {
if c.config == nil {
return errSSHConfig
Expand All @@ -186,6 +194,7 @@ func clientValidation(c *clientAttr) error {

// Start starts vSSH, including action queue and re-connect procedures.
// You can construct and start the vssh like below:
//
// vs := vssh.New().Start()
func (v *VSSH) Start() *VSSH {
ctx := context.Background()
Expand Down Expand Up @@ -271,11 +280,14 @@ func (v *VSSH) CurrentProc() uint64 {
// SetInitNumProc sets the initial number of processes / workers.
//
// You need to set this number right after creating vssh.
//
// vs := vssh.New()
// vs.SetInitNumProc(200)
// vs.Start()
//
// There are two other methods in case you need to change
// the settings in the middle of your code.
//
// IncreaseProc(n int)
// DecreaseProc(n int)
func (v *VSSH) SetInitNumProc(n int) {
Expand Down Expand Up @@ -307,14 +319,15 @@ func (v *VSSH) Run(ctx context.Context, cmd string, timeout time.Duration, opts

// RunWithLabel runs the command on the specific clients which
// they matched with given query statement.
// labels := map[string]string {
// "POP" : "LAX",
// "OS" : "JUNOS",
// }
// // sets labels to a client
// vs.AddClient(addr, config, vssh.SetLabels(labels))
// // run the command with label
// vs.RunWithLabel(ctx, cmd, timeout, "POP == LAX || POP == DCA) && OS == JUNOS")
//
// labels := map[string]string {
// "POP" : "LAX",
// "OS" : "JUNOS",
// }
// // sets labels to a client
// vs.AddClient(addr, config, vssh.SetLabels(labels))
// // run the command with label
// vs.RunWithLabel(ctx, cmd, timeout, "POP == LAX || POP == DCA) && OS == JUNOS")
func (v *VSSH) RunWithLabel(ctx context.Context, cmd, queryStmt string, timeout time.Duration, opts ...RunOption) (chan *Response, error) {
vis, err := parseExpr(queryStmt)
if err != nil {
Expand Down Expand Up @@ -342,6 +355,7 @@ func (v *VSSH) RunWithLabel(ctx context.Context, cmd, queryStmt string, timeout
}

// SetLimitReaderStdout sets limit for stdout reader.
//
// respChan := vs.Run(ctx, cmd, timeout, vssh.SetLimitReaderStdout(1024))
func SetLimitReaderStdout(n int64) RunOption {
return func(q *query) {
Expand Down