This function is to fetch some xml content and save it. One issue i encounterd is that how to send case s.updates ← pending[0]: (pending Item) one at a time. the code currently has an error of accessing index out of bound.
here’s the link to the full code : Better Go Playground
func(s *sub) loop(){
var pending []Item // appended by fetch; consumed by send
var next time.Time
var err error
var seen = make(map[string]bool) // set of item.GUIDS
for{
// var updates chan Item
var fetchDone chan fetchResult// if non-nil, fetch is running
var fetchDelay time.Duration // initally 0
if now := time.Now(); next.After(now) {
fetchDelay = next.Sub(now)
}
var startFetch <-chan time.Time
if fetchDone == nil && len(pending) < maxPending {
startFetch = time.After(fetchDelay)
}
select{
case errc := <-s.closing:
errc <- err
close(s.updates)// tells receiver we're done
return
case <-startFetch:
fmt.Println("start fetch")
var fetched []Item
fetchDone = make(chan fetchResult, 1)
go func(){
fetched, next, err = s.fetcher.Fetch()
fetchDone <- fetchResult{fetched, next, err}
}()
case s.updates <- pending[0]:
fmt.Println("add pending")
if len(pending) > 0 {
pending = pending[1:]
}
case result := <-fetchDone:
fmt.Println("fetch done")
fetchDone = nil
if result.err != nil {
next = time.Now().Add(10 * time.Second)
break
}
for _, item := range result.fetched {
if !seen[item.Channel.Link] {
pending = append(pending, item)
seen[item.Channel.Title] = true
}
}
}
}
}