Skip to content

Commit

Permalink
fix context & reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
shinebayar-g committed Nov 14, 2022
1 parent 6fd5e04 commit 6ad70a4
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (
"github.com/shinebayar-g/ufw-docker-automated/ufwhandler"
)

func createClient(ctx *context.Context) (*client.Client, error) {
func createClient() (*context.Context, *client.Client, error) {
ctx := context.Background()
client, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return client, err
return &ctx, client, err
}
_, err = client.Info(*ctx)
return client, err
_, err = client.Info(ctx)
return &ctx, client, err
}

func streamEvents(ctx *context.Context, c *client.Client) (<-chan events.Message, <-chan error) {
Expand All @@ -28,45 +29,47 @@ func streamEvents(ctx *context.Context, c *client.Client) (<-chan events.Message
return c.Events(*ctx, types.EventsOptions{Filters: filter})
}

func reconnect(ctx *context.Context, c *client.Client) {
func reconnect() (*context.Context, *client.Client, error) {
var ctx *context.Context
var client *client.Client
var err error
for {
time.Sleep(5 * time.Second)
log.Println("ufw-docker-automated: Trying to reconnect..")
var err error
c, err = createClient(ctx)
ctx, client, err = createClient()
if err == nil {
break
}
}
log.Println("ufw-docker-automated: Reconnected to the Docker Engine.")
return ctx, client, err
}

func main() {
ctx := context.Background()
client, err := createClient(&ctx)
ctx, client, err := createClient()
if err != nil {
log.Println("ufw-docker-automated: Client error:", err)
reconnect(&ctx, client)
ctx, client, err = reconnect()
} else {
log.Println("ufw-docker-automated: Connected to the Docker Engine.")
}
log.Println("ufw-docker-automated: Connected to the Docker Engine.")

createChannel := make(chan *types.ContainerJSON)
deleteChannel := make(chan string)

trackedContainers := make(map[string]*ufwhandler.TrackedContainer)

go ufwhandler.CreateUfwRule(createChannel, trackedContainers)
go ufwhandler.DeleteUfwRule(deleteChannel, trackedContainers)
go ufwhandler.Cleanup(client, &ctx)
go ufwhandler.Sync(createChannel, client, &ctx)
go ufwhandler.Cleanup(client, ctx)
go ufwhandler.Sync(createChannel, client, ctx)

messages, errors := streamEvents(&ctx, client)
messages, errors := streamEvents(ctx, client)
for {
select {
case msg := <-messages:
if ufwManaged := msg.Actor.Attributes["UFW_MANAGED"]; strings.ToUpper(ufwManaged) == "TRUE" {
if msg.Action == "start" {
container, err := client.ContainerInspect(ctx, msg.ID)
container, err := client.ContainerInspect(*ctx, msg.ID)
if err != nil {
log.Println("ufw-docker-automated: Couldn't inspect container:", err)
continue
Expand All @@ -80,7 +83,8 @@ func main() {
case err := <-errors:
if err != nil {
log.Println("ufw-docker-automated: Event error:", err)
reconnect(&ctx, client)
ctx, client, err = reconnect()
messages, errors = streamEvents(ctx, client)
}
}
}
Expand Down

0 comments on commit 6ad70a4

Please sign in to comment.