Skip to content

Commit b260752

Browse files
author
markya0616
committed
multiclient: update based on PR comments
1 parent 413c2e1 commit b260752

2 files changed

Lines changed: 36 additions & 39 deletions

File tree

multiclient/client.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ func (mc *Client) SubscribeNewHead(ctx context.Context, ch chan<- *Header) (ethe
634634
}
635635

636636
newClientCh := mc.pubSub.Sub(newAvailableClientTopic)
637-
// handle new clients comes
637+
// handle new clients come
638638
go func() {
639639
defer mc.pubSub.Unsub(newClientCh, newAvailableClientTopic)
640640
for {
@@ -661,37 +661,34 @@ func (mc *Client) subscribeNewHead(ctx context.Context, wg *sync.WaitGroup, id u
661661
logger := log.New("id", id)
662662
defer wg.Done()
663663

664+
retryTimer := time.NewTimer(0)
665+
defer retryTimer.Stop()
666+
664667
for {
665-
rc, existed := mc.rpcClientMap.GetById(id)
666-
if !existed {
668+
url, rc := mc.rpcClientMap.GetById(id)
669+
if rc == nil {
667670
logger.Trace("EthClient has been removed")
668671
return nil
669672
}
670-
if rc == nil {
671-
logger.Warn("The client is not ready, wait for a while")
672-
} else {
673-
// If we have error, we need to retry
674-
err := doSubscribe(ctx, id, rc, ch)
675-
if err == nil {
676-
return nil
677-
}
673+
subLogger := logger.New("url", url)
674+
// If we have error, we need to retry
675+
err := doSubscribe(ctx, subLogger, rc, ch)
676+
if err == nil {
677+
return nil
678678
}
679679

680-
// retry subscribe after retryPeriod
681-
t := time.NewTicker(retryPeriod)
680+
// reset timer with retryPeriod
681+
retryTimer.Reset(retryPeriod)
682682
select {
683-
case <-t.C:
683+
case <-retryTimer.C:
684684
case <-ctx.Done():
685-
t.Stop()
686685
return nil
687686
}
688-
t.Stop()
689-
logger.Trace("Retry to subscribe new head")
687+
subLogger.Trace("Retry to subscribe new head")
690688
}
691689
}
692690

693-
func doSubscribe(ctx context.Context, id uint64, rc *rpc.Client, ch chan<- *Header) error {
694-
logger := log.New("id", id)
691+
func doSubscribe(ctx context.Context, logger log.Logger, rc *rpc.Client, ch chan<- *Header) error {
695692
headerCh := make(chan *types.Header)
696693
c := ethclient.NewClient(rc)
697694
subCtx, cancel := context.WithCancel(ctx)

multiclient/map.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ type Map struct {
2727
// the mapping form url to client
2828
clientMap map[string]*client
2929
// the mapping subscription id to url
30-
idMap map[uint64]string
31-
subscripionCounter uint64
32-
newClientCh chan<- string
30+
idMap map[uint64]string
31+
idCounter uint64
32+
newClientCh chan<- string
3333

3434
lock sync.RWMutex
3535
}
@@ -41,10 +41,10 @@ type client struct {
4141

4242
func NewMap(newClientCh chan<- string) *Map {
4343
return &Map{
44-
clientMap: make(map[string]*client),
45-
idMap: make(map[uint64]string),
46-
subscripionCounter: 0,
47-
newClientCh: newClientCh,
44+
clientMap: make(map[string]*client),
45+
idMap: make(map[uint64]string),
46+
idCounter: 0,
47+
newClientCh: newClientCh,
4848
}
4949
}
5050

@@ -68,20 +68,20 @@ func (m *Map) Add(key string, value *rpc.Client) {
6868
m.lock.Lock()
6969
defer m.lock.Unlock()
7070

71-
m.subscripionCounter++
71+
m.idCounter++
7272
m.clientMap[key] = &client{
73-
Id: m.subscripionCounter,
73+
Id: m.idCounter,
7474
Client: value,
7575
}
76-
m.idMap[m.subscripionCounter] = key
76+
m.idMap[m.idCounter] = key
7777

7878
if m.newClientCh != nil {
7979
select {
8080
case m.newClientCh <- key:
8181
default:
8282
}
8383
}
84-
log.Trace("Eth client added", "id", m.subscripionCounter, "url", key)
84+
log.Trace("Eth client added", "id", m.idCounter, "url", key)
8585
}
8686

8787
func (m *Map) Replace(key string, value *rpc.Client) uint64 {
@@ -102,19 +102,19 @@ func (m *Map) Get(key string) *rpc.Client {
102102
return m.clientMap[key].Client
103103
}
104104

105-
func (m *Map) GetById(id uint64) (*rpc.Client, bool) {
105+
func (m *Map) GetById(id uint64) (string, *rpc.Client) {
106106
m.lock.RLock()
107107
defer m.lock.RUnlock()
108108

109109
key, ok := m.idMap[id]
110110
if !ok {
111-
return nil, false
111+
return "", nil
112112
}
113113
c, ok := m.clientMap[key]
114114
if !ok {
115-
return nil, false
115+
return "", nil
116116
}
117-
return c.Client, true
117+
return key, c.Client
118118
}
119119

120120
func (m *Map) Len() int {
@@ -156,11 +156,11 @@ func (m *Map) Ids() []uint64 {
156156
m.lock.RLock()
157157
defer m.lock.RUnlock()
158158

159-
ids := make([]uint64, len(m.idMap))
160-
i := 0
161-
for k := range m.idMap {
162-
ids[i] = k
163-
i++
159+
ids := []uint64{}
160+
for _, c := range m.clientMap {
161+
if c.Client != nil {
162+
ids = append(ids, c.Id)
163+
}
164164
}
165165
return ids
166166
}

0 commit comments

Comments
 (0)