This repository was archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathstream.go
More file actions
114 lines (96 loc) · 3.26 KB
/
Copy pathstream.go
File metadata and controls
114 lines (96 loc) · 3.26 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package stream
import (
"github.com/fallenstedt/twitter-stream/httpclient"
"net/http"
"net/url"
)
type (
// UnmarshalHook is a function that will unmarshal json.
UnmarshalHook func([]byte) (interface{}, error)
// IStream is the interface that the stream struct implements.
IStream interface {
StartStream(queryParams *url.Values) error
StopStream()
GetMessages() <-chan StreamMessage
SetUnmarshalHook(hook UnmarshalHook)
}
// StreamMessage is the message that is sent from the messages channel.
StreamMessage struct {
Data interface{}
Err error
}
// Stream is the struct that manages a long running TCP connection with Twitter.
// It accepts an 'unamarshalHook' which allows you to unmarshal json in a thread-safe manner.
// It is highly encouraged to set a unmarshal hook before starting a stream. Unmarshaling json
// in a separate goroutine is not recommended because the Go bytes.Buffer is not goroutine safe.
Stream struct {
unmarshalHook UnmarshalHook
messages chan StreamMessage
httpClient httpclient.IHttpClient
done chan struct{}
reader IStreamResponseBodyReader
}
)
// NewStream creates an instance of `Stream`. This is used to manage the stream with Twitter.
func NewStream(httpClient httpclient.IHttpClient, reader IStreamResponseBodyReader) IStream {
return &Stream{
unmarshalHook: func(bytes []byte) (interface{}, error) {
return bytes, nil
},
messages: make(chan StreamMessage),
done: make(chan struct{}),
reader: reader,
httpClient: httpClient,
}
}
// SetUnmarshalHook sets the function that unmarshals json. It is highly encouraged
// that you unmarshal json with this hook to promote thread-safety. Go's bytes.Buffer is not
// thread safe and can result in panics when a bytes.Buffer is shared across goroutines.
func (s *Stream) SetUnmarshalHook(hook UnmarshalHook) {
s.unmarshalHook = hook
}
// GetMessages returns the read-only messages channel
func (s *Stream) GetMessages() <-chan StreamMessage {
return s.messages
}
// StopStream sends a close signal to stop the stream of tweets.
func (s *Stream) StopStream() {
close(s.done)
}
// StartStream makes an HTTP GET request to twitter and starts streaming tweets to the Messages channel.
// Accepts query params described in GET /2/tweets/search/stream to expand the payload that is returned. Query params string must begin with a ?.
// See available query params here https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream.
// See an example here: https://developer.twitter.com/en/docs/twitter-api/expansions.
func (s *Stream) StartStream(optionalQueryParams *url.Values) error {
res, err := s.httpClient.GetSearchStream(optionalQueryParams)
if err != nil {
return err
}
s.reader.setStreamResponseBody(res.Body)
go s.streamMessages(res)
return nil
}
func (s *Stream) streamMessages(res *http.Response) {
defer res.Body.Close()
defer close(s.messages)
for !stopped(s.done) {
b, err := s.reader.readNext()
if err != nil {
s.messages <- StreamMessage{
Data: nil,
Err: err,
}
s.StopStream()
break
}
if len(b) == 0 {
// empty keep-alive
continue
}
data, err := s.unmarshalHook(b)
s.messages <- StreamMessage{
Data: data,
Err: err,
}
}
}