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

     1  package topicwriter
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/topic/topicwriterinternal"
     7  )
     8  
     9  type (
    10  	Message = topicwriterinternal.PublicMessage
    11  )
    12  
    13  var ErrQueueLimitExceed = topicwriterinternal.PublicErrQueueIsFull
    14  
    15  // Writer represent write session to topic
    16  // It handles connection problems, reconnect to server when need and resend buffered messages
    17  type Writer struct {
    18  	inner *topicwriterinternal.Writer
    19  }
    20  
    21  // PublicInitialInfo is an information about writer after initialize
    22  type PublicInitialInfo struct {
    23  	LastSeqNum int64
    24  }
    25  
    26  // NewWriter create new writer from internal type. Used internally only.
    27  func NewWriter(writer *topicwriterinternal.Writer) *Writer {
    28  	return &Writer{
    29  		inner: writer,
    30  	}
    31  }
    32  
    33  // Write send messages to topic
    34  // return after save messages into buffer in async mode (default) and after ack from server in sync mode.
    35  // see topicoptions.WithSyncWrite
    36  //
    37  // The method will wait first initial connection even for async mode, that mean first write may be slower.
    38  // especially when connection has problems.
    39  //
    40  // It returns ErrQueueLimitExceed (must be checked by errors.Is)
    41  // if ctx cancelled before messages put to internal buffer or try to add more messages, that can be put to queue
    42  func (w *Writer) Write(ctx context.Context, messages ...Message) error {
    43  	return w.inner.Write(ctx, messages...)
    44  }
    45  
    46  // WaitInit waits until the reader is initialized
    47  // or an error occurs, return PublicInitialInfo and err
    48  func (w *Writer) WaitInit(ctx context.Context) (err error) {
    49  	_, err = w.inner.WaitInit(ctx)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  // WaitInitInfo waits until the reader is initialized
    58  // or an error occurs, return PublicInitialInfo and err
    59  func (w *Writer) WaitInitInfo(ctx context.Context) (info PublicInitialInfo, err error) {
    60  	privateInfo, err := w.inner.WaitInit(ctx)
    61  	if err != nil {
    62  		return PublicInitialInfo{}, err
    63  	}
    64  	publicInfo := PublicInitialInfo{LastSeqNum: privateInfo.LastSeqNum}
    65  
    66  	return publicInfo, nil
    67  }
    68  
    69  func (w *Writer) Close(ctx context.Context) error {
    70  	return w.inner.Close(ctx)
    71  }