4

[Golang] Goroutines Poll Web Feeds

 2 years ago
source link: http://siongui.github.io/2015/03/10/goroutine-poll-web-feeds/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

This post shows how to periodically poll (fetch by http get) web feeds with goroutines.

Souce Code

get.go | repository | view raw

// http://tour.golang.org/concurrency/1
// http://golang.org/pkg/net/http/
package main

import (
	"time"
	"net/http"
	"io/ioutil"
	"fmt"
)

var pollingInterval = 1 * time.Minute

func fetch(url string) {
	resp, err := http.Get(url)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()

	body, err2 := ioutil.ReadAll(resp.Body)
	if err2 != nil {
		fmt.Println(err2)
		return
	}
	fmt.Println(body[:10])
}

func getFeed(url string) {
	for {
		fetch(url)
		time.Sleep(pollingInterval)
	}
}

func poll(feedUrls []string) {
	for _, url := range feedUrls {
		go getFeed(url)
	}
}

func main() {
	feedUrls := []string{
	"https://news.ycombinator.com/rss",
	"http://www.csdn.net/article/rss_lastnews",
	"http://www.solidot.org/index.rss",
	"http://blog.jobbole.com/feed/"}

	poll(feedUrls)

	// block here
	for { select {} }
}

Tested on: Ubuntu Linux 14.10, Go 1.4.


References:

[1]Concurrency - A Tour of Go

[2]http - The Go Programming Language

[3]go - Golang: Is it possible to capture a Ctrl+C signal and run a cleanup function, in a "defer" fashion? - Stack Overflow

[6]How to run a custom function with timeout? : golang

[7]Non blocking way of notifying a goroutine, by many at once : golang


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK