风险提示:理性看待区块链,提高风险意识!
golang-event 在以太坊中的使用
首页 > 币界资讯 > 区块链知识 2018-03-18 08:13:00

go-ethereum中go-event库的使用

github.com/ethereum/go-ethereum/event 包实现了一个事件发布订阅的库,使用接口主要是 event.Feed 类型,以前还有 event.TypeMux 类型,看代码注释,说过时了,目前主要使用 Feed 类型。

package main
 import (
 "fmt"
 "sync"
 "github.com/ethereum/go-ethereum/event"
 )
 func main() {
 type someEvent struct{ I int }
 var feed event.Feed
 var wg sync.WaitGroup
 ch := make(chan someEvent)
 sub := feed.Subscribe(ch)
 wg.Add(1)
 go func() {
 defer wg.Done()
 for event := range ch {
 fmt.Printf("Received: %#v\n", event.I)
 }
 sub.Unsubscribe()
 fmt.Println("done")
 }()
 feed.Send(someEvent{5})
 feed.Send(someEvent{10})
 feed.Send(someEvent{7})
 feed.Send(someEvent{14})
 close(ch)
 wg.Wait()
 }

通过调用 event.Feed 类型的Subscrible方法订阅事件通知,需要使用者提前指定接收事件的 channel,Subscribe 返回 Subscription 对象,是一个接口类型:

type Subscription interface {
 Err() // returns the error channel
 Unsubscribe()
 // cancels sending of events, closing the error channel
 }

Err() 返回获取error 的channel,调用Unsubscribe()取消事件订阅。事件的发布者调用 Send() 方法,发送事件。

可以使用同一个channel实例,多次调用Feed 的Subscrible()方法:

package main
 import (
 "fmt"
 "sync"
 "github.com/ethereum/go-ethereum/event"
 )
 func main() {
 var (
 feed event.Feed
 recv sync.WaitGroup
 sender sync.WaitGroup
 )
 ch := make(chan int)
 feed.Subscribe(ch)
 feed.Subscribe(ch)
 feed.Subscribe(ch)
 expectSends := func(value, n int) {
 defer sender.Done()
 if nsent := feed.Send(value); nsent != n {
 fmt.Printf("send delivered %d times, want %d\n", nsent, n)
 }
 }
 expectRecv := func(wantValue, n int) {
 defer recv.Done()
 for v := range ch {
 if v != wantValue {
 fmt.Printf("received %d, want %d\n", v, wantValue)
 } else {
 fmt.Printf("recv v = %d\n", v)
 }
 }
 }
 sender.Add(3)
 for i := 0; i < 3; i++ {
 go expectSends(1, 3)
 }
 go func() {
 sender.Wait()
 close(ch)
 }()
 recv.Add(1)
 go expectRecv(1, 3)
 recv.Wait()
 }

这个例子中, 有三个订阅者, 有三个发送者, 每个发送者发送三次1, 同一个channel ch 里面被推送了9个1。

ethereum event 库还提供了一些高级别的方便接口, 比如event.NewSubscription函数,接收一个函数类型,作为数据的生产者, producer本身在后台一个单独的goroutine内执行, 后台goroutine往用户的channel 发送数据:

package main
 import (
 "fmt"
 "github.com/ethereum/go-ethereum/event"
 )
 func main() {
 ch := make(chan int)
 sub := event.NewSubscription(
 func(quit for i := 0; i < 10; i++ {
 select {
 case ch case <-quit:
 fmt.Println("unsubscribed")
 return nil
 }
 }
 return nil
 })
 for i := range ch {
 fmt.Println(i)
 if i == 4 {
 sub.Unsubscribe()
 break
 }
 }
 }

库也提供了 event.SubscriptionScope 类型用于追踪多个订阅者,提供集中的取消订阅功能:

