github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/topic/topicwriterinternal/writer.go (about)

     1  package topicwriterinternal
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/jonboulle/clockwork"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/credentials"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawtopic/rawtopicwriter"
    10  )
    11  
    12  //go:generate mockgen -destination raw_topic_writer_stream_mock_test.go -package topicwriterinternal -write_package_comment=false github.com/ydb-platform/ydb-go-sdk/v3/internal/topic/topicwriterinternal RawTopicWriterStream
    13  
    14  type RawTopicWriterStream interface {
    15  	Recv() (rawtopicwriter.ServerMessage, error)
    16  	Send(mess rawtopicwriter.ClientMessage) error
    17  	CloseSend() error
    18  }
    19  
    20  type Writer struct {
    21  	streamWriter StreamWriter
    22  	clock        clockwork.Clock
    23  }
    24  
    25  func NewWriter(cred credentials.Credentials, options []PublicWriterOption) (*Writer, error) {
    26  	options = append(
    27  		options,
    28  		WithCredentials(cred),
    29  	)
    30  	cfg := newWriterReconnectorConfig(options...)
    31  	if err := cfg.validate(); err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	writerImpl := newWriterReconnector(cfg)
    36  
    37  	return &Writer{
    38  		streamWriter: writerImpl,
    39  		clock:        clockwork.NewRealClock(),
    40  	}, nil
    41  }
    42  
    43  func (w *Writer) Write(ctx context.Context, messages ...PublicMessage) error {
    44  	if err := ctx.Err(); err != nil {
    45  		return err
    46  	}
    47  
    48  	return w.streamWriter.Write(ctx, messages)
    49  }
    50  
    51  func (w *Writer) WaitInit(ctx context.Context) (info InitialInfo, err error) {
    52  	return w.streamWriter.WaitInit(ctx)
    53  }
    54  
    55  func (w *Writer) Close(ctx context.Context) error {
    56  	return w.streamWriter.Close(ctx)
    57  }