Skip to content

Commit

Permalink
Better handling of the closing of sessions, client...
Browse files Browse the repository at this point in the history
  • Loading branch information
Acanthostega committed Jul 1, 2014
1 parent 6a48d3a commit fdefaba
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@ func (host *Host) IsConnected() (bool, error) {
}

// Contact the host and if no error, it is connected
_, err := dial.Dial(
conn, err := dial.Dial(
host.Protocol,
net.JoinHostPort(host.Hostname, strconv.Itoa(host.Port)),
)
if err == nil {
conn.Close()
}

return err == nil, err
}
Expand All @@ -103,6 +106,8 @@ func (host *Host) CreateSession(user user) (string, error) {

// create a session object
host.session, err = ssh.New(user)

// check errors at the creation
if err != nil {
return "The session for the connection can't be created!\n" +
"Reason is: " + err.Error(), err
Expand All @@ -126,9 +131,9 @@ func (host *Host) CreateSession(user user) (string, error) {
}

// add a session to connect to host
_, err = host.session.AddSession()
err = host.session.AddSession()
if err != nil {
// Close the session
// close the session
host.session.Close()

// exit the loop
Expand All @@ -144,10 +149,17 @@ func (host *Host) OneCommand(command *commands.Command) (string, error) {

// create a new session for the host
message, err := host.CreateSession(command.User)
defer func() {
err := host.session.Close()
if err != nil {
return
}
}()

// check error at the create of the session
if err != nil {
return message, err
}
defer host.session.Close()

// execute the command on the host
output, err := host.session.Run(command.Command)
Expand Down

0 comments on commit fdefaba

Please sign in to comment.