Procházet zdrojové kódy

Add comments on potential race

Miro před 1 rokem
rodič
revize
58beeca990
1 změnil soubory, kde provedl 12 přidání a 0 odebrání
  1. 12 0
      psiphon/server/discovery/classic.go

+ 12 - 0
psiphon/server/discovery/classic.go

@@ -83,6 +83,12 @@ func (c *classicDiscovery) selectServers(clientIP net.IP) []*psinet.DiscoverySer
 // discoverServers selects new encoded server entries to be "discovered" by
 // discoverServers selects new encoded server entries to be "discovered" by
 // the client, using the discoveryValue -- a function of the client's IP
 // the client, using the discoveryValue -- a function of the client's IP
 // address -- as the input into the discovery algorithm.
 // address -- as the input into the discovery algorithm.
+//
+// Warning: if discoverServers is called as the set of discoverable servers
+// changes, i.e. a new server becomes un/discoverable, then there's a remote
+// possibility that discoverServers returns nil because of a race between
+// the timer that updates c.buckets firing and discoverServers obtaining a
+// reference to the value of c.buckets.
 func (c *classicDiscovery) discoverServers(discoveryValue int) []*psinet.DiscoveryServer {
 func (c *classicDiscovery) discoverServers(discoveryValue int) []*psinet.DiscoveryServer {
 
 
 	discoveryDate := c.clk.Now().UTC()
 	discoveryDate := c.clk.Now().UTC()
@@ -96,6 +102,7 @@ func (c *classicDiscovery) discoverServers(discoveryValue int) []*psinet.Discove
 	}
 	}
 
 
 	timeInSeconds := int(discoveryDate.Unix())
 	timeInSeconds := int(discoveryDate.Unix())
+	// TODO: ensure that each server in buckets is discoverable on discoveryDate.
 	servers := selectServers(buckets, timeInSeconds, discoveryValue, discoveryDate)
 	servers := selectServers(buckets, timeInSeconds, discoveryValue, discoveryDate)
 
 
 	return servers
 	return servers
@@ -116,6 +123,11 @@ func (c *classicDiscovery) discoverServers(discoveryValue int) []*psinet.Discove
 // both aspects determine which server is selected. IP address is given the
 // both aspects determine which server is selected. IP address is given the
 // priority: if there are only a couple of servers, for example, IP address alone
 // priority: if there are only a couple of servers, for example, IP address alone
 // determines the outcome.
 // determines the outcome.
+//
+// Warning: If discoveryDate does not fall within the discovery date range of the
+// selected server, then nil will be returned. For this reason, an attempt should
+// be made to ensure that buckets only contains discovery servers that are
+// discoverable on discoveryDate.
 func selectServers(
 func selectServers(
 	buckets [][]*psinet.DiscoveryServer,
 	buckets [][]*psinet.DiscoveryServer,
 	timeInSeconds,
 	timeInSeconds,