-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss_feed_reader1.go
99 lines (84 loc) · 2.03 KB
/
rss_feed_reader1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"math/rand"
"time"
)
type Item struct {
Title, Channel, GUID string // a subset of RSS fields
}
type Fetcher interface {
Fetch() (items []Item, next time.Time, err error)
}
// fetches Items from domain
func Fetch(domain string) Fetcher {
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
}
type Subscription interface {
Updates() <-chan Item // stream of Items
Close() error // shuts down the stream
}
// sub implements the Subscription interface.
type sub struct {
fetcher Fetcher // fetches items
updates chan Item // delivers items to the user
closed bool
err error
}
// converts Fetches to a stream
func Subscribe(fetcher Fetcher) Subscription {
s := &sub{
fetcher: fetcher,
updates: make(chan Item), // for Updates
}
go s.loop()
return s
}
// loop fetches items using s.fetcher and sends them
// on s.updates. loop exits when s.Close is called.
func (s *sub) loop() {
for {
if s.closed { // date race!
close(s.updates)
return
}
items, next, err := s.fetcher.Fetch()
if err != nil {
s.err = err // data race!
time.Sleep(10 * time.Second) // sleeping
continue
}
for _, item := range items {
s.updates <- item // block!
}
if now := time.Now(); next.After(now) {
time.Sleep(next.Sub(now)) // sleeping
}
}
}
func (s *sub) Updates() <-chan Item {
return s.updates
}
func (s *sub) Close() error {
s.closed = true // data race!
return s.err // data race!
}
// merges several streams
func Merge(subs ...Subscription) Subscription {
}
func main() {
// Subscribe to some feeds, and create a merged update stream.
merged := Merge(
Subscribe(Fetch("blog.golang.org")),
Subscribe(Fetch("googleblog.blogspot.com")),
Subscribe(Fetch("googledevelopers.blogspot.com")))
// Close the subscriptions after some time.
time.AfterFunc(3*time.Second, func() {
fmt.Println("closed:", merged.Close())
})
// Print the stream.
for it := range merged.Updates() {
fmt.Println(it.Channel, it.Title)
}
panic("show me the stacks")
}