Skip to content

Instantly share code, notes, and snippets.

@RicardoLinck
Last active June 10, 2020 08:50
Show Gist options
  • Save RicardoLinck/8f01a5c35e41c807ba32781ec969920b to your computer and use it in GitHub Desktop.
Save RicardoLinck/8f01a5c35e41c807ba32781ec969920b to your computer and use it in GitHub Desktop.
package main
import "time"
type saveResult struct {
idsSaved []string
timestamp int64
}
func saveData(ic <-chan externalData) <-chan saveResult {
oc := make(chan saveResult)
go func() {
const batchSize = 7
batch := make([]string, 0)
for input := range ic {
if len(batch) < batchSize {
batch = append(batch, input.inputData.id)
} else {
oc <- persistBatch(batch)
batch = []string{input.inputData.id}
}
}
if len(batch) > 0 {
oc <- persistBatch(batch)
}
close(oc)
}()
return oc
}
func persistBatch(batch []string) saveResult {
return saveResult{batch, time.Now().UnixNano()}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment