Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #54 from StrongMonkey/leak
Browse files Browse the repository at this point in the history
close resource and add client timeout
  • Loading branch information
ibuildthecloud authored Oct 10, 2016
2 parents 0b13c3a + 22a40c0 commit 086ff80
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 11 deletions.
2 changes: 1 addition & 1 deletion core/delegate/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NsExec(pid int, event *revents.Event) (int, string, map[string]interface{},
if fileErr != nil {
return 1, "", map[string]interface{}{}, errors.Wrap(err, constants.NsExecError+"failed to open environ files")
}
defer file.Close()
for _, line := range strings.Split(utils.ReadBuffer(file), "\x00") {
if len(line) == 0 {
continue
Expand Down Expand Up @@ -61,7 +62,6 @@ func NsExec(pid int, event *revents.Event) (int, string, map[string]interface{},
existCMD := exec.Command("nsenter", exCMD...)
existCMD.Env = envs
_, err1 := existCMD.CombinedOutput()
existCMD.Wait()
if err1 == nil {
output = buffer
break
Expand Down
11 changes: 9 additions & 2 deletions core/hostInfo/iops_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ func (i IopsCollector) getIopsData(readOrWrite string) (map[string]interface{},
if err != nil {
return map[string]interface{}{}, err
}
data, _ := ioutil.ReadAll(file)
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return map[string]interface{}{}, err
}
var result map[string]interface{}
json.Unmarshal(data, result)
err = json.Unmarshal(data, result)
if err != nil {
return map[string]interface{}{}, err
}
return result, nil
}

Expand Down
10 changes: 9 additions & 1 deletion core/ping/ping_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/rancher/agent/model"
"github.com/rancher/agent/utilities/config"
"github.com/rancher/agent/utilities/constants"
"github.com/rancher/agent/utilities/docker"
"github.com/rancher/agent/utilities/utils"
revents "github.com/rancher/event-subscriber/events"
"github.com/shirou/gopsutil/disk"
Expand Down Expand Up @@ -138,7 +139,14 @@ func addInstance(ping *revents.Event, pong *model.PingResponse, dockerClient *cl
UUID: uuid,
})
containers := []model.PingResource{}
running, nonrunning, err := getAllContainerByState(dockerClient)
header := map[string]string{}
header["timeout"] = "2"
dc, err := docker.NewEnvClientWithHeader(header)
if err != nil {
return errors.Wrap(err, constants.AddInstanceError+"failed to get docker client")
}
dc.UpdateClientVersion(constants.DefaultVersion)
running, nonrunning, err := getAllContainerByState(dc)
if err != nil {
return errors.Wrap(err, constants.AddInstanceError+"failed to get docker UUID")
}
Expand Down
1 change: 1 addition & 0 deletions core/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func DoImageActivate(image model.Image, storagePool model.StoragePool, progress
if err != nil {
return errors.Wrap(err, "Failed to pull image")
}
defer reader.Close()
buffer := utils.ReadBuffer(reader)
statusList := strings.Split(buffer, "\r\n")
for _, rawStatus := range statusList {
Expand Down
1 change: 1 addition & 0 deletions core/storage/storage_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func doBuild(opts model.BuildOptions, progress *progress.Progress, client *clien
if err != nil {
return errors.Wrap(err, constants.DoBuildError+"failed to build image")
}
defer response.Body.Close()
buffer := utils.ReadBuffer(response.Body)
statusList := strings.Split(buffer, "\r\n")
for _, rawStatus := range statusList {
Expand Down
7 changes: 6 additions & 1 deletion events/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ func Listen(eventURL, accessKey, secretKey string, workerCount int) error {
}()

eventHandlers := handlers.GetHandlers()
router, err := revents.NewEventRouter("", 0, eventURL, accessKey, secretKey, nil, eventHandlers, "", workerCount, revents.DefaultPingConfig)
pingConfig := revents.PingConfig{
SendPingInterval: 5000,
CheckPongInterval: 5000,
MaxPongWait: 60000,
}
router, err := revents.NewEventRouter("", 0, eventURL, accessKey, secretKey, nil, eventHandlers, "", workerCount, pingConfig)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion handlers/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type PingHandler struct {
}

func (h *PingHandler) Ping(event *revents.Event, cli *client.RancherClient) error {
if event.Name != "ping" || event.ReplyTo == "" {
if event.ReplyTo == "" {
return nil
}
resp := model.PingResponse{
Expand Down
44 changes: 41 additions & 3 deletions utilities/docker/launch_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,54 @@
package docker

import (
"github.com/docker/engine-api/client"
dclient "github.com/docker/engine-api/client"
"github.com/docker/go-connections/tlsconfig"
"github.com/pkg/errors"
"github.com/rancher/agent/utilities/constants"
"net/http"
"os"
"path/filepath"
)

func launchDefaultClient(version string) (*client.Client, error) {
cli, err := client.NewEnvClient()
func launchDefaultClient(version string) (*dclient.Client, error) {
cli, err := dclient.NewEnvClient()
if err != nil {
return nil, errors.Wrap(err, constants.LaunchDefaultClientError)
}
cli.UpdateClientVersion(version)
return cli, nil
}

func NewEnvClientWithHeader(header map[string]string) (*dclient.Client, error) {
var client *http.Client
if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); dockerCertPath != "" {
options := tlsconfig.Options{
CAFile: filepath.Join(dockerCertPath, "ca.pem"),
CertFile: filepath.Join(dockerCertPath, "cert.pem"),
KeyFile: filepath.Join(dockerCertPath, "key.pem"),
InsecureSkipVerify: os.Getenv("DOCKER_TLS_VERIFY") == "",
}
tlsc, err := tlsconfig.Client(options)
if err != nil {
return nil, err
}

client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsc,
},
}
}

host := os.Getenv("DOCKER_HOST")
if host == "" {
host = dclient.DefaultDockerHost
}

version := os.Getenv("DOCKER_API_VERSION")
if version == "" {
version = dclient.DefaultVersion
}

return dclient.NewClient(host, version, client, header)
}
2 changes: 1 addition & 1 deletion utilities/utils/download_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func validateChecksum(fileName string, checksumValue string) error {
func downloadFromURL(rawurl string, filepath string) error {
file, err := os.OpenFile(filepath, os.O_WRONLY, 0666)
if err == nil {
defer file.Close()
response, err1 := http.Get(rawurl)
if err1 != nil {
logrus.Error(fmt.Sprintf("Error while downloading error: %s", err1))
return err1
}
defer file.Close()
defer response.Body.Close()
n, err := io.Copy(file, response.Body)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion utilities/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ func HasLabel(instance model.Instance) bool {
func ReadBuffer(reader io.ReadCloser) string {
buffer := make([]byte, 1024)
s := ""
defer reader.Close()
for {
n, err := reader.Read(buffer)
s = s + string(buffer[:n])
Expand Down

0 comments on commit 086ff80

Please sign in to comment.