-
Notifications
You must be signed in to change notification settings - Fork 112
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 2 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 |
---|---|---|
|
@@ -45,6 +45,14 @@ type Handler struct { | |
log log.Logger | ||
} | ||
|
||
// Instance functions that telemetry handler instance needs to implement | ||
edwardmack marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type Instance interface { | ||
AddConnections(conns []*genesis.TelemetryEndpoint) | ||
SendMessage(msg *Message) error | ||
startListening() | ||
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. why the private method? 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'm trying to control who can start the listener, this is called when the instance is created. |
||
KillInstance() | ||
} | ||
|
||
// KeyValue object to hold key value pairs used in telemetry messages | ||
type KeyValue struct { | ||
key string | ||
|
@@ -53,11 +61,11 @@ type KeyValue struct { | |
|
||
var ( | ||
once sync.Once | ||
handlerInstance *Handler | ||
handlerInstance Instance | ||
) | ||
|
||
// GetInstance singleton pattern to for accessing TelemetryHandler | ||
func GetInstance() *Handler { //nolint | ||
func GetInstance() Instance { //nolint | ||
if handlerInstance == nil { | ||
once.Do( | ||
func() { | ||
|
@@ -134,6 +142,11 @@ func (h *Handler) startListening() { | |
} | ||
} | ||
|
||
// KillInstance replaces current telemetry handler instance with no op telemetry handler instance | ||
func (h *Handler) KillInstance() { | ||
handlerInstance = &NoopHandler{} | ||
} | ||
|
||
type response struct { | ||
ID int `json:"id"` | ||
Payload map[string]interface{} `json:"payload"` | ||
|
@@ -152,3 +165,20 @@ 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) {} | ||
|
||
// KillInstance no op for telemetry kill instance function | ||
func (h *NoopHandler) KillInstance() {} |
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 { | ||
GetInstance().KillInstance() | ||
} | ||
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.
it would be nicer to have
GetInstance()
return aNoopInstance
instead of havingKillInstance()
, you can add a global package var to thetelemetry
package and use that inGetInstance()
, egtelemetry.Enabled = cfg.Global.NoTelemetry
, then inGetInstance()
check for theEnabled
varThere 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.
Yes, this is nicer, I've updated.