-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* generic eval update event first pass at alloc client update events * api/event client
- Loading branch information
1 parent
0c6118c
commit a0b10c6
Showing
14 changed files
with
265 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type Events struct { | ||
Index uint64 | ||
Events []Event | ||
} | ||
|
||
type Topic string | ||
|
||
type Event struct { | ||
Topic Topic | ||
Type string | ||
Key string | ||
FilterKeys []string | ||
Index uint64 | ||
Payload interface{} | ||
} | ||
|
||
func (e *Events) IsHeartBeat() bool { | ||
return e.Index == 0 && len(e.Events) == 0 | ||
} | ||
|
||
type EventStream struct { | ||
client *Client | ||
} | ||
|
||
func (c *Client) EventStream() *EventStream { | ||
return &EventStream{client: c} | ||
} | ||
|
||
func (e *EventStream) Stream(ctx context.Context, topics map[Topic][]string, index uint64, q *QueryOptions) (<-chan *Events, <-chan error) { | ||
|
||
errCh := make(chan error, 1) | ||
|
||
r, err := e.client.newRequest("GET", "/v1/event/stream") | ||
if err != nil { | ||
errCh <- err | ||
return nil, errCh | ||
} | ||
r.setQueryOptions(q) | ||
|
||
// Build topic query params | ||
for topic, keys := range topics { | ||
for _, k := range keys { | ||
r.params.Add("topic", fmt.Sprintf("%s:%s", topic, k)) | ||
} | ||
} | ||
|
||
_, resp, err := requireOK(e.client.doRequest(r)) | ||
|
||
if err != nil { | ||
errCh <- err | ||
return nil, errCh | ||
} | ||
|
||
eventsCh := make(chan *Events, 10) | ||
go func() { | ||
defer resp.Body.Close() | ||
|
||
dec := json.NewDecoder(resp.Body) | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
close(eventsCh) | ||
return | ||
default: | ||
} | ||
|
||
// Decode next newline delimited json of events | ||
var events Events | ||
if err := dec.Decode(&events); err != nil { | ||
close(eventsCh) | ||
errCh <- err | ||
return | ||
} | ||
if events.IsHeartBeat() { | ||
continue | ||
} | ||
|
||
eventsCh <- &events | ||
|
||
} | ||
}() | ||
|
||
return eventsCh, errCh | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEvent_Stream(t *testing.T) { | ||
t.Parallel() | ||
|
||
c, s := makeClient(t, nil, nil) | ||
defer s.Stop() | ||
|
||
// register job to generate events | ||
jobs := c.Jobs() | ||
job := testJob() | ||
resp2, _, err := jobs.Register(job, nil) | ||
require.Nil(t, err) | ||
require.NotNil(t, resp2) | ||
|
||
// build event stream request | ||
events := c.EventStream() | ||
q := &QueryOptions{} | ||
topics := map[Topic][]string{ | ||
"Eval": {"*"}, | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
streamCh, errCh := events.Stream(ctx, topics, 0, q) | ||
|
||
OUTER: | ||
for { | ||
select { | ||
case event := <-streamCh: | ||
require.Equal(t, len(event.Events), 1) | ||
require.Equal(t, "Eval", string(event.Events[0].Topic)) | ||
|
||
break OUTER | ||
case err := <-errCh: | ||
require.Fail(t, err.Error()) | ||
case <-time.After(5 * time.Second): | ||
require.Fail(t, "failed waiting for event stream event") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.