Friday, May 10, 2024
HomeGolangAssist enhance go concurrency code - Code Assessment

Assist enhance go concurrency code – Code Assessment


This operate is to fetch some xml content material and reserve it. One difficulty i encounterd is that learn how to ship case s.updates ← pending[0]: (pending Merchandise) one by one. the code presently has an error of accessing index out of sure.

right here’s the hyperlink to the total code : Higher Go Playground

func(s *sub) loop(){
	var pending []Merchandise // appended by fetch; consumed by ship
	var subsequent time.Time
	var err error
	var seen = make(map[string]bool) // set of merchandise.GUIDS
	for{
		// var updates chan Merchandise
		var fetchDone chan fetchResult// if non-nil, fetch is working
		var fetchDelay time.Length // initally 0
		if now := time.Now(); subsequent.After(now) {
			fetchDelay = subsequent.Sub(now)
		}
		var startFetch <-chan time.Time
		if fetchDone == nil && len(pending) < maxPending {
			startFetch = time.After(fetchDelay)
		}
		choose{
		case errc := <-s.closing:
			errc <- err
			shut(s.updates)// tells receiver we're accomplished
			return
		case <-startFetch:
			fmt.Println("begin fetch")
			var fetched []Merchandise
			fetchDone = make(chan fetchResult, 1)
			go func(){
				fetched, subsequent, err = s.fetcher.Fetch()
				fetchDone <- fetchResult{fetched, subsequent, err}
			}()


		case s.updates <- pending[0]:
			fmt.Println("add pending")
      if len(pending) > 0 {
				pending = pending[1:]
			}
		case consequence := <-fetchDone:
			fmt.Println("fetch accomplished")
			fetchDone = nil
			if consequence.err != nil {
					subsequent = time.Now().Add(10 * time.Second)
					break
			}
			for _, merchandise := vary consequence.fetched {
					if !seen[item.Channel.Link] {
							pending = append(pending, merchandise)
							seen[item.Channel.Title] = true
					}
			}
		}
	
	}
}

Certain, I’d be glad to assist enhance your Go concurrency code. Might you please present the code you’re working with? Then, I can provide strategies for optimization or any enhancements primarily based in your particular necessities.

Howdy there,
The code within the hyperlink Higher Go Playground
The code is about fetching .xml knowledge on the internet, and retailer it, I wished to fetch 10 gadgets at a time. and retailer it one by one by passing to a channel named updates.
Thanks

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments