neoneo-go/pkg/network/tcp_transport.go

116 lines
2.3 KiB
Go
Raw Normal View History

package network
import (
"errors"
"net"
"sync"
"time"
2019-12-30 07:43:05 +00:00
"go.uber.org/zap"
)
// TCPTransport allows network communication over TCP.
type TCPTransport struct {
2019-12-30 07:43:05 +00:00
log *zap.Logger
server *Server
listener net.Listener
bindAddr string
hostPort hostPort
lock sync.RWMutex
network: don't log transport errors on exit Fix data race ================== WARNING: DATA RACE Read at 0x00c00012cca3 by goroutine 208: testing.(*common).logDepth() /usr/local/go/src/testing/testing.go:727 +0xa4 testing.(*common).log() /usr/local/go/src/testing/testing.go:720 +0x8f testing.(*common).Logf() /usr/local/go/src/testing/testing.go:766 +0x21 testing.(*T).Logf() <autogenerated>:1 +0x75 go.uber.org/zap/zaptest.testingWriter.Write() /go/pkg/mod/go.uber.org/zap@v1.10.0/zaptest/logger.go:130 +0x11f go.uber.org/zap/zaptest.(*testingWriter).Write() <autogenerated>:1 +0xa9 go.uber.org/zap/zapcore.(*ioCore).Write() /go/pkg/mod/go.uber.org/zap@v1.10.0/zapcore/core.go:90 +0x1c3 go.uber.org/zap/zapcore.(*CheckedEntry).Write() /go/pkg/mod/go.uber.org/zap@v1.10.0/zapcore/entry.go:215 +0x1e7 go.uber.org/zap.(*Logger).Warn() /go/pkg/mod/go.uber.org/zap@v1.10.0/logger.go:195 +0x95 github.com/nspcc-dev/neo-go/pkg/network.(*TCPTransport).Accept() /go/src/github.com/nspcc-dev/neo-go/pkg/network/tcp_transport.go:59 +0x8da Previous write at 0x00c00012cca3 by goroutine 168: testing.tRunner.func1() /usr/local/go/src/testing/testing.go:1036 +0x467 testing.tRunner() /usr/local/go/src/testing/testing.go:1054 +0x20d Goroutine 208 (running) created at: github.com/nspcc-dev/neo-go/pkg/network.(*Server).Start() /go/src/github.com/nspcc-dev/neo-go/pkg/network/server.go:274 +0x391 Goroutine 168 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:1095 +0x537 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1339 +0xa6 testing.tRunner() /usr/local/go/src/testing/testing.go:1050 +0x1eb testing.runTests() /usr/local/go/src/testing/testing.go:1337 +0x594 testing.(*M).Run() /usr/local/go/src/testing/testing.go:1252 +0x2ff main.main() _testmain.go:90 +0x223 ==================
2021-07-07 17:59:01 +00:00
quit bool
}
type hostPort struct {
Host string
Port string
}
2019-10-22 14:56:03 +00:00
// NewTCPTransport returns a new TCPTransport that will listen for
// new incoming peer connections.
2019-12-30 07:43:05 +00:00
func NewTCPTransport(s *Server, bindAddr string, log *zap.Logger) *TCPTransport {
host, port, err := net.SplitHostPort(bindAddr)
if err != nil {
// Only host can be provided, it's OK.
host = bindAddr
}
return &TCPTransport{
2019-12-30 07:43:05 +00:00
log: log,
server: s,
bindAddr: bindAddr,
hostPort: hostPort{
Host: host,
Port: port,
},
}
}
// Dial implements the Transporter interface.
func (t *TCPTransport) Dial(addr string, timeout time.Duration) (AddressablePeer, error) {
conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return nil, err
}
p := NewTCPPeer(conn, addr, t.server)
go p.handleConn()
return p, nil
}
// Accept implements the Transporter interface.
func (t *TCPTransport) Accept() {
l, err := net.Listen("tcp", t.bindAddr)
if err != nil {
2019-12-30 07:43:05 +00:00
t.log.Panic("TCP listen error", zap.Error(err))
return
}
t.lock.Lock()
network: don't log transport errors on exit Fix data race ================== WARNING: DATA RACE Read at 0x00c00012cca3 by goroutine 208: testing.(*common).logDepth() /usr/local/go/src/testing/testing.go:727 +0xa4 testing.(*common).log() /usr/local/go/src/testing/testing.go:720 +0x8f testing.(*common).Logf() /usr/local/go/src/testing/testing.go:766 +0x21 testing.(*T).Logf() <autogenerated>:1 +0x75 go.uber.org/zap/zaptest.testingWriter.Write() /go/pkg/mod/go.uber.org/zap@v1.10.0/zaptest/logger.go:130 +0x11f go.uber.org/zap/zaptest.(*testingWriter).Write() <autogenerated>:1 +0xa9 go.uber.org/zap/zapcore.(*ioCore).Write() /go/pkg/mod/go.uber.org/zap@v1.10.0/zapcore/core.go:90 +0x1c3 go.uber.org/zap/zapcore.(*CheckedEntry).Write() /go/pkg/mod/go.uber.org/zap@v1.10.0/zapcore/entry.go:215 +0x1e7 go.uber.org/zap.(*Logger).Warn() /go/pkg/mod/go.uber.org/zap@v1.10.0/logger.go:195 +0x95 github.com/nspcc-dev/neo-go/pkg/network.(*TCPTransport).Accept() /go/src/github.com/nspcc-dev/neo-go/pkg/network/tcp_transport.go:59 +0x8da Previous write at 0x00c00012cca3 by goroutine 168: testing.tRunner.func1() /usr/local/go/src/testing/testing.go:1036 +0x467 testing.tRunner() /usr/local/go/src/testing/testing.go:1054 +0x20d Goroutine 208 (running) created at: github.com/nspcc-dev/neo-go/pkg/network.(*Server).Start() /go/src/github.com/nspcc-dev/neo-go/pkg/network/server.go:274 +0x391 Goroutine 168 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:1095 +0x537 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1339 +0xa6 testing.tRunner() /usr/local/go/src/testing/testing.go:1050 +0x1eb testing.runTests() /usr/local/go/src/testing/testing.go:1337 +0x594 testing.(*M).Run() /usr/local/go/src/testing/testing.go:1252 +0x2ff main.main() _testmain.go:90 +0x223 ==================
2021-07-07 17:59:01 +00:00
if t.quit {
t.lock.Unlock()
l.Close()
return
}
t.listener = l
t.bindAddr = l.Addr().String()
t.hostPort.Host, t.hostPort.Port, _ = net.SplitHostPort(t.bindAddr) // no error expected as l.Addr() is a valid address.
t.lock.Unlock()
for {
conn, err := l.Accept()
if err != nil {
network: don't log transport errors on exit Fix data race ================== WARNING: DATA RACE Read at 0x00c00012cca3 by goroutine 208: testing.(*common).logDepth() /usr/local/go/src/testing/testing.go:727 +0xa4 testing.(*common).log() /usr/local/go/src/testing/testing.go:720 +0x8f testing.(*common).Logf() /usr/local/go/src/testing/testing.go:766 +0x21 testing.(*T).Logf() <autogenerated>:1 +0x75 go.uber.org/zap/zaptest.testingWriter.Write() /go/pkg/mod/go.uber.org/zap@v1.10.0/zaptest/logger.go:130 +0x11f go.uber.org/zap/zaptest.(*testingWriter).Write() <autogenerated>:1 +0xa9 go.uber.org/zap/zapcore.(*ioCore).Write() /go/pkg/mod/go.uber.org/zap@v1.10.0/zapcore/core.go:90 +0x1c3 go.uber.org/zap/zapcore.(*CheckedEntry).Write() /go/pkg/mod/go.uber.org/zap@v1.10.0/zapcore/entry.go:215 +0x1e7 go.uber.org/zap.(*Logger).Warn() /go/pkg/mod/go.uber.org/zap@v1.10.0/logger.go:195 +0x95 github.com/nspcc-dev/neo-go/pkg/network.(*TCPTransport).Accept() /go/src/github.com/nspcc-dev/neo-go/pkg/network/tcp_transport.go:59 +0x8da Previous write at 0x00c00012cca3 by goroutine 168: testing.tRunner.func1() /usr/local/go/src/testing/testing.go:1036 +0x467 testing.tRunner() /usr/local/go/src/testing/testing.go:1054 +0x20d Goroutine 208 (running) created at: github.com/nspcc-dev/neo-go/pkg/network.(*Server).Start() /go/src/github.com/nspcc-dev/neo-go/pkg/network/server.go:274 +0x391 Goroutine 168 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:1095 +0x537 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1339 +0xa6 testing.tRunner() /usr/local/go/src/testing/testing.go:1050 +0x1eb testing.runTests() /usr/local/go/src/testing/testing.go:1337 +0x594 testing.(*M).Run() /usr/local/go/src/testing/testing.go:1252 +0x2ff main.main() _testmain.go:90 +0x223 ==================
2021-07-07 17:59:01 +00:00
t.lock.Lock()
quit := t.quit
t.lock.Unlock()
if errors.Is(err, net.ErrClosed) && quit {
break
}
t.log.Warn("TCP accept error", zap.Stringer("address", l.Addr()), zap.Error(err))
continue
}
p := NewTCPPeer(conn, "", t.server)
go p.handleConn()
}
}
// Close implements the Transporter interface.
func (t *TCPTransport) Close() {
2020-09-02 10:21:24 +00:00
t.lock.Lock()
if t.listener != nil {
t.listener.Close()
}
network: don't log transport errors on exit Fix data race ================== WARNING: DATA RACE Read at 0x00c00012cca3 by goroutine 208: testing.(*common).logDepth() /usr/local/go/src/testing/testing.go:727 +0xa4 testing.(*common).log() /usr/local/go/src/testing/testing.go:720 +0x8f testing.(*common).Logf() /usr/local/go/src/testing/testing.go:766 +0x21 testing.(*T).Logf() <autogenerated>:1 +0x75 go.uber.org/zap/zaptest.testingWriter.Write() /go/pkg/mod/go.uber.org/zap@v1.10.0/zaptest/logger.go:130 +0x11f go.uber.org/zap/zaptest.(*testingWriter).Write() <autogenerated>:1 +0xa9 go.uber.org/zap/zapcore.(*ioCore).Write() /go/pkg/mod/go.uber.org/zap@v1.10.0/zapcore/core.go:90 +0x1c3 go.uber.org/zap/zapcore.(*CheckedEntry).Write() /go/pkg/mod/go.uber.org/zap@v1.10.0/zapcore/entry.go:215 +0x1e7 go.uber.org/zap.(*Logger).Warn() /go/pkg/mod/go.uber.org/zap@v1.10.0/logger.go:195 +0x95 github.com/nspcc-dev/neo-go/pkg/network.(*TCPTransport).Accept() /go/src/github.com/nspcc-dev/neo-go/pkg/network/tcp_transport.go:59 +0x8da Previous write at 0x00c00012cca3 by goroutine 168: testing.tRunner.func1() /usr/local/go/src/testing/testing.go:1036 +0x467 testing.tRunner() /usr/local/go/src/testing/testing.go:1054 +0x20d Goroutine 208 (running) created at: github.com/nspcc-dev/neo-go/pkg/network.(*Server).Start() /go/src/github.com/nspcc-dev/neo-go/pkg/network/server.go:274 +0x391 Goroutine 168 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:1095 +0x537 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1339 +0xa6 testing.tRunner() /usr/local/go/src/testing/testing.go:1050 +0x1eb testing.runTests() /usr/local/go/src/testing/testing.go:1337 +0x594 testing.(*M).Run() /usr/local/go/src/testing/testing.go:1252 +0x2ff main.main() _testmain.go:90 +0x223 ==================
2021-07-07 17:59:01 +00:00
t.quit = true
2020-09-02 10:21:24 +00:00
t.lock.Unlock()
}
// Proto implements the Transporter interface.
func (t *TCPTransport) Proto() string {
return "tcp"
}
// HostPort implements the Transporter interface.
func (t *TCPTransport) HostPort() (string, string) {
t.lock.RLock()
defer t.lock.RUnlock()
return t.hostPort.Host, t.hostPort.Port
}