code: void performFlush() {
      // Conditions could have changed between enqueuing the task and when it is run.
      if (!shouldFlush()) {
        return;
      }
  
      logger.verbose("Uploading payloads in queue to Segment.");
      int payloadsUploaded = 0;
      Client.Connection connection = null;
      try {
        // Open a connection.
        connection = client.upload();
  
        // Write the payloads into the OutputStream.
        BatchPayloadWriter writer =
            new BatchPayloadWriter(connection.os) //
                .beginObject() //
                .beginBatchArray();
        PayloadWriter payloadWriter = new PayloadWriter(writer, crypto);
        payloadQueue.forEach(payloadWriter);
        writer.endBatchArray().endObject().close();
        // Don't use the result of QueueFiles#forEach, since we may not upload the last element.
        payloadsUploaded = payloadWriter.payloadCount;
  
        // Upload the payloads.
        connection.close();
      } catch (Client.HTTPException e) {
        if (e.is4xx() && e.responseCode != 429) {
          // Simply log and proceed to remove the rejected payloads from the queue.
          logger.error(e, "Payloads were rejected by server. Marked for removal.");
          try {
            payloadQueue.remove(payloadsUploaded);
          } catch (IOException e1) {
            logger.error(e, "Unable to remove " + payloadsUploaded + " payload(s) from queue.");
          }
          return;
        } else {
          logger.error(e, "Error while uploading payloads");
          return;
        }
      } catch (IOException e) {
        logger.error(e, "Error while uploading payloads");
        return;
      } finally {
        closeQuietly(connection);
      }
  
      try {
        payloadQueue.remove(payloadsUploaded);
      } catch (IOException e) {
        logger.error(e, "Unable to remove " + payloadsUploaded + " payload(s) from queue.");
        return;
      }
  
      logger.verbose(
          "Uploaded %s payloads. %s remain in the queue.", payloadsUploaded, payloadQueue.size());
      stats.dispatchFlush(payloadsUploaded);
      if (payloadQueue.size() > 0) {
        performFlush(); // Flush any remaining items.
      }
    }
docs: Upload payloads to our servers and remove them from the queue file.