Skip to content

Commit 4df0ee3

Browse files
committed
make CloseWithError required for transport conns
1 parent dfa7e28 commit 4df0ee3

File tree

6 files changed

+6
-33
lines changed

6 files changed

+6
-33
lines changed

p2p/net/swarm/swarm.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,7 @@ func (s *Swarm) addConn(tc transport.CapableConn, dir network.Direction) (*Conn,
385385
// If we do this in the Upgrader, we will not be able to do this.
386386
if s.gater != nil {
387387
if allow, _ := s.gater.InterceptUpgraded(c); !allow {
388-
var err error
389-
if tcc, ok := tc.(network.CloseWithErrorer); ok {
390-
err = tcc.CloseWithError(network.ConnGated)
391-
} else {
392-
err = tc.Close()
393-
}
388+
err := tc.CloseWithError(network.ConnGated)
394389
if err != nil {
395390
log.Warnf("failed to close connection with peer %s and addr %s; err: %s", p, addr, err)
396391
}
@@ -852,10 +847,7 @@ func (c *connWithMetrics) Close() error {
852847
func (c *connWithMetrics) CloseWithError(errCode network.ConnErrorCode) error {
853848
c.once.Do(func() {
854849
c.metricsTracer.ClosedConnection(c.dir, time.Since(c.opened), c.ConnState(), c.LocalMultiaddr())
855-
if ce, ok := c.CapableConn.(network.CloseWithErrorer); ok {
856-
c.closeErr = ce.CloseWithError(errCode)
857-
}
858-
c.closeErr = c.CapableConn.Close()
850+
c.closeErr = c.CapableConn.CloseWithError(errCode)
859851
})
860852
return c.closeErr
861853
}

p2p/net/swarm/swarm_conn.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ func (c *Conn) doClose(errCode network.ConnErrorCode) {
8181
c.streams.Unlock()
8282

8383
if errCode != 0 {
84-
if ce, ok := c.conn.(network.CloseWithErrorer); ok {
85-
c.err = ce.CloseWithError(errCode)
86-
} else {
87-
c.err = c.conn.Close()
88-
}
84+
c.err = c.conn.CloseWithError(errCode)
8985
} else {
9086
c.err = c.conn.Close()
9187
}

p2p/net/upgrader/conn.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,5 @@ func (t *transportConn) ConnState() network.ConnectionState {
6666

6767
func (t *transportConn) CloseWithError(errCode network.ConnErrorCode) error {
6868
defer t.scope.Done()
69-
if ce, ok := t.MuxedConn.(network.CloseWithErrorer); ok {
70-
return ce.CloseWithError(errCode)
71-
}
72-
return t.Close()
69+
return t.MuxedConn.CloseWithError(errCode)
7370
}

p2p/net/upgrader/listener.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,7 @@ func (l *listener) handleIncoming() {
162162
// if we stop accepting connections for some reason,
163163
// we'll eventually close all the open ones
164164
// instead of hanging onto them.
165-
if cc, ok := conn.(network.CloseWithErrorer); ok {
166-
cc.CloseWithError(network.ConnRateLimited)
167-
} else {
168-
conn.Close()
169-
}
165+
conn.CloseWithError(network.ConnRateLimited)
170166
}
171167
}()
172168
}

p2p/transport/quic/virtuallistener.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (r *acceptLoopRunner) innerAccept(l *listener, expectedVersion quic.Version
143143
select {
144144
case ch <- acceptVal{conn: conn}:
145145
default:
146-
conn.(network.CloseWithErrorer).CloseWithError(network.ConnRateLimited)
146+
conn.CloseWithError(network.ConnRateLimited)
147147
// accept queue filled up, drop the connection
148148
log.Warn("Accept queue filled. Dropping connection.")
149149
}

p2p/transport/websocket/conn.go

-8
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,3 @@ func (c *capableConn) ConnState() network.ConnectionState {
203203
cs.Transport = "websocket"
204204
return cs
205205
}
206-
207-
// CloseWithError implements network.CloseWithErrorer
208-
func (c *capableConn) CloseWithError(errCode network.ConnErrorCode) error {
209-
if ce, ok := c.CapableConn.(network.CloseWithErrorer); ok {
210-
return ce.CloseWithError(errCode)
211-
}
212-
return c.Close()
213-
}

0 commit comments

Comments
 (0)