package main
 import (
 "fmt"
 "sync"
 "github.com/ethereum/go-ethereum/event"
 )
 // This example demonstrates how SubscriptionScope can be used to control the lifetime of
 // subscriptions.
 // Our example program consists of two servers, each of which performs a calculation when
 // requested. The servers also allow subscribing to results of all computations.
 type divServer struct{ results event.Feed }
 type mulServer struct{ results event.Feed }
 func (s *divServer) do(a, b int) int {
 r := a / b
 s.results.Send(r)
 return r
 }
 func (s *mulServer) do(a, b int) int {
 r := a * b
 s.results.Send(r)
 return r
 }
 // The servers are contained in an App. The app controls the servers and exposes them
 // through its API.
 type App struct {
 divServer
 mulServer
 scope event.SubscriptionScope
 }
 func (s *App) Calc(op byte, a, b int) int {
 switch op {
 case '/':
 return s.divServer.do(a, b)
 case '*':
 return s.mulServer.do(a, b)
 default:
 panic("invalid op")
 }
 }
 // The app's SubscribeResults method starts sending calculation results to the given
 // channel. Subscriptions created through this method are tied to the lifetime of the App
 // because they are registered in the scope.
 func (s *App) SubscribeResults(op byte, ch chan switch op {
 case '/':
 return s.scope.Track(s.divServer.results.Subscribe(ch))
 case '*':
 return s.scope.Track(s.mulServer.results.Subscribe(ch))
 default:
 panic("invalid op")
 }
 }
 // Stop stops the App, closing all subscriptions created through SubscribeResults.
 func (s *App) Stop() {
 s.scope.Close()
 }
 func main() {
 var (
 app App
 wg sync.WaitGroup
 divs = make(chan int)
 muls = make(chan int)
 )
 divsub := app.SubscribeResults('/', divs)
 mulsub := app.SubscribeResults('*', muls)
 wg.Add(1)
 go func() {
 defer wg.Done()
 defer fmt.Println("subscriber exited")
 for {
 select {
 case result := <-divs:
 fmt.Println("division happened:", result)
 case result := <-muls:
 fmt.Println("multiplication happened:", result)
 case divErr := <-divsub.Err():
 fmt.Println("divsub.Err() :", divErr)
 return
 case mulErr := <-mulsub.Err():
 fmt.Println("mulsub.Err() :", mulErr)
 return
 }
 }
 }()
 app.Calc('/', 22, 11)
 app.Calc('*', 3, 4)
 app.Stop()
 wg.Wait()
 }

SubscriptionScope的Close() 方法接收 Track 方法的返回值 ,Track 方法负责追踪订阅者。

上一篇: 比特币源码分析-网络(一)
下一篇: block header概念解析
推荐专栏
web3首席知识博主
一位相信价值投资的币圈KOL。稳定盈利的缠论野生交易员 #BTC行情分析师 #价值投资 #链上数据分析
爱Web 3,爱生活,爱科技,爱炒币的老韭菜
热门币种
更多
币种
价格
24H涨跌幅
BTC比特币
¥264,723.74
37,091.22 USDT
+0.1%
ETH以太坊
¥14,416.22
2,019.90 USDT
-0.12%
USDT泰达币
¥7.20
1.01 USDT
0%
BNB币安币
¥1,625.40
227.74 USDT
+0.36%
XRP瑞波币
¥4.32
0.60460 USDT
+0.37%
USDC
¥7.14
1.00 USDT
+0.03%
SOLSolana
¥398.85
55.89 USDT
+1.54%
OKBOK币
¥398.61
55.85 USDT
-1.64%
ADA艾达币
¥2.68
0.37580 USDT
-1.16%
DOGE狗狗币
¥0.55160
0.07730 USDT
-1.52%
热搜币种
更多
币种
价格
24H涨跌幅
Terra Classic
¥0.00
9.402E-5 USDT
-18.95%
Gala
¥0.18
0.025374 USDT
-4.66%
dYdX
¥22.58
3.1918 USDT
-0.91%
比特股
¥0.05
0.006964 USDT
+4.28%
PancakeSwap
¥15.52
2.1936 USDT
-2.74%
Conflux
¥1.08
0.1524 USDT
-2.87%
Filecoin
¥31.45
4.4454 USDT
-0.69%
FTX Token
¥29.82
4.2155 USDT
+16.96%
Yield Guild Games
¥2.55
0.3608 USDT
-0.52%
Shiba Inu
¥0.00
8.14E-6 USDT
-2.51%
比特币
¥262,381.44
37091.22 USDT
+0.1%
比原链
¥0.07
0.010011 USDT
-4.38%
最新快讯
更多
汇丰、恒生、渣打、富邦华一四家外资银行入围首批“数字人民币”业务试点名单
2023-11-28 19:06:57
摩根大通和Apollo计划建立代币化“企业主网”
2023-11-28 19:03:57
Nansen2公测版本上线,新增链上数据异动、智能搜索等功能
2023-11-28 18:59:52
西班牙公民需在明年3月底前申报其海外平台上加密货币持仓
2023-11-28 18:53:43
Nansen2已公开测试
2023-11-28 18:53:38
dYdX基金会:主网启动以来超过1645万DYDX被质押
2023-11-28 18:52:07
NicCarter等比特币倡导者发文:比特币挖矿是清洁能源和平衡电网的关键工具
2023-11-28 18:47:58
下载币界网APP