example_scope_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package event_test
  17. import (
  18. "fmt"
  19. "sync"
  20. "github.com/ethereum/go-ethereum/event"
  21. )
  22. // This example demonstrates how SubscriptionScope can be used to control the lifetime of
  23. // subscriptions.
  24. //
  25. // Our example program consists of two servers, each of which performs a calculation when
  26. // requested. The servers also allow subscribing to results of all computations.
  27. type divServer struct{ results event.Feed }
  28. type mulServer struct{ results event.Feed }
  29. func (s *divServer) do(a, b int) int {
  30. r := a / b
  31. s.results.Send(r)
  32. return r
  33. }
  34. func (s *mulServer) do(a, b int) int {
  35. r := a * b
  36. s.results.Send(r)
  37. return r
  38. }
  39. // The servers are contained in an App. The app controls the servers and exposes them
  40. // through its API.
  41. type App struct {
  42. divServer
  43. mulServer
  44. scope event.SubscriptionScope
  45. }
  46. func (s *App) Calc(op byte, a, b int) int {
  47. switch op {
  48. case '/':
  49. return s.divServer.do(a, b)
  50. case '*':
  51. return s.mulServer.do(a, b)
  52. default:
  53. panic("invalid op")
  54. }
  55. }
  56. // The app's SubscribeResults method starts sending calculation results to the given
  57. // channel. Subscriptions created through this method are tied to the lifetime of the App
  58. // because they are registered in the scope.
  59. func (s *App) SubscribeResults(op byte, ch chan<- int) event.Subscription {
  60. switch op {
  61. case '/':
  62. return s.scope.Track(s.divServer.results.Subscribe(ch))
  63. case '*':
  64. return s.scope.Track(s.mulServer.results.Subscribe(ch))
  65. default:
  66. panic("invalid op")
  67. }
  68. }
  69. // Stop stops the App, closing all subscriptions created through SubscribeResults.
  70. func (s *App) Stop() {
  71. s.scope.Close()
  72. }
  73. func ExampleSubscriptionScope() {
  74. // Create the app.
  75. var (
  76. app App
  77. wg sync.WaitGroup
  78. divs = make(chan int)
  79. muls = make(chan int)
  80. )
  81. // Run a subscriber in the background.
  82. divsub := app.SubscribeResults('/', divs)
  83. mulsub := app.SubscribeResults('*', muls)
  84. wg.Add(1)
  85. go func() {
  86. defer wg.Done()
  87. defer fmt.Println("subscriber exited")
  88. defer divsub.Unsubscribe()
  89. defer mulsub.Unsubscribe()
  90. for {
  91. select {
  92. case result := <-divs:
  93. fmt.Println("division happened:", result)
  94. case result := <-muls:
  95. fmt.Println("multiplication happened:", result)
  96. case <-divsub.Err():
  97. return
  98. case <-mulsub.Err():
  99. return
  100. }
  101. }
  102. }()
  103. // Interact with the app.
  104. app.Calc('/', 22, 11)
  105. app.Calc('*', 3, 4)
  106. // Stop the app. This shuts down the subscriptions, causing the subscriber to exit.
  107. app.Stop()
  108. wg.Wait()
  109. // Output:
  110. // division happened: 2
  111. // multiplication happened: 12
  112. // subscriber exited
  113. }