Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/refactor init #858

Merged
merged 2 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/util/netutil/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,15 @@ func HasPublicIP() (bool, error) {
}
return false, nil
}

// ExtractPort returns port of the given UDP or TCP address
func ExtractPort(addr net.Addr) (uint16, error) {
switch address := addr.(type) {
case *net.TCPAddr:
return uint16(address.Port), nil
case *net.UDPAddr:
return uint16(address.Port), nil
default:
return 0, fmt.Errorf("extract port: invalid address: %s", addr.String())
}
}
74 changes: 18 additions & 56 deletions pkg/visor/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"net"
"net/http"
"strconv"
"sync"
"time"

Expand Down Expand Up @@ -301,9 +300,9 @@ func initTransportSetup(ctx context.Context, v *Visor, log *logging.Logger) erro
func initRouter(ctx context.Context, v *Visor, log *logging.Logger) error {
conf := v.conf.Routing
rfClient := rfclient.NewHTTP(conf.RouteFinder, time.Duration(conf.RouteFinderTimeout))

logger := v.MasterLogger().PackageLogger("router")
rConf := router.Config{
Logger: v.MasterLogger().PackageLogger("router"),
Logger: logger,
PubKey: v.conf.PK,
SecKey: v.conf.SK,
TransportManager: v.tpM,
Expand All @@ -320,40 +319,15 @@ func initRouter(ctx context.Context, v *Visor, log *logging.Logger) error {
return err
}

// todo: this piece is somewhat ugly and inherited from the times when init was
// calling init functions sequentially
// It is probably a hack to run init
// "somewhat concurrently", where the heaviest init functions will be partially concurrent

// to avoid this we can:
// either introduce some kind of "task" functionality that abstracts out
// something that has to be run concurrent to the init, and check on their status
// stop in close functions, etc

// or, we can completely rely on the module system, and just wait for everything
// in init functions, instead of spawning more goroutines.
// but, even though modules themselves are concurrent this can introduce some
// performance penalties, because dependencies will be waiting on complete init

// leaving as it is until future requirements about init and modules are known

serveCtx, cancel := context.WithCancel(context.Background())
wg := new(sync.WaitGroup)
wg.Add(1)

go func() {
defer wg.Done()
runtimeErrors := getErrors(ctx)
if err := r.Serve(serveCtx); err != nil {
runtimeErrors <- fmt.Errorf("serve router stopped: %w", err)
}
}()
if err := r.Serve(serveCtx); err != nil {
cancel()
return err
}

v.pushCloseStack("router.serve", func() error {
cancel()
err := r.Close()
wg.Wait()
return err
return r.Close()
})

v.initLock.Lock()
Expand Down Expand Up @@ -565,50 +539,38 @@ func initUptimeTracker(ctx context.Context, v *Visor, log *logging.Logger) error
return nil
}

// this service is not considered critical and always returns true
// advertise this visor as public in service discovery
// this service is not considered critical and always returns true
func initPublicVisor(_ context.Context, v *Visor, log *logging.Logger) error {
if !v.conf.IsPublic {
return nil
}

// retrieve interface IPs and check if at least one is public
defaultIPs, err := netutil.DefaultNetworkInterfaceIPs()
logger := v.MasterLogger().PackageLogger("public_visor")
hasPublic, err := netutil.HasPublicIP()
if err != nil {
logger.WithError(err).Warn("Failed to check for existing public IP address")
return nil
}
var found bool
for _, IP := range defaultIPs {
if netutil.IsPublicIP(IP) {
found = true
break
}
}
if !found {
if !hasPublic {
logger.Warn("No public IP address found, stopping")
return nil
}

// todo: consider moving this to transport into some helper function
stcpr, ok := v.tpM.Stcpr()
if !ok {
logger.Warn("No stcpr client found, stopping")
return nil
}
la, err := stcpr.LocalAddr()
if err != nil {
log.WithError(err).Errorln("Failed to get STCPR local addr")
return nil
}
_, portStr, err := net.SplitHostPort(la.String())
addr, err := stcpr.LocalAddr()
if err != nil {
log.WithError(err).Errorf("Failed to extract port from addr %v", la.String())
logger.Warn("Failed to get STCPR local addr")
return nil
}
port, err := strconv.Atoi(portStr)
port, err := netutil.ExtractPort(addr)
if err != nil {
log.WithError(err).Errorf("Failed to convert port to int")
logger.Warn("Failed to get STCPR port")
return nil
}

visorUpdater := v.serviceDisc.VisorUpdater(uint16(port))
visorUpdater.Start()

Expand Down