Last active
June 10, 2020 08:50
-
-
Save RicardoLinck/8f01a5c35e41c807ba32781ec969920b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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