-
Notifications
You must be signed in to change notification settings - Fork 116
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
fix (dot/telemetry): NoTelemetry flag stops telemetry #1660
Changes from 6 commits
5b92145
6c08d7c
38b5d31
94dabef
be29889
cd69148
bd4000f
adf20bb
c1bfb11
2839806
6aa4400
d48501b
6e89d83
160bb40
a25deac
204dfe9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,13 @@ type Handler struct { | |
sendMessageTimeout time.Duration | ||
} | ||
|
||
// Instance interface that telemetry handler instance needs to implement | ||
type Instance interface { | ||
AddConnections(conns []*genesis.TelemetryEndpoint) | ||
SendMessage(msg *Message) error | ||
startListening() | ||
} | ||
|
||
// KeyValue object to hold key value pairs used in telemetry messages | ||
type KeyValue struct { | ||
key string | ||
|
@@ -54,13 +61,16 @@ type KeyValue struct { | |
|
||
var ( | ||
once sync.Once | ||
handlerInstance *Handler | ||
handlerInstance Instance | ||
|
||
// Enabled flag to determine if telemetry is enabled | ||
Enabled = true // enabled by default | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to disable telemetry from outside the package (i.e. in node.go line 343). I'm following suggestion by @noot above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I think this is fine, we could to create some There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup that works for me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good, I believe I've implemented it correctly now. |
||
) | ||
|
||
const defaultMessageTimeout = time.Second | ||
|
||
// GetInstance singleton pattern to for accessing TelemetryHandler | ||
func GetInstance() *Handler { //nolint | ||
func GetInstance() Instance { | ||
if handlerInstance == nil { | ||
once.Do( | ||
func() { | ||
|
@@ -72,6 +82,10 @@ func GetInstance() *Handler { //nolint | |
go handlerInstance.startListening() | ||
}) | ||
} | ||
if !Enabled { | ||
return &NoopHandler{} | ||
} | ||
|
||
return handlerInstance | ||
} | ||
|
||
|
@@ -158,3 +172,17 @@ func msgToBytes(message Message) []byte { | |
} | ||
return resB | ||
} | ||
|
||
// NoopHandler struct no op handling (ignoring) telemetry messages | ||
type NoopHandler struct { | ||
} | ||
|
||
func (h *NoopHandler) startListening() {} | ||
|
||
// SendMessage no op for telemetry send message function | ||
func (h *NoopHandler) SendMessage(msg *Message) error { | ||
return nil | ||
} | ||
|
||
// AddConnections no op for telemetry add connections function | ||
func (h *NoopHandler) AddConnections(conns []*genesis.TelemetryEndpoint) {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,6 +139,42 @@ func TestListenerConcurrency(t *testing.T) { | |
} | ||
} | ||
|
||
func TestKillInstance(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
const qty = 1000 | ||
var wg sync.WaitGroup | ||
wg.Add(qty) | ||
|
||
resultCh = make(chan []byte) | ||
for i := 0; i < qty; i++ { | ||
if i == qty/2 { | ||
Enabled = false | ||
} | ||
go func() { | ||
GetInstance().SendMessage(NewTelemetryMessage( | ||
NewKeyValue("best", "hash"), | ||
NewKeyValue("height", big.NewInt(2)), | ||
NewKeyValue("msg", "block.import"), | ||
NewKeyValue("origin", "NetworkInitialSync"))) | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
counter := 0 | ||
tk := time.NewTicker(time.Second * 2) | ||
main: | ||
for { | ||
select { | ||
case <-tk.C: | ||
break main | ||
case <-resultCh: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When this case will be reached? The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TestMain starts a web server that has a HandleFunc listen which reads websocket messages and passes them to |
||
counter++ | ||
} | ||
} | ||
tk.Stop() | ||
|
||
require.LessOrEqual(t, counter, qty/2) | ||
} | ||
|
||
func listen(w http.ResponseWriter, r *http.Request) { | ||
c, err := upgrader.Upgrade(w, r, nil) | ||
if err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the private method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to control who can start the listener, this is called when the instance is created.