use mutex to simplify the locking

fix travis test error by adding functions in mockhost

Signed-off-by: Leo Chen <leo@harmony.one>
pull/405/head
Leo Chen 6 years ago
parent c6c7f9c46d
commit b7838579de
  1. 20
      api/service/networkinfo/service.go
  2. 22
      p2p/host/hostv2/hostv2.go
  3. 33
      p2p/host/mock/host_mock.go

@ -28,6 +28,7 @@ type Service struct {
peerChan chan p2p.Peer peerChan chan p2p.Peer
peerInfo <-chan peerstore.PeerInfo peerInfo <-chan peerstore.PeerInfo
discovery *libp2pdis.RoutingDiscovery discovery *libp2pdis.RoutingDiscovery
lock sync.Mutex
} }
// New returns role conversion service. // New returns role conversion service.
@ -116,17 +117,14 @@ func (s *Service) DoService() {
} }
if peer.ID != s.Host.GetP2PHost().ID() && len(peer.ID) > 0 { if peer.ID != s.Host.GetP2PHost().ID() && len(peer.ID) > 0 {
utils.GetLogInstance().Info("Found Peer", "peer", peer.ID, "addr", peer.Addrs, "my ID", s.Host.GetP2PHost().ID()) utils.GetLogInstance().Info("Found Peer", "peer", peer.ID, "addr", peer.Addrs, "my ID", s.Host.GetP2PHost().ID())
var wg sync.WaitGroup s.lock.Lock()
wg.Add(1) if err := s.Host.GetP2PHost().Connect(s.ctx, peer); err != nil {
go func() { utils.GetLogInstance().Warn("can't connect to peer node", "error", err)
defer wg.Done() } else {
if err := s.Host.GetP2PHost().Connect(s.ctx, peer); err != nil { utils.GetLogInstance().Info("connected to peer node", "peer", peer)
utils.GetLogInstance().Warn("can't connect to peer node", "error", err) }
} else { s.lock.Unlock()
utils.GetLogInstance().Info("connected to peer node", "peer", peer) // figure out the public ip/port
}
}()
wg.Wait()
ip := "127.0.0.1" ip := "127.0.0.1"
var port string var port string
for _, addr := range peer.Addrs { for _, addr := range peer.Addrs {

@ -45,6 +45,7 @@ type HostV2 struct {
pubsub PubSub pubsub PubSub
self p2p.Peer self p2p.Peer
priKey p2p_crypto.PrivKey priKey p2p_crypto.PrivKey
lock sync.Mutex
incomingPeers []p2p.Peer // list of incoming Peers. TODO: fixed number incoming incomingPeers []p2p.Peer // list of incoming Peers. TODO: fixed number incoming
outgoingPeers []p2p.Peer // list of outgoing Peers. TODO: fixed number of outgoing outgoingPeers []p2p.Peer // list of outgoing Peers. TODO: fixed number of outgoing
@ -161,7 +162,8 @@ func New(self *p2p.Peer, priKey p2p_crypto.PrivKey, opts ...p2p_config.Option) *
append(opts, libp2p.ListenAddrs(listenAddr), libp2p.Identity(priKey))..., append(opts, libp2p.ListenAddrs(listenAddr), libp2p.Identity(priKey))...,
) )
catchError(err) catchError(err)
pubsub, err := pubsub.NewGossipSub(ctx, p2pHost) // pubsub, err := pubsub.NewGossipSub(ctx, p2pHost)
pubsub, err := pubsub.NewFloodSub(ctx, p2pHost)
catchError(err) catchError(err)
self.PeerID = p2pHost.ID() self.PeerID = p2pHost.ID()
@ -244,15 +246,11 @@ func (host *HostV2) ConnectHostPeer(peer p2p.Peer) {
utils.GetLogInstance().Error("ConnectHostPeer", "new peerinfo error", err, "peer", peer) utils.GetLogInstance().Error("ConnectHostPeer", "new peerinfo error", err, "peer", peer)
return return
} }
var wg sync.WaitGroup host.lock.Lock()
wg.Add(1) defer host.lock.Unlock()
go func() { if err := host.h.Connect(ctx, *peerInfo); err != nil {
defer wg.Done() utils.GetLogInstance().Warn("can't connect to peer", "error", err, "peer", peer)
if err := host.h.Connect(ctx, *peerInfo); err != nil { } else {
utils.GetLogInstance().Warn("can't connect to peer", "error", err, "peer", peer) utils.GetLogInstance().Info("connected to peer host", "node", *peerInfo)
} else { }
utils.GetLogInstance().Info("connected to peer host", "node", *peerInfo)
}
}()
wg.Wait()
} }

@ -5,11 +5,12 @@
package mock_p2p package mock_p2p
import ( import (
reflect "reflect"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
p2p "github.com/harmony-one/harmony/p2p" p2p "github.com/harmony-one/harmony/p2p"
go_libp2p_host "github.com/libp2p/go-libp2p-host" go_libp2p_host "github.com/libp2p/go-libp2p-host"
go_libp2p_peer "github.com/libp2p/go-libp2p-peer" go_libp2p_peer "github.com/libp2p/go-libp2p-peer"
reflect "reflect"
) )
// MockHost is a mock of Host interface // MockHost is a mock of Host interface
@ -141,3 +142,33 @@ func (m *MockHost) GroupReceiver(arg0 p2p.GroupID) (p2p.GroupReceiver, error) {
func (mr *MockHostMockRecorder) GroupReceiver(arg0 interface{}) *gomock.Call { func (mr *MockHostMockRecorder) GroupReceiver(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GroupReceiver", reflect.TypeOf((*MockHost)(nil).GroupReceiver), arg0) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GroupReceiver", reflect.TypeOf((*MockHost)(nil).GroupReceiver), arg0)
} }
// AddIncomingPeer mocks base method
func (m *MockHost) AddIncomingPeer(peer p2p.Peer) {
m.ctrl.Call(m, "AddIncomingPeer", peer)
}
// AddIncomingPeer indicates an expected call of AddIncomingPeer
func (mr *MockHostMockRecorder) AddIncomingPeer(groups, msg interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddIncomingPeer", reflect.TypeOf((*MockHost)(nil).AddIncomingPeer), groups, msg)
}
// AddOutgoingPeer mocks base method
func (m *MockHost) AddOutgoingPeer(peer p2p.Peer) {
m.ctrl.Call(m, "AddOutgoingPeer", peer)
}
// AddOutgoingPeer indicates an expected call of AddOutgoingPeer
func (mr *MockHostMockRecorder) AddOutgoingPeer(groups, msg interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddOutgoingPeer", reflect.TypeOf((*MockHost)(nil).AddOutgoingPeer), groups, msg)
}
// ConnectHostPeer mocks base method
func (m *MockHost) ConnectHostPeer(peer p2p.Peer) {
m.ctrl.Call(m, "ConnectHostPeer", peer)
}
// ConnectHostPeer indicates an expected call of ConnectHostPeer
func (mr *MockHostMockRecorder) ConnectHostPeer(groups, msg interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConnectHostPeer", reflect.TypeOf((*MockHost)(nil).ConnectHostPeer), groups, msg)
}

Loading…
Cancel
Save