Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 1 | // Copyright 2010 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package main |
| 6 | |
| 7 | import ( |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 8 | "log" |
Rob Pike | f9489be | 2011-11-08 15:43:02 -0800 | [diff] [blame] | 9 | "net/http" |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | const ( |
Stefan Nilsson | 1de4931 | 2012-01-19 14:45:59 +1100 | [diff] [blame] | 14 | numPollers = 2 // number of Poller goroutines to launch |
| 15 | pollInterval = 60 * time.Second // how often to poll each URL |
| 16 | statusInterval = 10 * time.Second // how often to log status to stdout |
| 17 | errTimeout = 10 * time.Second // back-off timeout on error |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 18 | ) |
| 19 | |
| 20 | var urls = []string{ |
| 21 | "http://www.google.com/", |
| 22 | "http://golang.org/", |
| 23 | "http://blog.golang.org/", |
| 24 | } |
| 25 | |
| 26 | // State represents the last-known state of a URL. |
| 27 | type State struct { |
| 28 | url string |
| 29 | status string |
| 30 | } |
| 31 | |
| 32 | // StateMonitor maintains a map that stores the state of the URLs being |
| 33 | // polled, and prints the current state every updateInterval nanoseconds. |
| 34 | // It returns a chan State to which resource state should be sent. |
Stefan Nilsson | 1de4931 | 2012-01-19 14:45:59 +1100 | [diff] [blame] | 35 | func StateMonitor(updateInterval time.Duration) chan<- State { |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 36 | updates := make(chan State) |
| 37 | urlStatus := make(map[string]string) |
| 38 | ticker := time.NewTicker(updateInterval) |
| 39 | go func() { |
| 40 | for { |
| 41 | select { |
| 42 | case <-ticker.C: |
| 43 | logState(urlStatus) |
| 44 | case s := <-updates: |
| 45 | urlStatus[s.url] = s.status |
| 46 | } |
| 47 | } |
| 48 | }() |
| 49 | return updates |
| 50 | } |
| 51 | |
| 52 | // logState prints a state map. |
| 53 | func logState(s map[string]string) { |
Andrey Mirtchovski | f1af2ec | 2010-11-08 09:58:57 -0800 | [diff] [blame] | 54 | log.Println("Current state:") |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 55 | for k, v := range s { |
Andrey Mirtchovski | f1af2ec | 2010-11-08 09:58:57 -0800 | [diff] [blame] | 56 | log.Printf(" %s %s", k, v) |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | // Resource represents an HTTP URL to be polled by this program. |
| 61 | type Resource struct { |
| 62 | url string |
Stefan Nilsson | 1de4931 | 2012-01-19 14:45:59 +1100 | [diff] [blame] | 63 | errCount int |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Poll executes an HTTP HEAD request for url |
| 67 | // and returns the HTTP status string or an error string. |
| 68 | func (r *Resource) Poll() string { |
| 69 | resp, err := http.Head(r.url) |
| 70 | if err != nil { |
Andrey Mirtchovski | f1af2ec | 2010-11-08 09:58:57 -0800 | [diff] [blame] | 71 | log.Println("Error", r.url, err) |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 72 | r.errCount++ |
Russ Cox | 44526cd | 2011-11-01 22:06:05 -0400 | [diff] [blame] | 73 | return err.Error() |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 74 | } |
| 75 | r.errCount = 0 |
| 76 | return resp.Status |
| 77 | } |
| 78 | |
Shenghou Ma | 7bec1a6 | 2013-04-24 04:46:14 +0800 | [diff] [blame] | 79 | // Sleep sleeps for an appropriate interval (dependent on error state) |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 80 | // before sending the Resource to done. |
Stefan Nilsson | 1de4931 | 2012-01-19 14:45:59 +1100 | [diff] [blame] | 81 | func (r *Resource) Sleep(done chan<- *Resource) { |
| 82 | time.Sleep(pollInterval + errTimeout*time.Duration(r.errCount)) |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 83 | done <- r |
| 84 | } |
| 85 | |
| 86 | func Poller(in <-chan *Resource, out chan<- *Resource, status chan<- State) { |
| 87 | for r := range in { |
| 88 | s := r.Poll() |
| 89 | status <- State{r.url, s} |
| 90 | out <- r |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func main() { |
Stefan Nilsson | 1de4931 | 2012-01-19 14:45:59 +1100 | [diff] [blame] | 95 | // Create our input and output channels. |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 96 | pending, complete := make(chan *Resource), make(chan *Resource) |
| 97 | |
Stefan Nilsson | 1de4931 | 2012-01-19 14:45:59 +1100 | [diff] [blame] | 98 | // Launch the StateMonitor. |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 99 | status := StateMonitor(statusInterval) |
| 100 | |
Stefan Nilsson | 1de4931 | 2012-01-19 14:45:59 +1100 | [diff] [blame] | 101 | // Launch some Poller goroutines. |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 102 | for i := 0; i < numPollers; i++ { |
| 103 | go Poller(pending, complete, status) |
| 104 | } |
| 105 | |
Stefan Nilsson | 1de4931 | 2012-01-19 14:45:59 +1100 | [diff] [blame] | 106 | // Send some Resources to the pending queue. |
Andrew Gerrand | 71675c6 | 2010-06-30 16:56:30 +1000 | [diff] [blame] | 107 | go func() { |
| 108 | for _, url := range urls { |
| 109 | pending <- &Resource{url: url} |
| 110 | } |
| 111 | }() |
| 112 | |
| 113 | for r := range complete { |
| 114 | go r.Sleep(pending) |
| 115 | } |
| 116 | } |