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

improve public autoconnect module #1266

Merged
merged 2 commits into from
Jun 22, 2022
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
58 changes: 30 additions & 28 deletions pkg/servicedisc/autoconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,45 @@ func MakeConnector(conf Config, maxConns int, tm *transport.Manager, httpC *http
func (a *autoconnector) Run(ctx context.Context) (err error) {
// failed addresses will be populated everytime any failed attempt at establishing transport occurs.
failedAddresses := map[cipher.PubKey]int{}
publicServiceTicket := time.NewTicker(PublicServiceDelay)

for {
time.Sleep(PublicServiceDelay)

// successfully established transports
tps := a.tm.GetTransportsByLabel(transport.LabelAutomatic)

// don't fetch public addresses if there are more or equal to the number of maximum transport defined.
if len(tps) >= a.maxConns {
a.log.Debugln("autoconnect: maximum number of established transports reached: ", a.maxConns)
return err
}
select {
case <-publicServiceTicket.C:
// successfully established transports
tps := a.tm.GetTransportsByLabel(transport.LabelAutomatic)

// don't fetch public addresses if there are more or equal to the number of maximum transport defined.
if len(tps) >= a.maxConns {
a.log.Debugln("autoconnect: maximum number of established transports reached: ", a.maxConns)
return err
}

a.log.Debugln("Fetching public visors")
addrs, err := a.fetchPubAddresses(ctx)
if err != nil {
if !errors.Is(context.Canceled, err) {
a.log.Infoln("Fetching public visors")
addrs, err := a.fetchPubAddresses(ctx)
if err != nil {
a.log.Errorf("Cannot fetch public services: %s", err)
}
}

// filter out any established transports
absent := a.filterDuplicates(addrs, tps)

for _, pk := range absent {
val, ok := failedAddresses[pk]
if !ok || val < maxFailedAddressRetryAttempt {
a.log.WithField("pk", pk).WithField("attempt", val).Debugln("Trying to add transport to public visor")
logger := a.log.WithField("pk", pk).WithField("type", string(network.STCPR))
if err = a.tryEstablishTransport(ctx, pk, logger); err != nil {
if !errors.Is(err, io.ErrClosedPipe) {
logger.WithError(err).Warnln("Failed to add transport to public visor")
// filter out any established transports
absent := a.filterDuplicates(addrs, tps)

for _, pk := range absent {
val, ok := failedAddresses[pk]
if !ok || val < maxFailedAddressRetryAttempt {
a.log.WithField("pk", pk).WithField("attempt", val).Debugln("Trying to add transport to public visor")
logger := a.log.WithField("pk", pk).WithField("type", string(network.STCPR))
if err = a.tryEstablishTransport(ctx, pk, logger); err != nil {
if !errors.Is(err, io.ErrClosedPipe) {
logger.WithError(err).Warnln("Failed to add transport to public visor")
}
failedAddresses[pk]++
continue
}
failedAddresses[pk]++
continue
}
}
case <-ctx.Done():
return context.Canceled
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/visor/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,14 @@ func initPublicAutoconnect(ctx context.Context, v *Visor, log *logging.Logger) e
return err
}
connector := servicedisc.MakeConnector(conf, 3, v.tpM, v.serviceDisc.Client, pIP, log, v.MasterLogger())
go connector.Run(ctx) //nolint:errcheck

cctx, cancel := context.WithCancel(ctx)
v.pushCloseStack("public_autoconnect", func() error {
cancel()
return err
})

go connector.Run(cctx) //nolint:errcheck

return nil
}
Expand Down