-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmultisub.go
48 lines (40 loc) · 1.18 KB
/
multisub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Example for multiple subscribers
package main
import (
"context"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/flashbots/go-utils/blocksub"
)
func DemoMultiSub(httpURI, wsURI string) {
// Create and start a BlockSub instance
blocksub := blocksub.NewBlockSub(context.Background(), httpURI, wsURI)
blocksub.DebugOutput = true
if err := blocksub.Start(); err != nil {
log.Crit(err.Error())
}
// Create two regular subscriptions
go listen(1, blocksub.Subscribe(context.Background()))
go listen(2, blocksub.Subscribe(context.Background()))
// Create a third subscription, which will be cancelled after 10 seconds
ctx, cancel := context.WithCancel(context.Background())
go listen(3, blocksub.Subscribe(ctx))
time.Sleep(10 * time.Second)
cancel()
// // Wait 10 seconds and then stop the blocksub
// time.Sleep(10 * time.Second)
// blocksub.Stop()
// Sleep forever
select {}
}
func listen(id int, subscription blocksub.Subscription) {
for {
select {
case <-subscription.Done():
log.Info("sub finished", "id", id)
return
case header := <-subscription.C:
log.Info("new header", "id", id, "number", header.Number.Uint64(), "hash", header.Hash().Hex())
}
}
}