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

Websocket #411

Merged
merged 4 commits into from
Sep 14, 2023
Merged

Websocket #411

merged 4 commits into from
Sep 14, 2023

Conversation

qdongxu
Copy link
Contributor

@qdongxu qdongxu commented Sep 6, 2023

No description provided.

@codecov-commenter
Copy link

codecov-commenter commented Sep 7, 2023

Codecov Report

Patch coverage is 70.23% of modified lines.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Files Changed Coverage
conf/conf.go ø
metric/prometheus.go 0.00%
probe/websocket/ws.go 71.95%

📢 Thoughts on this report? Let us know!.

@qdongxu
Copy link
Contributor Author

qdongxu commented Sep 7, 2023

This PR is ready for review.

notify/notify.go Outdated
@@ -50,6 +51,7 @@ type Config struct {
Sms []sms.NotifyConfig `yaml:"sms,omitempty" json:"sms,omitempty" jsonschema:"title=SMS Notification,description=SMS Notification Configuration"`
Teams []teams.NotifyConfig `yaml:"teams,omitempty" json:"teams,omitempty" jsonschema:"title=Teams Notification,description=Teams Notification Configuration"`
Shell []shell.NotifyConfig `yaml:"shell,omitempty" json:"shell,omitempty" jsonschema:"title=Shell Notification,description=Shell Notification Configuration"`
WebSocket []websocket.WebSocket `yaml:"webSocket,omitempty" json:"webSocket,omitempty" jsonschema:"title=WebSocket Notification,description=WebSocket Notification Configuration"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to align with other items and the documentation.

Suggested change
WebSocket []websocket.WebSocket `yaml:"webSocket,omitempty" json:"webSocket,omitempty" jsonschema:"title=WebSocket Notification,description=WebSocket Notification Configuration"`
WebSocket []websocket.WebSocket `yaml:"websocket,omitempty" json:"websocket,omitempty" jsonschema:"title=WebSocket Notification,description=WebSocket Notification Configuration"`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this line. This was a wrong copy/paste. websocket is a probe, not a notify module.

}

begin := time.Now()
remaining := new(time.Duration)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why declare remaining as a pointer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

docs/Manual.md Outdated
Comment on lines 859 to 864
```yaml
websocket:
name: asr-server
url: wss://example.com/asr/
timeout: 5s
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Conf, websocket is an array []websocket.WebSocket. And in examples above, http, tcp use array yaml format. Should this example update to array format?

websocket:
- name: ...
  ...
- name: ...
  ...

In WebSocket struct, there are field of proxy and headers, could you please add some example to them? Please make sure every parameter in the configuration has their own example, so users know how to use it properly. Please check 1.2.2 Complete Configuration of http.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

udpated.

"github.com/megaease/easeprobe/probe/base"
)

type WebSocket struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments to exportable struct and methods in this file. Websocket, Config, DoProbe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.


// doing nothing else but trigger the read message loop to receive the
// Pong and Close messages from the server. exit after ws.Close()
wg := sync.WaitGroup{}
Copy link
Contributor

@suchen-sci suchen-sci Sep 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a little confused about this WaitGroup here. wg should be used to wait the goroutine is finished. Then maybe do some checking after. But in this case, pingPongChan is used to do the waiting. So, maybe remove this wg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally the wg.Wait is unnecessary. the goroutine will quit shortly. In case of any extreme condition, the ws connection hangs, doProbe will hang too and not return, to avoid goroutine leak. I was a little knotty when adding the wg. Wait.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

remaining = h.ProbeTimeout - time.Now().Sub(begin)
err = ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(remaining))
if err != nil {
ws.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ws.Close is called before with defer ws.Close(), so no need to call it again. And since ws.Close is called in defer, then the goroutine before will return too. So, no goroutine leak.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

}
}()

remaining = h.ProbeTimeout - time.Now().Sub(begin)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use time.Since instead of time.Now().Sub()...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use time.Since instead of time.Now().Sub (S1012)go-staticcheck

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

return false, "ping timeout"
case <-pingPongChan:
remaining = h.ProbeTimeout - time.Now().Sub(begin)
closeCode := bytes.NewBufferString(fmt.Sprintf("%d", websocket.CloseNormalClosure))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use websocket.FormatCloseMessage() to format close message. FormatCloseMessage treat CloseNormalClosure code as uint16 with two bytes. Here, it is a four byte string...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

@suchen-sci suchen-sci merged commit 27d8368 into megaease:main Sep 14, 2023
6 checks passed
@qdongxu qdongxu deleted the websocket branch September 14, 2023 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants