Skip to content

Commit c3d1acd

Browse files
authored
Merge pull request #339 from meshplus/fix-retry-start-plugin
Fix retry start plugin
2 parents 2f7c630 + 2d790a7 commit c3d1acd

File tree

4 files changed

+42
-38
lines changed

4 files changed

+42
-38
lines changed

go.sum

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,6 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
697697
github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
698698
github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE=
699699
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
700-
github.com/meshplus/bitxhub-core v1.3.1-0.20220419015433-0fc9632c8a9c h1:LNX5HHIw2iHdT2+Hxvfo7TWU3fAgL46iEHNVRq7Xdro=
701-
github.com/meshplus/bitxhub-core v1.3.1-0.20220419015433-0fc9632c8a9c/go.mod h1:4XwpJi5KRDb7Uwzf8mCy3XZVrLw2HOA3pngpvyGLtLU=
702700
github.com/meshplus/bitxhub-core v1.3.1-0.20220419072638-73c914bd56f1 h1:R0cssmTlWe8yeRiEjw/zo/q8HLh3To+mX5mqrfgpDJM=
703701
github.com/meshplus/bitxhub-core v1.3.1-0.20220419072638-73c914bd56f1/go.mod h1:Rkokix/QLfDhQBTtr5jvFNJlxnPH4tb3oZhgTXDP3iY=
704702
github.com/meshplus/bitxhub-kit v1.0.0-rc1/go.mod h1:ra/AhOkPvpElI+wXrB9G6DjdcrdxFU3vMwA5MYKr9D0=
@@ -708,10 +706,8 @@ github.com/meshplus/bitxhub-kit v1.2.1-0.20220325052414-bc17176c509d/go.mod h1:w
708706
github.com/meshplus/bitxhub-kit v1.2.1-0.20220412092457-5836414df781 h1:xFu/v4zA+nbveKVc/GxzCZr8opP7AzDL79mWivAY/DA=
709707
github.com/meshplus/bitxhub-kit v1.2.1-0.20220412092457-5836414df781/go.mod h1:wrEdhHp1tktzdwcWb4bOxYsVc+KkcrYL18IYWYeumPQ=
710708
github.com/meshplus/bitxhub-model v1.0.0-rc3/go.mod h1:ZCctQIYTlE3vJ8Lhkrgs9bWwNA+Dw4JzojOSIzLVU6E=
711-
github.com/meshplus/bitxhub-model v1.2.1-0.20220407083019-e55040281b3f/go.mod h1:sPko8fD+G3MYOnH/44Ju4T4jD/tlEU3IJDIJ4hjrzxg=
712709
github.com/meshplus/bitxhub-model v1.2.1-0.20220412064024-c35cae241eb2/go.mod h1:sPko8fD+G3MYOnH/44Ju4T4jD/tlEU3IJDIJ4hjrzxg=
713-
github.com/meshplus/bitxhub-model v1.2.1-0.20220415095224-3a9571aa1d15 h1:8BP+MSBMq2r5UquhNrAA/ZOdNLmCgNWBUnvZpxIWAjU=
714-
github.com/meshplus/bitxhub-model v1.2.1-0.20220415095224-3a9571aa1d15/go.mod h1:sPko8fD+G3MYOnH/44Ju4T4jD/tlEU3IJDIJ4hjrzxg=
710+
github.com/meshplus/bitxhub-model v1.2.1-0.20220419071920-bd5cc4a17768 h1:OMfyCVQAHsGYh/J24MQmJfrcMxCKwZ9nmWlqBMyS+s8=
715711
github.com/meshplus/bitxhub-model v1.2.1-0.20220419071920-bd5cc4a17768/go.mod h1:sPko8fD+G3MYOnH/44Ju4T4jD/tlEU3IJDIJ4hjrzxg=
716712
github.com/meshplus/go-bitxhub-client v1.4.1-0.20220412093230-11ca79f069fc h1:3J52XPtUFf0gvJHn61EBr+pI268/edQv544w+TWgEwU=
717713
github.com/meshplus/go-bitxhub-client v1.4.1-0.20220412093230-11ca79f069fc/go.mod h1:pvV1elDWb8ITsOccfJa6CXEHHHb7yURmtGSOngTPL8c=

