Skip to content

Commit

Permalink
parallel container removal
Browse files Browse the repository at this point in the history
  • Loading branch information
pfandzelter committed Jul 3, 2023
1 parent cbca669 commit 1c17cf1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 49 deletions.
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
PROJECT_NAME := "tinyFaaS"
PKG := "github.com/pfandzelter/$(PROJECT_NAME)"
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v /ext/ | grep -v _test.go)

.PHONY: all build start clean

all: build clean start

build: manager rproxy

manager: ./cmd/manager/main.go
@go build -o ./manager ./cmd/manager/*.go

rproxy: ./cmd/rproxy/main.go
@go build -o ./rproxy ./cmd/rproxy/*.go
manager rproxy: $(GO_FILES)
@go build -o $@ -v $(PKG)/cmd/$@

start:
./manager
Expand Down
86 changes: 50 additions & 36 deletions pkg/docker/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"path"
"sync"
"time"

"github.com/docker/docker/api/types"
Expand All @@ -20,7 +21,8 @@ import (
)

const (
TmpDir = "./tmp"
TmpDir = "./tmp"
containerTimeout = 1
)

type dockerHandler struct {
Expand Down Expand Up @@ -198,18 +200,26 @@ func (dh *dockerHandler) Start() error {

// start containers
// docker start <container>

wg := sync.WaitGroup{}
for _, container := range dh.thisContainers {
err := client.ContainerStart(
context.Background(),
container,
types.ContainerStartOptions{},
)
if err != nil {
return err
}
wg.Add(1)
go func(c string) {
err := client.ContainerStart(
context.Background(),
c,
types.ContainerStartOptions{},
)
wg.Done()
if err != nil {
log.Printf("error starting container %s: %s", c, err)
return
}

log.Println("started container", container)
log.Println("started container", c)
}(container)
}
wg.Wait()

// get container IPs
// docker inspect <container>
Expand Down Expand Up @@ -275,40 +285,44 @@ func (dh *dockerHandler) Destroy() error {

log.Printf("fh: %+v", dh)

// stop containers
// docker stop <container>
wg := sync.WaitGroup{}
log.Printf("stopping containers: %v", dh.thisContainers)
for _, c := range dh.thisContainers {
log.Println("stopping container", c)
log.Println("removing container", c)

err := client.ContainerStop(
context.Background(),
c,
container.StopOptions{},
)
if err != nil {
return err
}
wg.Add(1)
go func(c string) {
log.Println("stopping container", c)

log.Println("stopped container", c)
}
timeout := 1 // seconds

// remove containers
// docker rm <container>
for _, container := range dh.thisContainers {
log.Println("removing container", container)
err := client.ContainerStop(
context.Background(),
c,
container.StopOptions{
Timeout: &timeout,
},
)
if err != nil {
log.Printf("error stopping container %s: %s", c, err)
}

err := client.ContainerRemove(
context.Background(),
container,
types.ContainerRemoveOptions{},
)
if err != nil {
return err
}
log.Println("stopped container", c)

err = client.ContainerRemove(
context.Background(),
c,
types.ContainerRemoveOptions{},
)
wg.Done()
if err != nil {
log.Printf("error removing container %s: %s", c, err)
}
}(c)

log.Println("removed container", container)
log.Println("removed container", c)
}
wg.Wait()

// remove network
// docker network rm <network>
Expand Down
10 changes: 2 additions & 8 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,9 @@ func (ms *ManagementService) LogsFunction(name string) (string, error) {
}

func (ms *ManagementService) Wipe() error {
for name, fh := range ms.functionHandlers {
for name := range ms.functionHandlers {
log.Println("destroying function", name)

err := fh.Destroy()
if err != nil {
return err
}

log.Println("removed function", name)
ms.Delete(name)
}

return nil
Expand Down

0 comments on commit 1c17cf1

Please sign in to comment.