Skip to content

Commit

Permalink
feat(tcp): add write to peer lock
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Jul 17, 2024
1 parent 58cb7e0 commit 06853c6
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions gold/p2p/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"reflect"
"strconv"
"sync"
"time"

"github.com/FloatTech/ttl"
Expand Down Expand Up @@ -71,6 +72,7 @@ func (ep *EndPoint) Listen() (p2p.Conn, error) {
}, nil,
}),
recv: make(chan *connrecv, chansz),
cplk: &sync.Mutex{},
}
go conn.accept()
return conn, nil
Expand All @@ -88,6 +90,7 @@ type Conn struct {
lstn *net.TCPListener
peers *ttl.Cache[string, *net.TCPConn]
recv chan *connrecv
cplk *sync.Mutex
}

func (conn *Conn) accept() {
Expand Down Expand Up @@ -236,6 +239,8 @@ func (conn *Conn) WriteToPeer(b []byte, ep p2p.EndPoint) (n int, err error) {
return 0, errors.New("data size " + strconv.Itoa(blen) + " is too large")
}
retried := false
conn.cplk.Lock()
defer conn.cplk.Unlock()
tcpconn := conn.peers.Get(tcpep.String())
RECONNECT:
if tcpconn == nil {
Expand Down

0 comments on commit 06853c6

Please sign in to comment.