|
44 | 44 | ) |
45 | 45 |
|
46 | 46 | type Client struct { |
47 | | - rpcClientMap *Map |
48 | | - subscribeNewHeadWg sync.WaitGroup |
| 47 | + rpcClientMap *Map |
49 | 48 | } |
50 | 49 |
|
51 | 50 | func New(ctx context.Context, opts ...Option) (*Client, error) { |
@@ -611,23 +610,25 @@ func (mc *Client) SubscribeNewHead(ctx context.Context, ch chan<- *Header) (ethe |
611 | 610 | return nil, ErrNoEthClient |
612 | 611 | } |
613 | 612 |
|
| 613 | + var subscribeNewHeadWg sync.WaitGroup |
| 614 | + |
614 | 615 | cctx, cancel := context.WithCancel(ctx) |
615 | 616 | for url := range clientsMap { |
616 | | - go mc.subscribeNewHead(cctx, url, ch) |
| 617 | + subscribeNewHeadWg.Add(1) |
| 618 | + go mc.subscribeNewHead(cctx, &subscribeNewHeadWg, url, ch) |
617 | 619 | } |
618 | 620 |
|
619 | 621 | // TODO: handle new clients comes |
620 | 622 | return event.NewSubscription(func(unsub <-chan struct{}) error { |
621 | 623 | <-unsub |
622 | 624 | cancel() |
623 | | - mc.subscribeNewHeadWg.Wait() |
| 625 | + subscribeNewHeadWg.Wait() |
624 | 626 | return nil |
625 | 627 | }), nil |
626 | 628 | } |
627 | 629 |
|
628 | | -func (mc *Client) subscribeNewHead(ctx context.Context, url string, ch chan<- *Header) error { |
629 | | - mc.subscribeNewHeadWg.Add(1) |
630 | | - defer mc.subscribeNewHeadWg.Done() |
| 630 | +func (mc *Client) subscribeNewHead(ctx context.Context, wg *sync.WaitGroup, url string, ch chan<- *Header) error { |
| 631 | + defer wg.Done() |
631 | 632 |
|
632 | 633 | for { |
633 | 634 | rc := mc.rpcClientMap.Get(url) |
|
0 commit comments