Skip to content

Instantly share code, notes, and snippets.

@cyborgshead
Created December 3, 2019 15:34
Show Gist options
  • Save cyborgshead/9ad09b5034c27dd14492b9d7d52ceb60 to your computer and use it in GitHub Desktop.
Save cyborgshead/9ad09b5034c27dd14492b9d7d52ceb60 to your computer and use it in GitHub Desktop.
// ___________________________________________________
ch := make(chan int64, 100000)
var wg sync.WaitGroup
var lock1 sync.Mutex
var lock2 sync.Mutex
wg.Add(int(cidsCount))
// the worker's function
f := func(i int64) {
defer wg.Done()
if inLinks, sortedCids, ok := ctx.GetSortedInLinks(types.CidNumber(i)); ok {
for _, cid := range sortedCids {
inLinksCount[i] += uint32(len(inLinks[cid]))
for acc := range inLinks[cid] {
lock2.Lock()
inLinksOuts = append(inLinksOuts, uint64(cid))
inLinksUsers = append(inLinksUsers, uint64(acc))
lock2.Unlock()
}
}
linksCount += uint64(inLinksCount[i])
}
if outLinks, ok := outLinks[types.CidNumber(i)]; ok {
for _, accs := range outLinks {
outLinksCount[i] += uint32(len(accs))
for acc := range accs {
lock1.Lock()
outLinksUsers = append(outLinksUsers, uint64(acc))
lock1.Unlock()
}
}
}
}
countWorkers := int64(math.Min(10000, float64(cidsCount)))
// here the workers start
for i:=int64(0); i < countWorkers; i++ {
go func() {
var cid int64
for {
cid = <- ch
f(cid)
}
}()
}
// data is added to the channel for workers
for i := int64(0); i < cidsCount; i++ {
ch <- i
}
// waiting for a while all workers will finish work
wg.Wait()
// ___________________________________________________
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment