controller.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. * Copyright (c) 2015, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. // Package psiphon implements the core tunnel functionality of a Psiphon client.
  20. // The main function is RunForever, which runs a Controller that obtains lists of
  21. // servers, establishes tunnel connections, and runs local proxies through which
  22. // tunneled traffic may be sent.
  23. package psiphon
  24. import (
  25. "errors"
  26. "net"
  27. "sync"
  28. "time"
  29. )
  30. // Controller is a tunnel lifecycle coordinator. It manages lists of servers to
  31. // connect to; establishes and monitors tunnels; and runs local proxies which
  32. // route traffic through the tunnels.
  33. type Controller struct {
  34. config *Config
  35. sessionId string
  36. componentFailureSignal chan struct{}
  37. shutdownBroadcast chan struct{}
  38. runWaitGroup *sync.WaitGroup
  39. establishedTunnels chan *Tunnel
  40. failedTunnels chan *Tunnel
  41. tunnelMutex sync.Mutex
  42. establishedOnce bool
  43. tunnels []*Tunnel
  44. nextTunnel int
  45. startedConnectedReporter bool
  46. isEstablishing bool
  47. establishWaitGroup *sync.WaitGroup
  48. stopEstablishingBroadcast chan struct{}
  49. candidateServerEntries chan *ServerEntry
  50. establishPendingConns *Conns
  51. fetchRemotePendingConns *Conns
  52. }
  53. // NewController initializes a new controller.
  54. func NewController(config *Config) (controller *Controller, err error) {
  55. // Generate a session ID for the Psiphon server API. This session ID is
  56. // used across all tunnels established by the controller.
  57. sessionId, err := MakeSessionId()
  58. if err != nil {
  59. return nil, ContextError(err)
  60. }
  61. return &Controller{
  62. config: config,
  63. sessionId: sessionId,
  64. // componentFailureSignal receives a signal from a component (including socks and
  65. // http local proxies) if they unexpectedly fail. Senders should not block.
  66. // A buffer allows at least one stop signal to be sent before there is a receiver.
  67. componentFailureSignal: make(chan struct{}, 1),
  68. shutdownBroadcast: make(chan struct{}),
  69. runWaitGroup: new(sync.WaitGroup),
  70. // establishedTunnels and failedTunnels buffer sizes are large enough to
  71. // receive full pools of tunnels without blocking. Senders should not block.
  72. establishedTunnels: make(chan *Tunnel, config.TunnelPoolSize),
  73. failedTunnels: make(chan *Tunnel, config.TunnelPoolSize),
  74. tunnels: make([]*Tunnel, 0),
  75. establishedOnce: false,
  76. startedConnectedReporter: false,
  77. isEstablishing: false,
  78. establishPendingConns: new(Conns),
  79. fetchRemotePendingConns: new(Conns),
  80. }, nil
  81. }
  82. // Run executes the controller. It launches components and then monitors
  83. // for a shutdown signal; after receiving the signal it shuts down the
  84. // controller.
  85. // The components include:
  86. // - the periodic remote server list fetcher
  87. // - the connected reporter
  88. // - the tunnel manager
  89. // - a local SOCKS proxy that port forwards through the pool of tunnels
  90. // - a local HTTP proxy that port forwards through the pool of tunnels
  91. func (controller *Controller) Run(shutdownBroadcast <-chan struct{}) {
  92. NoticeCoreVersion(VERSION)
  93. // Start components
  94. socksProxy, err := NewSocksProxy(controller.config, controller)
  95. if err != nil {
  96. NoticeAlert("error initializing local SOCKS proxy: %s", err)
  97. return
  98. }
  99. defer socksProxy.Close()
  100. httpProxy, err := NewHttpProxy(controller.config, controller)
  101. if err != nil {
  102. NoticeAlert("error initializing local HTTP proxy: %s", err)
  103. return
  104. }
  105. defer httpProxy.Close()
  106. if !controller.config.DisableRemoteServerListFetcher {
  107. controller.runWaitGroup.Add(1)
  108. go controller.remoteServerListFetcher()
  109. }
  110. /// Note: the connected reporter isn't started until a tunnel is
  111. // established
  112. controller.runWaitGroup.Add(1)
  113. go controller.runTunnels()
  114. if *controller.config.EstablishTunnelTimeoutSeconds != 0 {
  115. controller.runWaitGroup.Add(1)
  116. go controller.establishTunnelWatcher()
  117. }
  118. // Wait while running
  119. select {
  120. case <-shutdownBroadcast:
  121. NoticeInfo("controller shutdown by request")
  122. case <-controller.componentFailureSignal:
  123. NoticeAlert("controller shutdown due to component failure")
  124. }
  125. close(controller.shutdownBroadcast)
  126. controller.establishPendingConns.CloseAll()
  127. controller.fetchRemotePendingConns.CloseAll()
  128. controller.runWaitGroup.Wait()
  129. NoticeInfo("exiting controller")
  130. }
  131. // SignalComponentFailure notifies the controller that an associated component has failed.
  132. // This will terminate the controller.
  133. func (controller *Controller) SignalComponentFailure() {
  134. select {
  135. case controller.componentFailureSignal <- *new(struct{}):
  136. default:
  137. }
  138. }
  139. // remoteServerListFetcher fetches an out-of-band list of server entries
  140. // for more tunnel candidates. It fetches immediately, retries after failure
  141. // with a wait period, and refetches after success with a longer wait period.
  142. func (controller *Controller) remoteServerListFetcher() {
  143. defer controller.runWaitGroup.Done()
  144. loop:
  145. for {
  146. err := FetchRemoteServerList(
  147. controller.config, controller.fetchRemotePendingConns)
  148. var duration time.Duration
  149. if err != nil {
  150. NoticeAlert("failed to fetch remote server list: %s", err)
  151. duration = FETCH_REMOTE_SERVER_LIST_RETRY_PERIOD
  152. } else {
  153. duration = FETCH_REMOTE_SERVER_LIST_STALE_PERIOD
  154. }
  155. timeout := time.After(duration)
  156. select {
  157. case <-timeout:
  158. // Fetch again
  159. case <-controller.shutdownBroadcast:
  160. break loop
  161. }
  162. }
  163. NoticeInfo("exiting remote server list fetcher")
  164. }
  165. // establishTunnelWatcher terminates the controller if a tunnel
  166. // has not been established in the configured time period. This
  167. // is regardless of how many tunnels are presently active -- meaning
  168. // that if an active tunnel was established and lost the controller
  169. // is left running (to re-establish).
  170. func (controller *Controller) establishTunnelWatcher() {
  171. defer controller.runWaitGroup.Done()
  172. timeout := time.After(
  173. time.Duration(*controller.config.EstablishTunnelTimeoutSeconds) * time.Second)
  174. select {
  175. case <-timeout:
  176. if !controller.hasEstablishedOnce() {
  177. NoticeAlert("failed to establish tunnel before timeout")
  178. controller.SignalComponentFailure()
  179. }
  180. case <-controller.shutdownBroadcast:
  181. }
  182. NoticeInfo("exiting establish tunnel watcher")
  183. }
  184. // connectedReporter sends periodic "connected" requests to the Psiphon API.
  185. // These requests are for server-side unique user stats calculation. See the
  186. // comment in DoConnectedRequest for a description of the request mechanism.
  187. // To ensure we don't over- or under-count unique users, only one connected
  188. // request is made across all simultaneous multi-tunnels; and the connected
  189. // request is repeated periodically.
  190. func (controller *Controller) connectedReporter() {
  191. defer controller.runWaitGroup.Done()
  192. loop:
  193. for {
  194. // Pick any active tunnel and make the next connected request. No error
  195. // is logged if there's no active tunnel, as that's not an unexpected condition.
  196. reported := false
  197. tunnel := controller.getNextActiveTunnel()
  198. if tunnel != nil {
  199. err := tunnel.session.DoConnectedRequest()
  200. if err == nil {
  201. reported = true
  202. } else {
  203. NoticeAlert("failed to make connected request: %s", err)
  204. }
  205. }
  206. // Schedule the next connected request and wait.
  207. var duration time.Duration
  208. if reported {
  209. duration = PSIPHON_API_CONNECTED_REQUEST_PERIOD
  210. } else {
  211. duration = PSIPHON_API_CONNECTED_REQUEST_RETRY_PERIOD
  212. }
  213. timeout := time.After(duration)
  214. select {
  215. case <-timeout:
  216. // Make another connected request
  217. case <-controller.shutdownBroadcast:
  218. break loop
  219. }
  220. }
  221. NoticeInfo("exiting connected reporter")
  222. }
  223. func (controller *Controller) startConnectedReporter() {
  224. if controller.config.DisableApi {
  225. return
  226. }
  227. // Start the connected reporter after the first tunnel is established.
  228. // Concurrency note: only the runTunnels goroutine may access startedConnectedReporter.
  229. if !controller.startedConnectedReporter {
  230. controller.startedConnectedReporter = true
  231. controller.runWaitGroup.Add(1)
  232. go controller.connectedReporter()
  233. }
  234. }
  235. // runTunnels is the controller tunnel management main loop. It starts and stops
  236. // establishing tunnels based on the target tunnel pool size and the current size
  237. // of the pool. Tunnels are established asynchronously using worker goroutines.
  238. //
  239. // When there are no server entries for the target region/protocol, the
  240. // establishCandidateGenerator will yield no candidates and wait before
  241. // trying again. In the meantime, a remote server entry fetch may supply
  242. // valid candidates.
  243. //
  244. // When a tunnel is established, it's added to the active pool. The tunnel's
  245. // operateTunnel goroutine monitors the tunnel.
  246. //
  247. // When a tunnel fails, it's removed from the pool and the establish process is
  248. // restarted to fill the pool.
  249. func (controller *Controller) runTunnels() {
  250. defer controller.runWaitGroup.Done()
  251. // Start running
  252. controller.startEstablishing()
  253. loop:
  254. for {
  255. select {
  256. case failedTunnel := <-controller.failedTunnels:
  257. NoticeAlert("tunnel failed: %s", failedTunnel.serverEntry.IpAddress)
  258. controller.terminateTunnel(failedTunnel)
  259. // Concurrency note: only this goroutine may call startEstablishing/stopEstablishing
  260. // and access isEstablishing.
  261. if !controller.isEstablishing {
  262. controller.startEstablishing()
  263. }
  264. // !TODO! design issue: might not be enough server entries with region/caps to ever fill tunnel slots
  265. // solution(?) target MIN(CountServerEntries(region, protocol), TunnelPoolSize)
  266. case establishedTunnel := <-controller.establishedTunnels:
  267. if controller.registerTunnel(establishedTunnel) {
  268. NoticeActiveTunnel(establishedTunnel.serverEntry.IpAddress)
  269. } else {
  270. controller.discardTunnel(establishedTunnel)
  271. }
  272. if controller.isFullyEstablished() {
  273. controller.stopEstablishing()
  274. }
  275. controller.startConnectedReporter()
  276. case <-controller.shutdownBroadcast:
  277. break loop
  278. }
  279. }
  280. // Stop running
  281. controller.stopEstablishing()
  282. controller.terminateAllTunnels()
  283. // Drain tunnel channels
  284. close(controller.establishedTunnels)
  285. for tunnel := range controller.establishedTunnels {
  286. controller.discardTunnel(tunnel)
  287. }
  288. close(controller.failedTunnels)
  289. for tunnel := range controller.failedTunnels {
  290. controller.discardTunnel(tunnel)
  291. }
  292. NoticeInfo("exiting run tunnels")
  293. }
  294. // SignalTunnelFailure implements the TunnelOwner interface. This function
  295. // is called by Tunnel.operateTunnel when the tunnel has detected that it
  296. // has failed. The Controller will signal runTunnels to create a new
  297. // tunnel and/or remove the tunnel from the list of active tunnels.
  298. func (controller *Controller) SignalTunnelFailure(tunnel *Tunnel) {
  299. // Don't block. Assumes the receiver has a buffer large enough for
  300. // the typical number of operated tunnels. In case there's no room,
  301. // terminate the tunnel (runTunnels won't get a signal in this case,
  302. // but the tunnel will be removed from the list of active tunnels).
  303. select {
  304. case controller.failedTunnels <- tunnel:
  305. default:
  306. controller.terminateTunnel(tunnel)
  307. }
  308. }
  309. // discardTunnel disposes of a successful connection that is no longer required.
  310. func (controller *Controller) discardTunnel(tunnel *Tunnel) {
  311. NoticeInfo("discard tunnel: %s", tunnel.serverEntry.IpAddress)
  312. // TODO: not calling PromoteServerEntry, since that would rank the
  313. // discarded tunnel before fully active tunnels. Can a discarded tunnel
  314. // be promoted (since it connects), but with lower rank than all active
  315. // tunnels?
  316. tunnel.Close()
  317. }
  318. // registerTunnel adds the connected tunnel to the pool of active tunnels
  319. // which are candidates for port forwarding. Returns true if the pool has an
  320. // empty slot and false if the pool is full (caller should discard the tunnel).
  321. func (controller *Controller) registerTunnel(tunnel *Tunnel) bool {
  322. controller.tunnelMutex.Lock()
  323. defer controller.tunnelMutex.Unlock()
  324. if len(controller.tunnels) >= controller.config.TunnelPoolSize {
  325. return false
  326. }
  327. // Perform a final check just in case we've established
  328. // a duplicate connection.
  329. for _, activeTunnel := range controller.tunnels {
  330. if activeTunnel.serverEntry.IpAddress == tunnel.serverEntry.IpAddress {
  331. NoticeAlert("duplicate tunnel: %s", tunnel.serverEntry.IpAddress)
  332. return false
  333. }
  334. }
  335. controller.establishedOnce = true
  336. controller.tunnels = append(controller.tunnels, tunnel)
  337. NoticeTunnels(len(controller.tunnels))
  338. return true
  339. }
  340. // hasEstablishedOnce indicates if at least one active tunnel has
  341. // been established up to this point. This is regardeless of how many
  342. // tunnels are presently active.
  343. func (controller *Controller) hasEstablishedOnce() bool {
  344. controller.tunnelMutex.Lock()
  345. defer controller.tunnelMutex.Unlock()
  346. return controller.establishedOnce
  347. }
  348. // isFullyEstablished indicates if the pool of active tunnels is full.
  349. func (controller *Controller) isFullyEstablished() bool {
  350. controller.tunnelMutex.Lock()
  351. defer controller.tunnelMutex.Unlock()
  352. return len(controller.tunnels) >= controller.config.TunnelPoolSize
  353. }
  354. // terminateTunnel removes a tunnel from the pool of active tunnels
  355. // and closes the tunnel. The next-tunnel state used by getNextActiveTunnel
  356. // is adjusted as required.
  357. func (controller *Controller) terminateTunnel(tunnel *Tunnel) {
  358. controller.tunnelMutex.Lock()
  359. defer controller.tunnelMutex.Unlock()
  360. for index, activeTunnel := range controller.tunnels {
  361. if tunnel == activeTunnel {
  362. controller.tunnels = append(
  363. controller.tunnels[:index], controller.tunnels[index+1:]...)
  364. if controller.nextTunnel > index {
  365. controller.nextTunnel--
  366. }
  367. if controller.nextTunnel >= len(controller.tunnels) {
  368. controller.nextTunnel = 0
  369. }
  370. activeTunnel.Close()
  371. NoticeTunnels(len(controller.tunnels))
  372. break
  373. }
  374. }
  375. }
  376. // terminateAllTunnels empties the tunnel pool, closing all active tunnels.
  377. // This is used when shutting down the controller.
  378. func (controller *Controller) terminateAllTunnels() {
  379. controller.tunnelMutex.Lock()
  380. defer controller.tunnelMutex.Unlock()
  381. for _, activeTunnel := range controller.tunnels {
  382. activeTunnel.Close()
  383. }
  384. controller.tunnels = make([]*Tunnel, 0)
  385. controller.nextTunnel = 0
  386. NoticeTunnels(len(controller.tunnels))
  387. }
  388. // getNextActiveTunnel returns the next tunnel from the pool of active
  389. // tunnels. Currently, tunnel selection order is simple round-robin.
  390. func (controller *Controller) getNextActiveTunnel() (tunnel *Tunnel) {
  391. controller.tunnelMutex.Lock()
  392. defer controller.tunnelMutex.Unlock()
  393. for i := len(controller.tunnels); i > 0; i-- {
  394. tunnel = controller.tunnels[controller.nextTunnel]
  395. controller.nextTunnel =
  396. (controller.nextTunnel + 1) % len(controller.tunnels)
  397. return tunnel
  398. }
  399. return nil
  400. }
  401. // isActiveTunnelServerEntries is used to check if there's already
  402. // an existing tunnel to a candidate server.
  403. func (controller *Controller) isActiveTunnelServerEntry(serverEntry *ServerEntry) bool {
  404. controller.tunnelMutex.Lock()
  405. defer controller.tunnelMutex.Unlock()
  406. for _, activeTunnel := range controller.tunnels {
  407. if activeTunnel.serverEntry.IpAddress == serverEntry.IpAddress {
  408. return true
  409. }
  410. }
  411. return false
  412. }
  413. // Dial selects an active tunnel and establishes a port forward
  414. // connection through the selected tunnel. Failure to connect is considered
  415. // a port foward failure, for the purpose of monitoring tunnel health.
  416. func (controller *Controller) Dial(remoteAddr string) (conn net.Conn, err error) {
  417. tunnel := controller.getNextActiveTunnel()
  418. if tunnel == nil {
  419. return nil, ContextError(errors.New("no active tunnels"))
  420. }
  421. tunneledConn, err := tunnel.Dial(remoteAddr)
  422. if err != nil {
  423. return nil, ContextError(err)
  424. }
  425. return tunneledConn, nil
  426. }
  427. // startEstablishing creates a pool of worker goroutines which will
  428. // attempt to establish tunnels to candidate servers. The candidates
  429. // are generated by another goroutine.
  430. func (controller *Controller) startEstablishing() {
  431. if controller.isEstablishing {
  432. return
  433. }
  434. NoticeInfo("start establishing")
  435. controller.isEstablishing = true
  436. controller.establishWaitGroup = new(sync.WaitGroup)
  437. controller.stopEstablishingBroadcast = make(chan struct{})
  438. controller.candidateServerEntries = make(chan *ServerEntry)
  439. controller.establishPendingConns.Reset()
  440. for i := 0; i < controller.config.ConnectionWorkerPoolSize; i++ {
  441. controller.establishWaitGroup.Add(1)
  442. go controller.establishTunnelWorker()
  443. }
  444. controller.establishWaitGroup.Add(1)
  445. go controller.establishCandidateGenerator()
  446. }
  447. // stopEstablishing signals the establish goroutines to stop and waits
  448. // for the group to halt. pendingConns is used to interrupt any worker
  449. // blocked on a socket connect.
  450. func (controller *Controller) stopEstablishing() {
  451. if !controller.isEstablishing {
  452. return
  453. }
  454. NoticeInfo("stop establishing")
  455. close(controller.stopEstablishingBroadcast)
  456. // Note: on Windows, interruptibleTCPClose doesn't really interrupt socket connects
  457. // and may leave goroutines running for a time after the Wait call.
  458. controller.establishPendingConns.CloseAll()
  459. // Note: establishCandidateGenerator closes controller.candidateServerEntries
  460. // (as it may be sending to that channel).
  461. controller.establishWaitGroup.Wait()
  462. controller.isEstablishing = false
  463. controller.establishWaitGroup = nil
  464. controller.stopEstablishingBroadcast = nil
  465. controller.candidateServerEntries = nil
  466. }
  467. // establishCandidateGenerator populates the candidate queue with server entries
  468. // from the data store. Server entries are iterated in rank order, so that promoted
  469. // servers with higher rank are priority candidates.
  470. func (controller *Controller) establishCandidateGenerator() {
  471. defer controller.establishWaitGroup.Done()
  472. defer close(controller.candidateServerEntries)
  473. iterator, err := NewServerEntryIterator(controller.config)
  474. if err != nil {
  475. NoticeAlert("failed to iterate over candidates: %s", err)
  476. controller.SignalComponentFailure()
  477. return
  478. }
  479. defer iterator.Close()
  480. loop:
  481. // Repeat until stopped
  482. for {
  483. // Yield each server entry returned by the iterator
  484. for {
  485. serverEntry, err := iterator.Next()
  486. if err != nil {
  487. NoticeAlert("failed to get next candidate: %s", err)
  488. controller.SignalComponentFailure()
  489. break loop
  490. }
  491. if serverEntry == nil {
  492. // Completed this iteration
  493. break
  494. }
  495. select {
  496. case controller.candidateServerEntries <- serverEntry:
  497. case <-controller.stopEstablishingBroadcast:
  498. break loop
  499. case <-controller.shutdownBroadcast:
  500. break loop
  501. }
  502. }
  503. iterator.Reset()
  504. // After a complete iteration of candidate servers, pause before iterating again.
  505. // This helps avoid some busy wait loop conditions, and also allows some time for
  506. // network conditions to change.
  507. timeout := time.After(ESTABLISH_TUNNEL_PAUSE_PERIOD)
  508. select {
  509. case <-timeout:
  510. // Retry iterating
  511. case <-controller.stopEstablishingBroadcast:
  512. break loop
  513. case <-controller.shutdownBroadcast:
  514. break loop
  515. }
  516. }
  517. NoticeInfo("stopped candidate generator")
  518. }
  519. // establishTunnelWorker pulls candidates from the candidate queue, establishes
  520. // a connection to the tunnel server, and delivers the established tunnel to a channel.
  521. func (controller *Controller) establishTunnelWorker() {
  522. defer controller.establishWaitGroup.Done()
  523. loop:
  524. for serverEntry := range controller.candidateServerEntries {
  525. // Note: don't receive from candidateServerEntries and stopEstablishingBroadcast
  526. // in the same select, since we want to prioritize receiving the stop signal
  527. if controller.isStopEstablishingBroadcast() {
  528. break loop
  529. }
  530. // There may already be a tunnel to this candidate. If so, skip it.
  531. if controller.isActiveTunnelServerEntry(serverEntry) {
  532. continue
  533. }
  534. tunnel, err := EstablishTunnel(
  535. controller.config,
  536. controller.sessionId,
  537. controller.establishPendingConns,
  538. serverEntry,
  539. controller) // TunnelOwner
  540. if err != nil {
  541. // Before emitting error, check if establish interrupted, in which
  542. // case the error is noise.
  543. if controller.isStopEstablishingBroadcast() {
  544. break loop
  545. }
  546. NoticeInfo("failed to connect to %s: %s", serverEntry.IpAddress, err)
  547. continue
  548. }
  549. // Deliver established tunnel.
  550. // Don't block. Assumes the receiver has a buffer large enough for
  551. // the number of desired tunnels. If there's no room, the tunnel must
  552. // not be required so it's discarded.
  553. select {
  554. case controller.establishedTunnels <- tunnel:
  555. default:
  556. controller.discardTunnel(tunnel)
  557. }
  558. }
  559. NoticeInfo("stopped establish worker")
  560. }
  561. func (controller *Controller) isStopEstablishingBroadcast() bool {
  562. select {
  563. case <-controller.stopEstablishingBroadcast:
  564. return true
  565. default:
  566. }
  567. return false
  568. }