Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: fix slow client connection to empty event stream #10637

Merged
merged 2 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

BUG FIXES:
* cli: Fixed a bug where `quota status` and `namespace status` commands may panic if the CLI targets a pre-1.1.0 cluster
* Event stream: Fixed slow event stream connection initialisation not having any events yet [[GH-10636](https://github.com/hashicorp/nomad/issues/10636)]
drewbailey marked this conversation as resolved.
Show resolved Hide resolved

## 1.1.0 (May 18, 2021)

Expand Down
4 changes: 3 additions & 1 deletion nomad/stream/ndjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ type JsonStream struct {

// NewJsonStream creates a new json stream that will output Json structs
// to the passed output channel. The constructor starts a goroutine
// to begin heartbeating on its set interval.
// to begin heartbeating on its set interval and also sends an initial heartbeat
// to notify the client about the successful connection initialization.
func NewJsonStream(ctx context.Context, heartbeat time.Duration) *JsonStream {
s := &JsonStream{
ctx: ctx,
outCh: make(chan *structs.EventJson, 10),
heartbeatTick: time.NewTicker(heartbeat),
}

s.outCh <- JsonHeartbeat
go s.heartbeat()

return s
Expand Down
17 changes: 6 additions & 11 deletions nomad/stream/ndjson_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package stream

import (
"bytes"
"context"
"testing"
"time"
Expand All @@ -24,12 +23,12 @@ func TestJsonStream(t *testing.T) {

require.NoError(t, s.Send(testObj{Name: "test"}))

out1 := <-out
initialHeartbeat := <-out
require.Equal(t, []byte(`{}`), initialHeartbeat.Data)

var expected bytes.Buffer
expected.Write([]byte(`{"name":"test"}`))
testMessage1 := <-out
require.Equal(t, []byte(`{"name":"test"}`), testMessage1.Data)

require.Equal(t, expected.Bytes(), out1.Data)
select {
case msg := <-out:
require.Failf(t, "Did not expect another message", "%#v", msg)
Expand All @@ -38,12 +37,8 @@ func TestJsonStream(t *testing.T) {

require.NoError(t, s.Send(testObj{Name: "test2"}))

out2 := <-out
expected.Reset()

expected.Write([]byte(`{"name":"test2"}`))
require.Equal(t, expected.Bytes(), out2.Data)

testMessage2 := <-out
require.Equal(t, []byte(`{"name":"test2"}`), testMessage2.Data)
}

func TestJson_Send_After_Stop(t *testing.T) {
Expand Down