@@ -286,7 +286,7 @@ func (c *Client) BulkUpsert(
286286 tableName string ,
287287 data table.BulkUpsertData ,
288288 opts ... table.Option ,
289- ) error {
289+ ) ( finalErr error ) {
290290 if c == nil {
291291 return xerrors .WithStackTrace (errNilClient )
292292 }
@@ -295,34 +295,13 @@ func (c *Client) BulkUpsert(
295295 return xerrors .WithStackTrace (errClosedClient )
296296 }
297297
298- request , err := data .ToYDB (tableName )
299- if err != nil {
300- return xerrors .WithStackTrace (err )
301- }
302-
303- chunks := make ([]* Ydb_Table.BulkUpsertRequest , 0 , 1 )
304-
305- // We must send requests in chunks to avoid exceeding the maximum message size
306- chunks , err = chunkBulkUpsertRequest (chunks , request , c .config .MaxRequestMessageSize ())
307- if err != nil {
308- return err
309- }
310-
311- return c .sendBulkUpsertRequest (ctx , chunks , opts ... )
312- }
313-
314- func (c * Client ) sendBulkUpsertRequest (
315- ctx context.Context ,
316- requests []* Ydb_Table.BulkUpsertRequest ,
317- opts ... table.Option ,
318- ) (finalErr error ) {
319298 attempts , config := 0 , c .retryOptions (opts ... )
320299 config .RetryOptions = append (config .RetryOptions ,
321300 retry .WithIdempotent (true ),
322301 retry .WithTrace (& trace.Retry {
323302 OnRetry : func (info trace.RetryLoopStartInfo ) func (trace.RetryLoopDoneInfo ) {
324303 return func (info trace.RetryLoopDoneInfo ) {
325- attempts += info .Attempts - 1
304+ attempts += max ( info .Attempts - 1 , 0 ) // `max` guarded against negative values
326305 }
327306 },
328307 }),
@@ -335,6 +314,27 @@ func (c *Client) sendBulkUpsertRequest(
335314 onDone (finalErr , attempts + 1 )
336315 }()
337316
317+ request , err := data .ToYDB (tableName )
318+ if err != nil {
319+ return xerrors .WithStackTrace (err )
320+ }
321+
322+ chunks := make ([]* Ydb_Table.BulkUpsertRequest , 0 , 1 )
323+
324+ // We must send requests in chunks to avoid exceeding the maximum message size
325+ chunks , err = chunkBulkUpsertRequest (chunks , request , c .config .MaxRequestMessageSize ())
326+ if err != nil {
327+ return err
328+ }
329+
330+ return c .sendBulkUpsertRequest (ctx , chunks , config .RetryOptions ... )
331+ }
332+
333+ func (c * Client ) sendBulkUpsertRequest (
334+ ctx context.Context ,
335+ requests []* Ydb_Table.BulkUpsertRequest ,
336+ retryOpts ... retry.Option ,
337+ ) error {
338338 client := Ydb_Table_V1 .NewTableServiceClient (c .cc )
339339
340340 for _ , request := range requests {
@@ -344,7 +344,7 @@ func (c *Client) sendBulkUpsertRequest(
344344
345345 return err
346346 },
347- config . RetryOptions ... ,
347+ retryOpts ... ,
348348 )
349349 if err != nil {
350350 return xerrors .WithStackTrace (err )
0 commit comments