Skip to content

Commit

Permalink
fix goroutine leak in compilation
Browse files Browse the repository at this point in the history
Creating a new docker.Client each time was leaking persistent http
conns.

Change-Id: Ib6302b2bffcb54373b8057ab26974fa1d9c347c8
  • Loading branch information
codekitchen committed Dec 14, 2015
1 parent 1d64a10 commit 218463a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
10 changes: 4 additions & 6 deletions engine/compilation.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ func (image *Image) Remove() {
// Compile sets up a new Container and gets ready to execute the given source code.
// It's important to Remove the Container to clean up resources.
func (lang *Language) Compile(timeout int64, source string) (image *Image, result *ExecutionResult, err error) {
client, _ := docker.NewClient(endpoint)

filePath := fmt.Sprintf("/src/%s", lang.Filename)

container, err := createContainer(client, docker.CreateContainerOptions{
container, err := createContainer(lang.client, docker.CreateContainerOptions{
Config: &docker.Config{
Image: lang.DockerImage,
Cmd: []string{"--build", filePath},
Expand All @@ -87,7 +85,7 @@ func (lang *Language) Compile(timeout int64, source string) (image *Image, resul
})

if err == nil {
err = client.UploadToContainer(container.id, docker.UploadToContainerOptions{
err = lang.client.UploadToContainer(container.id, docker.UploadToContainerOptions{
InputStream: lang.tarSource(source, filePath),
Path: "/",
})
Expand All @@ -112,12 +110,12 @@ func (lang *Language) Compile(timeout int64, source string) (image *Image, resul
image = &Image{
ID: container.id,
lang: lang,
client: client,
client: lang.client,
}
}

if image == nil {
client.RemoveContainer(docker.RemoveContainerOptions{ID: container.id, Force: true})
lang.client.RemoveContainer(docker.RemoveContainerOptions{ID: container.id, Force: true})
}

return
Expand Down
8 changes: 6 additions & 2 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func New(confPath string, disableApparmor bool) (result *Engine, err error) {
return
}

result.client, err = docker.NewClient(endpoint)
if err != nil {
return
}

for _, config := range configs {
var lang *Language
lang, err = loadLanguage(config)
Expand All @@ -42,14 +47,13 @@ func New(confPath string, disableApparmor bool) (result *Engine, err error) {
// fail everything if one language fails to load
return
}
lang.client = result.client
if disableApparmor {
lang.disableAppArmor()
}
result.languages = append(result.languages, lang)
}

result.client, err = docker.NewClient(endpoint)

return
}

Expand Down
2 changes: 2 additions & 0 deletions engine/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"io/ioutil"

"github.com/fsouza/go-dockerclient"
"gopkg.in/yaml.v2"
)

Expand All @@ -21,6 +22,7 @@ type Language struct {
CompilerProfile string `yaml:"compiler_profile"`
Checks Checks `yaml:"tests"`
FileExtensions []string `yaml:"file_extensions"`
client *docker.Client
}

// RunResult contains the full results for each executed step of a code run.
Expand Down

0 comments on commit 218463a

Please sign in to comment.