Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-listener-close #1485

Merged
merged 8 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Fixed error on experimental TopicListener.Close
* Disabled reporting of `ydb_go_sdk_query_session_count` when metrics are disabled
* Disabled reporting of `ydb_go_sdk_ydb_query_session_create_latency` histogram metrics when metrics are disabled
* Allowed skip column for `ScanStruct` by tag `-`
Expand Down
27 changes: 18 additions & 9 deletions internal/topic/topiclistenerinternal/stream_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type streamListener struct {
hasNewMessagesToSend empty.Chan
syncCommitter *topicreadercommon.Committer

closing atomic.Bool

m xsync.Mutex
messagesToSend []rawtopicreader.ClientMessage
}
Expand All @@ -56,7 +58,7 @@ func newStreamListener(

res.initVars(sessionIDCounter)
if err := res.initStream(connectionCtx, client); err != nil {
res.closeWithTimeout(connectionCtx, err)
res.goClose(connectionCtx, err)

return nil, err
}
Expand All @@ -75,17 +77,22 @@ func newStreamListener(
}

func (l *streamListener) Close(ctx context.Context, reason error) error {
if !l.closing.CompareAndSwap(false, true) {
return errTopicListenerClosed
}

var resErrors []error

// should be first because background wait stop of steams
if l.stream != nil {
l.streamClose(reason)
}

if err := l.syncCommitter.Close(ctx, reason); err != nil {
if err := l.background.Close(ctx, reason); err != nil {
resErrors = append(resErrors, err)
}

if err := l.background.Close(ctx, reason); err != nil {
if err := l.syncCommitter.Close(ctx, reason); err != nil {
resErrors = append(resErrors, err)
}

Expand All @@ -109,10 +116,12 @@ func (l *streamListener) Close(ctx context.Context, reason error) error {
return errors.Join(resErrors...)
}

func (l *streamListener) closeWithTimeout(ctx context.Context, reason error) {
func (l *streamListener) goClose(ctx context.Context, reason error) {
ctx, cancel := context.WithTimeout(xcontext.ValueOnly(ctx), time.Second)
l.streamClose(reason)
_ = l.background.Close(ctx, reason)
go func() {
_ = l.background.Close(ctx, reason)
}()

cancel()
}
Expand Down Expand Up @@ -145,7 +154,7 @@ func (l *streamListener) initStream(ctx context.Context, client TopicClient) err
err := xerrors.WithStackTrace(xerrors.Wrap(fmt.Errorf(
"ydb: topic listener stream init timeout: %w", ctx.Err(),
)))
l.closeWithTimeout(ctx, err)
l.goClose(ctx, err)
l.streamClose(err)
case <-initDone:
// pass
Expand Down Expand Up @@ -216,7 +225,7 @@ func (l *streamListener) sendMessagesLoop(ctx context.Context) {

for _, m := range messages {
if err := l.stream.Send(m); err != nil {
l.closeWithTimeout(ctx, xerrors.WithStackTrace(xerrors.Wrap(fmt.Errorf(
l.goClose(ctx, xerrors.WithStackTrace(xerrors.Wrap(fmt.Errorf(
"ydb: failed send message by grpc to topic reader stream from listener: %w",
err,
))))
Expand All @@ -236,7 +245,7 @@ func (l *streamListener) receiveMessagesLoop(ctx context.Context) {

mess, err := l.stream.Recv()
if err != nil {
l.closeWithTimeout(ctx, xerrors.WithStackTrace(
l.goClose(ctx, xerrors.WithStackTrace(
fmt.Errorf("ydb: failed read message from the stream in the topic reader listener: %w", err),
))

Expand All @@ -263,7 +272,7 @@ func (l *streamListener) onReceiveServerMessage(ctx context.Context, mess rawtop
// todo log
}
if err != nil {
l.closeWithTimeout(ctx, err)
l.goClose(ctx, err)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3/internal/empty"
)

var ErrUserCloseTopic = errors.New("ydb: user closed topic listener")
var (
ErrUserCloseTopic = errors.New("ydb: user closed topic listener")
errTopicListenerClosed = errors.New("ydb: the topic listener already closed")
)

type TopicListenerReconnector struct {
streamConfig *StreamListenerConfig
Expand All @@ -22,6 +25,7 @@ type TopicListenerReconnector struct {
connectionResult error
connectionCompleted empty.Chan
connectionIDCounter atomic.Int64
closing atomic.Bool

m sync.Mutex
streamListener *streamListener
Expand All @@ -45,6 +49,9 @@ func NewTopicListenerReconnector(
}

func (lr *TopicListenerReconnector) Close(ctx context.Context, reason error) error {
if !lr.closing.CompareAndSwap(false, true) {
return errTopicListenerClosed
}
var closeErrors []error
err := lr.background.Close(ctx, reason)
closeErrors = append(closeErrors, err)
Expand Down
Loading