Make it even simpler by removing Init
parent
4b807ca95e
commit
9bbb2b0911
|
@ -146,7 +146,6 @@ type LocalBackend struct {
|
||||||
backendLogID logid.PublicID
|
backendLogID logid.PublicID
|
||||||
unregisterNetMon func()
|
unregisterNetMon func()
|
||||||
unregisterHealthWatch func()
|
unregisterHealthWatch func()
|
||||||
portpoll *portlist.Poller // may be nil
|
|
||||||
portpollOnce sync.Once // guards starting readPoller
|
portpollOnce sync.Once // guards starting readPoller
|
||||||
gotPortPollRes chan struct{} // closed upon first readPoller result
|
gotPortPollRes chan struct{} // closed upon first readPoller result
|
||||||
newDecompressor func() (controlclient.Decompressor, error)
|
newDecompressor func() (controlclient.Decompressor, error)
|
||||||
|
@ -292,12 +291,6 @@ func NewLocalBackend(logf logger.Logf, logID logid.PublicID, sys *tsd.System, lo
|
||||||
osshare.SetFileSharingEnabled(false, logf)
|
osshare.SetFileSharingEnabled(false, logf)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
portpoll := new(portlist.Poller)
|
|
||||||
err = portpoll.Init()
|
|
||||||
if err != nil {
|
|
||||||
logf("skipping portlist: %s", err)
|
|
||||||
portpoll = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
b := &LocalBackend{
|
b := &LocalBackend{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
|
@ -312,7 +305,6 @@ func NewLocalBackend(logf logger.Logf, logID logid.PublicID, sys *tsd.System, lo
|
||||||
pm: pm,
|
pm: pm,
|
||||||
backendLogID: logID,
|
backendLogID: logID,
|
||||||
state: ipn.NoState,
|
state: ipn.NoState,
|
||||||
portpoll: portpoll,
|
|
||||||
em: newExpiryManager(logf),
|
em: newExpiryManager(logf),
|
||||||
gotPortPollRes: make(chan struct{}),
|
gotPortPollRes: make(chan struct{}),
|
||||||
loginFlags: loginFlags,
|
loginFlags: loginFlags,
|
||||||
|
@ -1377,14 +1369,17 @@ func (b *LocalBackend) Start(opts ipn.Options) error {
|
||||||
b.updateFilterLocked(nil, ipn.PrefsView{})
|
b.updateFilterLocked(nil, ipn.PrefsView{})
|
||||||
b.mu.Unlock()
|
b.mu.Unlock()
|
||||||
|
|
||||||
if b.portpoll != nil {
|
|
||||||
b.portpollOnce.Do(func() {
|
b.portpollOnce.Do(func() {
|
||||||
updates, err := b.portpoll.Run(b.ctx)
|
var p portlist.Poller
|
||||||
|
updates, err := p.Run(b.ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.logf("error running port poller: %v", err)
|
b.logf("skipping portlist: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
go b.readPoller(updates)
|
go func() {
|
||||||
|
defer p.Close()
|
||||||
|
b.readPoller(updates)
|
||||||
|
}()
|
||||||
|
|
||||||
// Give the poller a second to get results to
|
// Give the poller a second to get results to
|
||||||
// prevent it from restarting our map poll
|
// prevent it from restarting our map poll
|
||||||
|
@ -1400,7 +1395,6 @@ func (b *LocalBackend) Start(opts ipn.Options) error {
|
||||||
b.logf("timeout waiting for initial portlist")
|
b.logf("timeout waiting for initial portlist")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
discoPublic := b.e.DiscoPublicKey()
|
discoPublic := b.e.DiscoPublicKey()
|
||||||
|
|
||||||
|
@ -1818,16 +1812,11 @@ func dnsMapsEqual(new, old *netmap.NetworkMap) bool {
|
||||||
// readPoller is a goroutine that receives service lists from
|
// readPoller is a goroutine that receives service lists from
|
||||||
// b.portpoll and propagates them into the controlclient's HostInfo.
|
// b.portpoll and propagates them into the controlclient's HostInfo.
|
||||||
func (b *LocalBackend) readPoller(updates chan portlist.Update) {
|
func (b *LocalBackend) readPoller(updates chan portlist.Update) {
|
||||||
defer b.portpoll.Close()
|
firstResults := true
|
||||||
n := 0
|
for update := range updates {
|
||||||
for {
|
|
||||||
update, ok := <-updates
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if update.Error != nil {
|
if update.Error != nil {
|
||||||
b.logf("error polling os ports: %v", update.Error)
|
b.logf("error polling os ports: %v", update.Error)
|
||||||
return // preserve all behavior, though we can just continue
|
return // preserve old behavior, though we can just continue and try again?
|
||||||
}
|
}
|
||||||
sl := []tailcfg.Service{}
|
sl := []tailcfg.Service{}
|
||||||
for _, p := range update.List {
|
for _, p := range update.List {
|
||||||
|
@ -1851,8 +1840,8 @@ func (b *LocalBackend) readPoller(updates chan portlist.Update) {
|
||||||
|
|
||||||
b.doSetHostinfoFilterServices(hi)
|
b.doSetHostinfoFilterServices(hi)
|
||||||
|
|
||||||
n++
|
if firstResults {
|
||||||
if n == 1 {
|
firstResults = false
|
||||||
close(b.gotPortPollRes)
|
close(b.gotPortPollRes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,20 +97,20 @@ func (p *Poller) setPrev(pl List) {
|
||||||
p.prev = slices.Clone(pl)
|
p.prev = slices.Clone(pl)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init is an optional method that makes sure the Poller is enabled
|
// init makes sure the Poller is enabled
|
||||||
// and the undelrying OS implementation is working properly.
|
// and the undelrying OS implementation is working properly.
|
||||||
//
|
//
|
||||||
// An error returned from Init is non-fatal and means
|
// An error returned from init is non-fatal and means
|
||||||
// that it's been administratively disabled or the underlying
|
// that it's been administratively disabled or the underlying
|
||||||
// OS is not implemented.
|
// OS is not implemented.
|
||||||
func (p *Poller) Init() error {
|
func (p *Poller) init() error {
|
||||||
p.initOnce.Do(func() {
|
p.initOnce.Do(func() {
|
||||||
p.initErr = p.init()
|
p.initErr = p.initWithErr()
|
||||||
})
|
})
|
||||||
return p.initErr
|
return p.initErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Poller) init() error {
|
func (p *Poller) initWithErr() error {
|
||||||
if debugDisablePortlist() {
|
if debugDisablePortlist() {
|
||||||
return errors.New("portlist disabled by envknob")
|
return errors.New("portlist disabled by envknob")
|
||||||
}
|
}
|
||||||
|
@ -170,7 +170,7 @@ func (p *Poller) send(ctx context.Context, pl List, plErr error) (sent bool) {
|
||||||
// Run may only be called once.
|
// Run may only be called once.
|
||||||
func (p *Poller) Run(ctx context.Context) (chan Update, error) {
|
func (p *Poller) Run(ctx context.Context) (chan Update, error) {
|
||||||
if p.os == nil {
|
if p.os == nil {
|
||||||
err := p.Init()
|
err := p.init()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error initializing poller: %w", err)
|
return nil, fmt.Errorf("error initializing poller: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -179,7 +179,7 @@ func TestEqualLessThan(t *testing.T) {
|
||||||
|
|
||||||
func TestPoller(t *testing.T) {
|
func TestPoller(t *testing.T) {
|
||||||
var p Poller
|
var p Poller
|
||||||
err := p.Init()
|
err := p.init()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Skipf("not running test: %v", err)
|
t.Skipf("not running test: %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue