Skip to content

Commit

Permalink
fix binding issues for monitor (issue #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
reddec committed Jan 22, 2020
1 parent ea0cf11 commit 93ef724
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 7 additions & 1 deletion domain/monitor/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package monitor

import (
"errors"
"net"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -40,5 +41,10 @@ func (cfg *Config) Binding() (string, error) {
if err != nil {
return "", err
}
return addrs[0].(*net.IPNet).IP.String() + ":" + strconv.Itoa(cfg.Port), nil
for _, addr := range addrs {
if v, ok := addr.(*net.IPNet); ok && v.IP.IsGlobalUnicast() {
return v.IP.String() + ":" + strconv.Itoa(cfg.Port), nil
}
}
return "127.0.0.1:0", errors.New("unable to detect binding address")
}
21 changes: 18 additions & 3 deletions domain/monitor/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"time"
)

const retryInterval = 3 * time.Second

func (cfg Config) CreateAndRun(ctx context.Context) (*service, error) {
bind, err := cfg.Binding()
if err != nil {
Expand All @@ -31,9 +33,22 @@ func (cfg Config) CreateAndRun(ctx context.Context) (*service, error) {
cancel: cancel,
events: cfg.events,
}
listener, err := net.Listen("tcp", bind)
if err != nil {
return nil, err

var listener net.Listener

for {
serverListener, err := net.Listen("tcp", bind)
if err != nil {
log.Println(err)
} else {
listener = serverListener
break
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(retryInterval):
}
}
api := srv.createAPI()

Expand Down

0 comments on commit 93ef724

Please sign in to comment.