Skip to content

Commit

Permalink
Make await timeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
devplayer0 committed Jun 10, 2021
1 parent 7981235 commit f5faea0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
11 changes: 10 additions & 1 deletion cmd/net-dhcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"os"
"os/signal"
"time"

log "github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -42,7 +43,15 @@ func main() {
log.StandardLogger().Out = f
}

p, err := plugin.NewPlugin()
awaitTimeout := 5 * time.Second
if t, ok := os.LookupEnv("AWAIT_TIMEOUT"); ok {
awaitTimeout, err = time.ParseDuration(t)
if err != nil {
log.WithError(err).Fatal("Failed to parse await timeout")
}
}

p, err := plugin.NewPlugin(awaitTimeout)
if err != nil {
log.WithError(err).Fatal("Failed to create plugin")
}
Expand Down
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
"settable": [
"value"
]
},
{
"description": "Log level",
"name": "AWAIT_TIMEOUT",
"value": "10s",
"settable": [
"value"
]
}
],
"workdir": "/",
Expand Down
4 changes: 1 addition & 3 deletions pkg/plugin/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net"
"time"

dTypes "github.com/docker/docker/api/types"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -441,8 +440,7 @@ func (p *Plugin) Join(ctx context.Context, r JoinRequest) (JoinResponse, error)
}

go func() {
// TODO: Make timeout configurable?
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), p.awaitTimeout)
defer cancel()

m := newDHCPManager(p.docker, r, opts)
Expand Down
6 changes: 5 additions & 1 deletion pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type joinHint struct {

// Plugin is the DHCP network plugin
type Plugin struct {
awaitTimeout time.Duration

docker *docker.Client
server http.Server

Expand All @@ -63,13 +65,15 @@ type Plugin struct {
}

// NewPlugin creates a new Plugin
func NewPlugin() (*Plugin, error) {
func NewPlugin(awaitTimeout time.Duration) (*Plugin, error) {
client, err := docker.NewClient("unix:///run/docker.sock", "v1.13.1", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to create docker client: %w", err)
}

p := Plugin{
awaitTimeout: awaitTimeout,

docker: client,

joinHints: make(map[string]joinHint),
Expand Down

0 comments on commit f5faea0

Please sign in to comment.