@@ -620,18 +620,27 @@ func inTest() bool { return flag.Lookup("test.v") != nil }
620
620
621
621
// PollNetMap makes a /map request to download the network map, calling cb with
622
622
// each new netmap.
623
- //
624
- // maxPolls is how many network maps to download; common values are 1
625
- // or -1 (to keep a long-poll query open to the server).
626
- func (c * Direct ) PollNetMap (ctx context.Context , maxPolls int , cb func (* netmap.NetworkMap )) error {
627
- return c .sendMapRequest (ctx , maxPolls , cb )
623
+ func (c * Direct ) PollNetMap (ctx context.Context , cb func (* netmap.NetworkMap )) error {
624
+ return c .sendMapRequest (ctx , - 1 , false , cb )
625
+ }
626
+
627
+ // FetchNetMap fetches the netmap once.
628
+ func (c * Direct ) FetchNetMap (ctx context.Context ) (* netmap.NetworkMap , error ) {
629
+ var ret * netmap.NetworkMap
630
+ err := c .sendMapRequest (ctx , 1 , false , func (nm * netmap.NetworkMap ) {
631
+ ret = nm
632
+ })
633
+ if err == nil && ret == nil {
634
+ return nil , errors .New ("[unexpected] sendMapRequest success without callback" )
635
+ }
636
+ return ret , err
628
637
}
629
638
630
639
// SendLiteMapUpdate makes a /map request to update the server of our latest state,
631
640
// but does not fetch anything. It returns an error if the server did not return a
632
641
// successful 200 OK response.
633
642
func (c * Direct ) SendLiteMapUpdate (ctx context.Context ) error {
634
- return c .sendMapRequest (ctx , 1 , nil )
643
+ return c .sendMapRequest (ctx , 1 , false , nil )
635
644
}
636
645
637
646
// If we go more than pollTimeout without hearing from the server,
@@ -640,7 +649,7 @@ func (c *Direct) SendLiteMapUpdate(ctx context.Context) error {
640
649
const pollTimeout = 120 * time .Second
641
650
642
651
// cb nil means to omit peers.
643
- func (c * Direct ) sendMapRequest (ctx context.Context , maxPolls int , cb func (* netmap.NetworkMap )) error {
652
+ func (c * Direct ) sendMapRequest (ctx context.Context , maxPolls int , readOnly bool , cb func (* netmap.NetworkMap )) error {
644
653
metricMapRequests .Add (1 )
645
654
metricMapRequestsActive .Add (1 )
646
655
defer metricMapRequestsActive .Add (- 1 )
@@ -703,6 +712,16 @@ func (c *Direct) sendMapRequest(ctx context.Context, maxPolls int, cb func(*netm
703
712
Hostinfo : hi ,
704
713
DebugFlags : c .debugFlags ,
705
714
OmitPeers : cb == nil ,
715
+
716
+ // On initial startup before we know our endpoints, set the ReadOnly flag
717
+ // to tell the control server not to distribute out our (empty) endpoints to peers.
718
+ // Presumably we'll learn our endpoints in a half second and do another post
719
+ // with useful results. The first POST just gets us the DERP map which we
720
+ // need to do the STUN queries to discover our endpoints.
721
+ // TODO(bradfitz): we skip this optimization in tests, though,
722
+ // because the e2e tests are currently hyperspecific about the
723
+ // ordering of things. The e2e tests need love.
724
+ ReadOnly : readOnly || (len (epStrs ) == 0 && ! everEndpoints && ! inTest ()),
706
725
}
707
726
var extraDebugFlags []string
708
727
if hi != nil && c .linkMon != nil && ! c .skipIPForwardingCheck &&
@@ -725,17 +744,6 @@ func (c *Direct) sendMapRequest(ctx context.Context, maxPolls int, cb func(*netm
725
744
if c .newDecompressor != nil {
726
745
request .Compress = "zstd"
727
746
}
728
- // On initial startup before we know our endpoints, set the ReadOnly flag
729
- // to tell the control server not to distribute out our (empty) endpoints to peers.
730
- // Presumably we'll learn our endpoints in a half second and do another post
731
- // with useful results. The first POST just gets us the DERP map which we
732
- // need to do the STUN queries to discover our endpoints.
733
- // TODO(bradfitz): we skip this optimization in tests, though,
734
- // because the e2e tests are currently hyperspecific about the
735
- // ordering of things. The e2e tests need love.
736
- if len (epStrs ) == 0 && ! everEndpoints && ! inTest () {
737
- request .ReadOnly = true
738
- }
739
747
740
748
bodyData , err := encode (request , serverKey , serverNoiseKey , machinePrivKey )
741
749
if err != nil {
0 commit comments