From 06853c65529165500f5e653cbc5e9cbb9b0641f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:29:15 +0900 Subject: [PATCH] feat(tcp): add write to peer lock --- gold/p2p/tcp/tcp.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gold/p2p/tcp/tcp.go b/gold/p2p/tcp/tcp.go index bd5f75c..e77edac 100644 --- a/gold/p2p/tcp/tcp.go +++ b/gold/p2p/tcp/tcp.go @@ -6,6 +6,7 @@ import ( "net" "reflect" "strconv" + "sync" "time" "github.com/FloatTech/ttl" @@ -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 @@ -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() { @@ -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 {