internal/adapt/appchain_adapter/adapter.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package appchain_adapter
33
import (
44
"fmt"
55
"strings"
6-
"time"
76

8-
"github.com/Rican7/retry"
9-
"github.com/Rican7/retry/strategy"
107
"github.com/hashicorp/go-plugin"
118
"github.com/meshplus/bitxhub-model/pb"
129
"github.com/meshplus/pier/internal/adapt"
@@ -312,15 +309,15 @@ func findRemoteInterchain(remoteServiceID string, outMeta map[string]uint64, cal
312309
func (a *AppchainAdapter) init() error {
313310
var err error
314311

315-
if err := retry.Retry(func(attempt uint) error {
316-
a.client, a.pluginClient, err = plugins.CreateClient(&a.config.Appchain, nil)
317-
if err != nil {
318-
a.logger.Errorf("create client plugin", "error", err.Error())
319-
}
320-
return err
321-
}, strategy.Wait(3*time.Second)); err != nil {
322-
return fmt.Errorf("retry error to create plugin: %w", err)
312+
//if err := retry.Retry(func(attempt uint) error {
313+
a.client, a.pluginClient, err = plugins.CreateClient(&a.config.Appchain, nil)
314+
if err != nil {
315+
a.logger.Errorf("create client plugin", "error", err.Error())
323316
}
317+
return err
318+
//}, strategy.Wait(3*time.Second)); err != nil {
319+
// return fmt.Errorf("retry error to create plugin: %w", err)
320+
//}
324321

325322
a.ibtpC = make(chan *pb.IBTP, IBTP_CH_SIZE)
326323

internal/adapt/direct_adapter/adapter.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package direct_adapter
22

33
import (
4+
"context"
45
"fmt"
56
"runtime"
67
"sync"
@@ -33,6 +34,8 @@ type DirectAdapter struct {
3334
ibtps sync.Map
3435
appchainID string
3536
remotePierID string
37+
ctx context.Context
38+
cancel context.CancelFunc
3639
gopool *pool
3740
}
3841

@@ -55,7 +58,7 @@ func (d *DirectAdapter) GetServiceIDList() ([]string, error) {
5558
func New(peerMgr peermgr.PeerManager, appchainAdapt adapt.Adapt, logger logrus.FieldLogger) (*DirectAdapter, error) {
5659

5760
appchainID := appchainAdapt.ID()
58-
61+
ctx, cancel := context.WithCancel(context.Background())
5962
da := &DirectAdapter{
6063
logger: logger,
6164
peerMgr: peerMgr,
@@ -64,6 +67,8 @@ func New(peerMgr peermgr.PeerManager, appchainAdapt adapt.Adapt, logger logrus.F
6467
ibtpC: make(chan *pb.IBTP, maxChSize),
6568
appchainID: appchainID,
6669
gopool: NewGoPool(runtime.GOMAXPROCS(runtime.NumCPU())),
70+
ctx: ctx,
71+
cancel: cancel,
6772
}
6873

6974
return da, nil

internal/adapt/direct_adapter/handler.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,31 +82,37 @@ func (d *DirectAdapter) handleSendIBTPMessage(stream network.Stream, msg *pb.Mes
8282
}()
8383
d.ibtpC <- ibtp
8484
index := ibtp.Index
85+
ticker := time.NewTicker(time.Second)
86+
defer ticker.Stop()
8587
for {
86-
pool.lock.Lock()
87-
if item := pool.ibtps.Min(); item != nil {
88-
if item.(*MyTree).index < index+1 {
89-
pool.ibtps.DeleteMin()
90-
}
91-
92-
if item.(*MyTree).index == index+1 {
93-
d.ibtpC <- item.(*MyTree).ibtp
94-
pool.ibtps.DeleteMin()
95-
index++
96-
pool.time = time.Now()
97-
}
98-
99-
// By default, the index will be equalized after 5 seconds
100-
if time.Now().Sub(pool.time).Seconds() > 5.0 {
101-
d.ibtpC <- item.(*MyTree).ibtp
102-
pool.ibtps.DeleteMin()
103-
index = item.(*MyTree).index
104-
pool.time = time.Now()
88+
select {
89+
case <-ticker.C:
90+
pool.lock.Lock()
91+
if item := pool.ibtps.Min(); item != nil {
92+
if item.(*MyTree).index < index+1 {
93+
pool.ibtps.DeleteMin()
94+
}
95+
96+
if item.(*MyTree).index == index+1 {
97+
d.ibtpC <- item.(*MyTree).ibtp
98+
pool.ibtps.DeleteMin()
99+
index++
100+
pool.time = time.Now()
101+
}
102+
103+
// By default, the index will be equalized after 5 seconds
104+
if time.Now().Sub(pool.time).Seconds() > 5.0 {
105+
d.ibtpC <- item.(*MyTree).ibtp
106+
pool.ibtps.DeleteMin()
107+
index = item.(*MyTree).index
108+
pool.time = time.Now()
109+
}
105110
}
111+
pool.lock.Unlock()
112+
case <-d.ctx.Done():
113+
return
106114
}
107-
pool.lock.Unlock()
108115
}
109-
110116
}(pool, ibtp)
111117
} else {
112118
pool.lock.Lock()

0 commit comments

Comments
 (0)