How to reuse batchpoints in the client API?

Hi there,

I am using the influxdb v2 client to batch points and send it. I have a flush function in which I write the batchpoints, and then start using a new batchpoints struct. Is there a better way to achieve this ? I don’t see any clearPoints API or anything like that.

My research in github did lead me to this - https://github.com/influxdata/influxdb/pull/4387#r41890634. So this issue was apparently discussed but nothing came out of it.

This is my code -

// helper function to return new batch points for every batch
func getNewBatch(db, precision string) (influxdb.BatchPoints, error) {
  return influxdb.NewBatchPoints(influxdb.BatchPointsConfig{
    Database:  db,
    Precision: precision,
  })
}
func (i *influxDBOutput) Flush() error {
  // Flushing the current batch
  err := i.client.Write(i.batchPts)
  if err != nil {
    return err
  }
  // Creating new batch
  bp, err := getNewBatch(i.database, i.precision)
  if err != nil {
    return err
  }
  i.batchPts = bp
  return nil
}

The Flush() function is called from a different part of the code.

@agniva That code above looks like it’s working. Whats wrong with letting the GC clean up those client.BatchPoints structs? Is that causing significant overhead for you?

Technically there’s nothing wrong. But I was wondering if this is the ideal method to do this, as I do not see an example like this in the docs.

1 Like

Hi @agniva

The current idiomatic approach is to call influxdb.NewBatchPoints each time, which as you note will allocate a new struct implementing the models.BatchPoints interface. We’re not overly concerned about the garbage generated by this process since a batch of points usually contains thousands of individual points, which generates significantly more garbage during a write anyway.

We could add a Reset or similar method to the interface, which would allow the struct to be reused, but there aren’t any clear advantages to doing so.

Hope that helps!

2 Likes

Got it. Thanks for replying !