Skip to content

Commit 5c3154b

Browse files
committed
stop sending if the connenction is closed
1 parent 139514f commit 5c3154b

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

pulsar/internal/connection.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type Connection interface {
8888
Close()
8989
WaitForClose() <-chan struct{}
9090
IsProxied() bool
91+
Closed() bool
9192
}
9293

9394
type ConsumerHandler interface {
@@ -658,7 +659,7 @@ func (c *connection) SendRequestNoWait(req *pb.BaseCommand) error {
658659
}
659660

660661
func (c *connection) internalSendRequest(req *request) {
661-
if c.closed() {
662+
if c.Closed() {
662663
c.log.Warnf("internalSendRequest failed for connectionClosed")
663664
if req.callback != nil {
664665
req.callback(req.cmd, ErrConnectionClosed)
@@ -1064,7 +1065,7 @@ func (c *connection) setStateClosed() {
10641065
c.state.Store(int32(connectionClosed))
10651066
}
10661067

1067-
func (c *connection) closed() bool {
1068+
func (c *connection) Closed() bool {
10681069
return connectionClosed == c.getState()
10691070
}
10701071

pulsar/internal/connection_pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (p *connectionPool) GetConnection(logicalAddr *url.URL, physicalAddr *url.U
103103
// When the current connection is in a closed state or the broker actively notifies that the
104104
// current connection is closed, we need to remove the connection object from the current
105105
// connection pool and create a new connection.
106-
if conn.closed() {
106+
if conn.Closed() {
107107
p.log.Debugf("Removed connection from pool key=%s logical_addr=%+v physical_addr=%+v",
108108
key, conn.logicalAddr, conn.physicalAddr)
109109
delete(p.connections, key)

pulsar/internal/connection_reader.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import (
2121
"fmt"
2222
"io"
2323

24-
pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto"
2524
"google.golang.org/protobuf/proto"
25+
26+
pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto"
2627
)
2728

2829
type connectionReader struct {
@@ -41,7 +42,7 @@ func (r *connectionReader) readFromConnection() {
4142
for {
4243
cmd, headersAndPayload, err := r.readSingleCommand()
4344
if err != nil {
44-
if !r.cnx.closed() {
45+
if !r.cnx.Closed() {
4546
r.cnx.log.WithError(err).Infof("Error reading from connection")
4647
r.cnx.Close()
4748
}
@@ -122,7 +123,7 @@ func (r *connectionReader) readAtLeast(size uint32) error {
122123
n, err := io.ReadAtLeast(r.cnx.cnx, r.buffer.WritableSlice(), int(size))
123124
if err != nil {
124125
// has the connection been closed?
125-
if r.cnx.closed() {
126+
if r.cnx.Closed() {
126127
return errConnectionClosed
127128
}
128129
r.cnx.Close()

pulsar/producer_partition.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,15 @@ func (p *partitionProducer) writeData(buffer internal.Buffer, sequenceID uint64,
903903
sequenceID: sequenceID,
904904
sendRequests: callbacks,
905905
})
906-
p._getConn().WriteData(buffer)
906+
907+
// If the connection is closed, stop sending data. Continuing to send data
908+
// to a closed connection will cause the buffer to be passed to it, which
909+
// prevents further processing.
910+
conn := p._getConn()
911+
if conn.Closed() {
912+
return
913+
}
914+
conn.WriteData(buffer)
907915
}
908916
}
909917

0 commit comments

Comments
 (0)