get donks in parallel
Ted Unangst tedu@tedunangst.com
Sat, 20 Jan 2024 22:27:23 -0500
1 files changed,
24 insertions(+),
4 deletions(-)
jump to
M
activity.go
→
activity.go
@@ -28,6 +28,7 @@ "net/http"
"os" "regexp" "strings" + "sync" "time" "humungus.tedunangst.com/r/webs/gate"@@ -902,6 +903,7 @@ return nil
} numatts := 0 + var donkfns []func() *Donk procatt := func(att junk.Junk) { at, _ := att.GetString("type") mt, _ := att.GetString("mediaType")@@ -965,11 +967,10 @@ }
if preferorig && !localize { return } - donk := savedonk(u, name, desc, mt, localize) - if donk != nil { - xonk.Donks = append(xonk.Donks, donk) - } numatts++ + donkfns = append(donkfns, func() *Donk { + return savedonk(u, name, desc, mt, localize) + }) } if img, ok := obj.GetMap("image"); ok { procatt(img)@@ -997,6 +998,11 @@ ilog.Printf("attachment that wasn't map?")
continue } procatt(att) + } + } + for _, donk := range multiRun(donkfns) { + if donk != nil { + xonk.Donks = append(xonk.Donks, donk) } } proctag := func(tag junk.Junk) {@@ -2104,3 +2110,17 @@ elog.Printf("error updating honker: %s", err)
return } } + +func multiRun[R any](fns []func() R) []R { + results := make([]R, len(fns)) + var wg sync.WaitGroup + for i, fn := range fns { + wg.Add(1) + go func(i int, fn func() R) { + results[i] = fn() + wg.Done() + }(i, fn) + } + wg.Wait() + return results +}