wip
parent
e32e5c0d0c
commit
17c3f5c7d5
|
@ -146,7 +146,7 @@ type LocalBackend struct {
|
||||||
backendLogID logid.PublicID
|
backendLogID logid.PublicID
|
||||||
unregisterNetMon func()
|
unregisterNetMon func()
|
||||||
unregisterHealthWatch func()
|
unregisterHealthWatch func()
|
||||||
portpoll *portlist.Poller // may be nil
|
portpoll chan portlist.Update // 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,7 +292,8 @@ 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, err := portlist.NewPoller()
|
var p portlist.Poller
|
||||||
|
portUpdates, err := p.Run(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logf("skipping portlist: %s", err)
|
logf("skipping portlist: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -310,7 +311,7 @@ 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,
|
portpoll: portUpdates,
|
||||||
em: newExpiryManager(logf),
|
em: newExpiryManager(logf),
|
||||||
gotPortPollRes: make(chan struct{}),
|
gotPortPollRes: make(chan struct{}),
|
||||||
loginFlags: loginFlags,
|
loginFlags: loginFlags,
|
||||||
|
@ -1377,7 +1378,6 @@ func (b *LocalBackend) Start(opts ipn.Options) error {
|
||||||
|
|
||||||
if b.portpoll != nil {
|
if b.portpoll != nil {
|
||||||
b.portpollOnce.Do(func() {
|
b.portpollOnce.Do(func() {
|
||||||
go b.portpoll.Run(b.ctx)
|
|
||||||
go b.readPoller()
|
go b.readPoller()
|
||||||
|
|
||||||
// Give the poller a second to get results to
|
// Give the poller a second to get results to
|
||||||
|
@ -1814,12 +1814,17 @@ func dnsMapsEqual(new, old *netmap.NetworkMap) bool {
|
||||||
func (b *LocalBackend) readPoller() {
|
func (b *LocalBackend) readPoller() {
|
||||||
n := 0
|
n := 0
|
||||||
for {
|
for {
|
||||||
ports, ok := <-b.portpoll.Updates()
|
update, ok := <-b.portpoll
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if update.Err() != nil {
|
||||||
|
// TODO(marwan-at-work): do we need to log this?
|
||||||
|
// TODO(marwan-at-work): should we return or keep trying?
|
||||||
|
return
|
||||||
|
}
|
||||||
sl := []tailcfg.Service{}
|
sl := []tailcfg.Service{}
|
||||||
for _, p := range ports {
|
for _, p := range update.List() {
|
||||||
s := tailcfg.Service{
|
s := tailcfg.Service{
|
||||||
Proto: tailcfg.ServiceProto(p.Proto),
|
Proto: tailcfg.ServiceProto(p.Proto),
|
||||||
Port: p.Port,
|
Port: p.Port,
|
||||||
|
|
|
@ -29,7 +29,7 @@ type Poller struct {
|
||||||
// This field should only be changed before calling Run.
|
// This field should only be changed before calling Run.
|
||||||
IncludeLocalhost bool
|
IncludeLocalhost bool
|
||||||
|
|
||||||
c chan List // unbuffered
|
c chan Update // unbuffered
|
||||||
|
|
||||||
// os, if non-nil, is an OS-specific implementation of the portlist getting
|
// os, if non-nil, is an OS-specific implementation of the portlist getting
|
||||||
// code. When non-nil, it's responsible for getting the complete list of
|
// code. When non-nil, it's responsible for getting the complete list of
|
||||||
|
@ -52,6 +52,23 @@ type Poller struct {
|
||||||
prev List // most recent data, not aliasing scratch
|
prev List // most recent data, not aliasing scratch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update is sent by Poller to indicate
|
||||||
|
// an update has been made to the machine's
|
||||||
|
// open ports. Receiver of this struct must
|
||||||
|
// check the Err() method before calling List().
|
||||||
|
type Update struct {
|
||||||
|
list List
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *Update) Err() error {
|
||||||
|
return u.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *Update) List() List {
|
||||||
|
return u.list
|
||||||
|
}
|
||||||
|
|
||||||
// osImpl is the OS-specific implementation of getting the open listening ports.
|
// osImpl is the OS-specific implementation of getting the open listening ports.
|
||||||
type osImpl interface {
|
type osImpl interface {
|
||||||
Close() error
|
Close() error
|
||||||
|
@ -71,32 +88,6 @@ var newOSImpl func(includeLocalhost bool) osImpl
|
||||||
|
|
||||||
var errUnimplemented = errors.New("portlist poller not implemented on " + runtime.GOOS)
|
var errUnimplemented = errors.New("portlist poller not implemented on " + runtime.GOOS)
|
||||||
|
|
||||||
// NewPoller returns a new portlist Poller. It returns an error
|
|
||||||
// if the portlist couldn't be obtained.
|
|
||||||
func NewPoller() (*Poller, error) {
|
|
||||||
if debugDisablePortlist() {
|
|
||||||
return nil, errors.New("portlist disabled by envknob")
|
|
||||||
}
|
|
||||||
p := &Poller{
|
|
||||||
c: make(chan List),
|
|
||||||
runDone: make(chan struct{}),
|
|
||||||
}
|
|
||||||
p.closeCtx, p.closeCtxCancel = context.WithCancel(context.Background())
|
|
||||||
p.osOnce.Do(p.initOSField)
|
|
||||||
if p.os == nil {
|
|
||||||
return nil, errUnimplemented
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do one initial poll synchronously so we can return an error
|
|
||||||
// early.
|
|
||||||
if pl, err := p.getList(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
p.setPrev(pl)
|
|
||||||
}
|
|
||||||
return p, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Poller) setPrev(pl List) {
|
func (p *Poller) setPrev(pl List) {
|
||||||
// Make a copy, as the pass in pl slice aliases pl.scratch and we don't want
|
// Make a copy, as the pass in pl slice aliases pl.scratch and we don't want
|
||||||
// that to except to the caller.
|
// that to except to the caller.
|
||||||
|
@ -112,11 +103,15 @@ func (p *Poller) initOSField() {
|
||||||
// Updates return the channel that receives port list updates.
|
// Updates return the channel that receives port list updates.
|
||||||
//
|
//
|
||||||
// The channel is closed when the Poller is closed.
|
// The channel is closed when the Poller is closed.
|
||||||
func (p *Poller) Updates() <-chan List { return p.c }
|
func (p *Poller) Updates() <-chan Update { return p.c }
|
||||||
|
|
||||||
// Close closes the Poller.
|
// Close closes the Poller.
|
||||||
// Run will return with a nil error.
|
// Run will return with a nil error.
|
||||||
func (p *Poller) Close() error {
|
func (p *Poller) Close() error {
|
||||||
|
// Skip if uninitialized.
|
||||||
|
if p.os == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
p.closeCtxCancel()
|
p.closeCtxCancel()
|
||||||
<-p.runDone
|
<-p.runDone
|
||||||
if p.os != nil {
|
if p.os != nil {
|
||||||
|
@ -126,14 +121,14 @@ func (p *Poller) Close() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// send sends pl to p.c and returns whether it was successfully sent.
|
// send sends pl to p.c and returns whether it was successfully sent.
|
||||||
func (p *Poller) send(ctx context.Context, pl List) (sent bool, err error) {
|
func (p *Poller) send(ctx context.Context, pl List, listErr error) (sent bool) {
|
||||||
select {
|
select {
|
||||||
case p.c <- pl:
|
case p.c <- Update{list: pl, err: listErr}:
|
||||||
return true, nil
|
return true
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return false, ctx.Err()
|
return false
|
||||||
case <-p.closeCtx.Done():
|
case <-p.closeCtx.Done():
|
||||||
return false, nil
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,48 +136,82 @@ func (p *Poller) send(ctx context.Context, pl List) (sent bool, err error) {
|
||||||
// is done, or the Close is called.
|
// is done, or the Close is called.
|
||||||
//
|
//
|
||||||
// Run may only be called once.
|
// Run may only be called once.
|
||||||
func (p *Poller) Run(ctx context.Context) error {
|
func (p *Poller) Run(ctx context.Context) (chan Update, error) {
|
||||||
|
if debugDisablePortlist() {
|
||||||
|
return nil, errors.New("portlist disabled by envknob")
|
||||||
|
}
|
||||||
|
if p.os != nil {
|
||||||
|
return nil, errors.New("method called more than once")
|
||||||
|
}
|
||||||
|
p.initOSField()
|
||||||
|
if p.os == nil {
|
||||||
|
return nil, errUnimplemented
|
||||||
|
}
|
||||||
|
|
||||||
|
p.c = make(chan Update)
|
||||||
|
p.runDone = make(chan struct{})
|
||||||
|
p.closeCtx, p.closeCtxCancel = context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
// Do one initial poll synchronously so we can return an error
|
||||||
|
// early.
|
||||||
|
if pl, err := p.getList(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
p.setPrev(pl)
|
||||||
|
}
|
||||||
|
|
||||||
tick := time.NewTicker(pollInterval)
|
tick := time.NewTicker(pollInterval)
|
||||||
defer tick.Stop()
|
defer tick.Stop()
|
||||||
return p.runWithTickChan(ctx, tick.C)
|
go p.runWithTickChan(ctx, tick.C)
|
||||||
|
return p.c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Poller) runWithTickChan(ctx context.Context, tickChan <-chan time.Time) error {
|
func (p *Poller) runWithTickChan(ctx context.Context, tickChan <-chan time.Time) {
|
||||||
defer close(p.runDone)
|
defer close(p.runDone)
|
||||||
defer close(p.c)
|
defer close(p.c)
|
||||||
|
|
||||||
// Send out the pre-generated initial value.
|
// Send out the pre-generated initial value.
|
||||||
if sent, err := p.send(ctx, p.prev); !sent {
|
if sent := p.send(ctx, p.prev, nil); !sent {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Order of events:
|
||||||
|
// 1. If the context is done, exit
|
||||||
|
// 2. If the user called p.Close(), exit.
|
||||||
|
// 3. If we received a tick, then get the list
|
||||||
|
// 3B. If that error'd, send an error to the user.
|
||||||
|
// 3C. If the context or p.Close where called in the meantime, exit.
|
||||||
|
// 3D. If getList succeeded, skip if there are no updates.
|
||||||
|
// 3E. If there are indeed updates, send them, or exit if 1/2 are true.
|
||||||
|
// We check 1 & 2 in 3 places: top of the for-loop,
|
||||||
|
// whenever we send (which is two places: sending an error, or sending a list).
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-p.closeCtx.Done():
|
||||||
|
return
|
||||||
case <-tickChan:
|
case <-tickChan:
|
||||||
pl, err := p.getList()
|
pl, err := p.getList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
sent := p.send(ctx, nil, err)
|
||||||
|
if !sent {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if pl.equal(p.prev) {
|
if pl.equal(p.prev) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
p.setPrev(pl)
|
p.setPrev(pl)
|
||||||
if sent, err := p.send(ctx, p.prev); !sent {
|
if sent := p.send(ctx, p.prev, nil); !sent {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
case <-ctx.Done():
|
|
||||||
return ctx.Err()
|
|
||||||
case <-p.closeCtx.Done():
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Poller) getList() (List, error) {
|
func (p *Poller) getList() (List, error) {
|
||||||
if debugDisablePortlist() {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
p.osOnce.Do(p.initOSField)
|
|
||||||
var err error
|
var err error
|
||||||
p.scratch, err = p.os.AppendListeningPorts(p.scratch[:0])
|
p.scratch, err = p.os.AppendListeningPorts(p.scratch[:0])
|
||||||
return p.scratch, err
|
return p.scratch, err
|
||||||
|
|
|
@ -192,7 +192,7 @@ func TestPoller(t *testing.T) {
|
||||||
for pl := range p.Updates() {
|
for pl := range p.Updates() {
|
||||||
// Look at all the pl slice memory to maximize
|
// Look at all the pl slice memory to maximize
|
||||||
// chance of race detector seeing violations.
|
// chance of race detector seeing violations.
|
||||||
for _, v := range pl {
|
for _, v := range pl.List() {
|
||||||
if v == (Port{}) {
|
if v == (Port{}) {
|
||||||
// Force use
|
// Force use
|
||||||
panic("empty port")
|
panic("empty port")
|
||||||
|
|
Loading…
Reference in New Issue