peer: gc active sub conn

This commit is contained in:
Sergey Cherepanov 2023-06-09 19:14:23 +02:00
parent b0c0e4b26e
commit e97e6b68c6
No known key found for this signature in database
GPG Key ID: 87F8EDE8FBDF637C
2 changed files with 20 additions and 0 deletions

View File

@ -222,8 +222,15 @@ func (p *peer) gc(ttl time.Duration) {
select { select {
case <-act.Closed(): case <-act.Closed():
delete(p.active, act) delete(p.active, act)
continue
default: default:
} }
if act.LastUsage().Before(minLastUsage) {
log.Warn("close active connection because no activity", zap.String("peerId", p.id), zap.String("addr", p.Addr()))
_ = act.Close()
delete(p.active, act)
continue
}
} }
} }

View File

@ -105,6 +105,7 @@ func TestPeer_TryClose(t *testing.T) {
}() }()
defer out2.Close() defer out2.Close()
fx.mc.EXPECT().Open(gomock.Any()).Return(in2, nil) fx.mc.EXPECT().Open(gomock.Any()).Return(in2, nil)
fx.mc.EXPECT().Addr().Return("")
dc2, err := fx.AcquireDrpcConn(ctx) dc2, err := fx.AcquireDrpcConn(ctx)
require.NoError(t, err) require.NoError(t, err)
_ = dc2.Close() _ = dc2.Close()
@ -118,6 +119,18 @@ func TestPeer_TryClose(t *testing.T) {
fx.mc.EXPECT().Open(gomock.Any()).Return(in3, nil) fx.mc.EXPECT().Open(gomock.Any()).Return(in3, nil)
dc3, err := fx.AcquireDrpcConn(ctx) dc3, err := fx.AcquireDrpcConn(ctx)
require.NoError(t, err) require.NoError(t, err)
// make another active, should be removed by ttl
in4, out4 := net.Pipe()
go func() {
handshake.IncomingProtoHandshake(ctx, out4, defaultProtoChecker)
}()
defer out4.Close()
fx.mc.EXPECT().Open(gomock.Any()).Return(in4, nil)
dc4, err := fx.AcquireDrpcConn(ctx)
require.NoError(t, err)
defer dc4.Close()
fx.ReleaseDrpcConn(dc3) fx.ReleaseDrpcConn(dc3)
_ = dc3.Close() _ = dc3.Close()
fx.ReleaseDrpcConn(dc) fx.ReleaseDrpcConn(dc)