all repos — honk @ aab77a5021bdc107fa264eb2ad054b3b58273dce

my fork of honk

get donks in parallel
Ted Unangst tedu@tedunangst.com
Sat, 20 Jan 2024 22:27:23 -0500
commit

aab77a5021bdc107fa264eb2ad054b3b58273dce

parent

74b8bee5d64de7897980eb8902a482588aa20d2b

1 files changed, 24 insertions(+), 4 deletions(-)

jump to
M activity.goactivity.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 +}