Browse Source

fix: spider doesn't work, lru position out of range

master
OhYee 2 years ago
parent
commit
587ef1d8d9
Signed by: OhYee
GPG Key ID: 5A9E1F63ED274FBB
  1. 25
      cron/spider.go
  2. 15
      utils/lru/heap.go

25
cron/spider.go

@ -10,6 +10,7 @@ import ( @@ -10,6 +10,7 @@ import (
"github.com/OhYee/blotter/cron/spider"
"github.com/OhYee/blotter/output"
"github.com/OhYee/blotter/register"
pool "github.com/OhYee/blotter/utils/goroutine_pool"
)
func spiderSite(f friends.Friend, wg *sync.WaitGroup) {
@ -108,18 +109,18 @@ func Spider() { @@ -108,18 +109,18 @@ func Spider() {
if spiderURL == "" {
wg := &sync.WaitGroup{}
// fs, _ := friends.GetFriends()
// for _, f := range fs {
// if f.RSS == "" {
// continue
// }
// wg.Add(1)
// func(f friends.Friend, wg *sync.WaitGroup) {
// pool.Do(func() {
// spiderSite(f, wg)
// })
// }(f, wg)
// }
fs, _ := friends.GetFriends()
for _, f := range fs {
if f.RSS == "" {
continue
}
wg.Add(1)
func(f friends.Friend, wg *sync.WaitGroup) {
pool.Do(func() {
spiderSite(f, wg)
})
}(f, wg)
}
wg.Wait()
sortFriends()
} else {

15
utils/lru/heap.go

@ -2,6 +2,7 @@ package lru @@ -2,6 +2,7 @@ package lru
import (
"container/heap"
"math/rand"
)
type keyValue struct {
@ -25,12 +26,17 @@ func (h keyValueHeap) Swap(i, j int) { @@ -25,12 +26,17 @@ func (h keyValueHeap) Swap(i, j int) {
h[i].pos = i
h[j].pos = j
}
func (h keyValueHeap) Less(i, j int) bool { return h[i].value < h[j].value }
func (h *keyValueHeap) Push(item interface{}) { *h = append(*h, item.(*keyValue)) }
func (h keyValueHeap) Less(i, j int) bool { return h[i].value < h[j].value }
func (h *keyValueHeap) Push(item interface{}) {
*h = append(*h, item.(*keyValue))
l := h.Len()
(*h)[l-1].pos = l - 1
}
func (h *keyValueHeap) Pop() interface{} {
l := h.Len()
t := (*h)[l-1]
(*h) = (*h)[:l-1]
t.pos = -1
return t
}
@ -96,6 +102,11 @@ func (h *Heap) PopUntil(value int64) []string { @@ -96,6 +102,11 @@ func (h *Heap) PopUntil(value int64) []string {
}
func (h *Heap) gc() {
// 10% 概率执行一次 gc
if rand.Float64() > 0.1 {
return
}
temp := h.m
h.m = make(map[string]*keyValue)
for k, v := range temp {

Loading…
Cancel
Save