forked from cloudfoundry-attic/receptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_source.go
171 lines (132 loc) · 3.64 KB
/
event_source.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package receptor
import (
"encoding/json"
"errors"
"fmt"
"io"
"github.com/vito/go-sse/sse"
)
var ErrUnrecognizedEventType = errors.New("unrecognized event type")
var ErrSourceClosed = errors.New("source closed")
type invalidPayloadError struct {
jsonErr error
}
func NewInvalidPayloadError(jsonErr error) error {
return invalidPayloadError{jsonErr: jsonErr}
}
func (e invalidPayloadError) Error() string {
return fmt.Sprintf("invalid json payload: %s", e.jsonErr.Error())
}
type rawEventSourceError struct {
rawError error
}
func NewRawEventSourceError(rawError error) error {
return rawEventSourceError{rawError: rawError}
}
func (e rawEventSourceError) Error() string {
return fmt.Sprintf("raw event source error: %s", e.rawError.Error())
}
type closeError struct {
err error
}
func NewCloseError(err error) error {
return closeError{err: err}
}
func (e closeError) Error() string {
return fmt.Sprintf("error closing raw source: %s", e.err.Error())
}
//go:generate counterfeiter -o fake_receptor/fake_event_source.go . EventSource
// EventSource provides sequential access to a stream of events.
type EventSource interface {
// Next reads the next event from the source. If the connection is lost, it
// automatically reconnects.
//
// If the end of the stream is reached cleanly (which should actually never
// happen), io.EOF is returned. If called after or during Close,
// ErrSourceClosed is returned.
Next() (Event, error)
// Close releases the underlying response, interrupts any in-flight Next, and
// prevents further calls to Next.
Close() error
}
//go:generate counterfeiter -o fake_receptor/fake_raw_event_source.go . RawEventSource
type RawEventSource interface {
Next() (sse.Event, error)
Close() error
}
type eventSource struct {
rawEventSource RawEventSource
}
func NewEventSource(raw RawEventSource) EventSource {
return &eventSource{
rawEventSource: raw,
}
}
func (e *eventSource) Next() (Event, error) {
rawEvent, err := e.rawEventSource.Next()
if err != nil {
switch err {
case io.EOF:
return nil, err
case sse.ErrSourceClosed:
return nil, ErrSourceClosed
default:
return nil, NewRawEventSourceError(err)
}
}
return parseRawEvent(rawEvent)
}
func (e *eventSource) Close() error {
err := e.rawEventSource.Close()
if err != nil {
return NewCloseError(err)
}
return nil
}
func parseRawEvent(rawEvent sse.Event) (Event, error) {
switch EventType(rawEvent.Name) {
case EventTypeDesiredLRPCreated:
var event DesiredLRPCreatedEvent
err := json.Unmarshal(rawEvent.Data, &event)
if err != nil {
return nil, NewInvalidPayloadError(err)
}
return event, nil
case EventTypeDesiredLRPChanged:
var event DesiredLRPChangedEvent
err := json.Unmarshal(rawEvent.Data, &event)
if err != nil {
return nil, NewInvalidPayloadError(err)
}
return event, nil
case EventTypeDesiredLRPRemoved:
var event DesiredLRPRemovedEvent
err := json.Unmarshal(rawEvent.Data, &event)
if err != nil {
return nil, NewInvalidPayloadError(err)
}
return event, nil
case EventTypeActualLRPCreated:
var event ActualLRPCreatedEvent
err := json.Unmarshal(rawEvent.Data, &event)
if err != nil {
return nil, NewInvalidPayloadError(err)
}
return event, nil
case EventTypeActualLRPChanged:
var event ActualLRPChangedEvent
err := json.Unmarshal(rawEvent.Data, &event)
if err != nil {
return nil, NewInvalidPayloadError(err)
}
return event, nil
case EventTypeActualLRPRemoved:
var event ActualLRPRemovedEvent
err := json.Unmarshal(rawEvent.Data, &event)
if err != nil {
return nil, NewInvalidPayloadError(err)
}
return event, nil
}
return nil, ErrUnrecognizedEventType
}