Skip to content

Instantly share code, notes, and snippets.

@eriknelson
Last active July 8, 2017 02:47
Show Gist options
  • Save eriknelson/3d2980324811db122860554e16fd88b3 to your computer and use it in GitHub Desktop.
Save eriknelson/3d2980324811db122860554e16fd88b3 to your computer and use it in GitHub Desktop.
listprocessing
package main
import (
"fmt"
"regexp"
"sync"
)
func _totallist() []string {
return []string{
"test-good-apb",
"test-bad-apb",
"test-derp-apb",
"test-override-apb",
"bad-foo-apb",
"bad-bar-apb",
"thing-apb",
}
}
func _whitelist() []string {
return []string{
"test.*-apb",
"thing-apb",
}
}
func _blacklist() []string {
return []string{
"test-override-apb",
"bad-.*-apb",
}
}
func main() {
fmt.Println("derp")
var whiteMatches, blackMatches []string
fmt.Println("Kicking off...")
whiteChan := genMatches(_whitelist(), _totallist(), "whitelist")
blackChan := genMatches(_blacklist(), _totallist(), "blacklist")
whiteMatches = <-whiteChan
blackMatches = <-blackChan
fmt.Println("============================================================")
fmt.Println("whiteMatches:")
for _, m := range whiteMatches {
fmt.Printf("-> %s\n", m)
}
fmt.Println("blackMatches:")
for _, m := range blackMatches {
fmt.Printf("-> %s\n", m)
}
fmt.Println("============================================================")
}
func genMatches(regexlist []string, totallist []string, label string) <-chan []string {
fmt.Printf("Generating matches. Label -> [ %s ]\n", label)
matchChunksChan := make(chan []string)
var wg sync.WaitGroup
wg.Add(len(regexlist))
// Produce set of matches for each regex against each item in totallist
// Run each regex against totallist concurrently
for _, regex := range regexlist {
go func(regex string) {
matchChunk := []string{}
for _, testStr := range totallist {
if ok, _ := regexp.MatchString(regex, testStr); ok {
matchChunk = append(matchChunk, testStr)
}
}
matchChunksChan <- matchChunk
wg.Done()
}(regex)
}
// Join workers and close chunks channel when finished
go func() {
wg.Wait()
close(matchChunksChan)
}()
return reduceChunks(matchChunksChan)
}
func reduceChunks(chunks <-chan []string) <-chan []string {
// Fan-in chunks, reduce to single set of matches
out := make(chan []string)
go func() {
uniqMatches := make(map[string]bool)
for chunk := range chunks {
for _, match := range chunk {
uniqMatches[match] = true
}
}
out <- toSlice(uniqMatches)
close(out)
}()
return out
}
func toSlice(m map[string]bool) []string {
s := make([]string, len(m))
i := 0
for k, _ := range m {
s[i] = k
i++
}
return s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment