-
Notifications
You must be signed in to change notification settings - Fork 903
websocket: Initial refactor for websocket handling to support context propagation #2066
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
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
360d0e5
Refactor websocket handling to support context propagation
4be8b4b
linter/AI: nits and fixes
cce08e7
linter: fixes again
86d218c
fix test
0cb3876
Merge branch 'master' into websock_read_ctx
98c0817
instead of a read function just expose the read only channel field li…
a87e39f
Merge branch 'master' into websock_read_ctx
6b0a3b5
Merge branch 'master' into websock_read_ctx
f380808
glorious: nits
5eacbae
Merge branch 'master' into websock_read_ctx
88e48a1
glorious: nits
c8fb278
rm jank for finding issue
f6dd7c3
linter: fix
fc7df14
glorious: more nits
770d7a6
Add todo for context removal
7d6e595
Merge branch 'master' into websock_read_ctx
cabcd83
Merge branch 'master' into websock_read_ctx
f21e02d
Implement context freezing and thawing functions; update Payload to u…
7625f60
linter: fix
7185eb9
linter: fix again
2eb5f6a
Merge branch 'master' into websock_read_ctx
e42ae00
refactor: simplify FrozenContext structure and update related functions
bab00e0
Update common/common.go
shazbert 1680819
Update common/common.go
shazbert 239d160
glorious: removal of check
07bba2a
thrasher: rm unused error return
1e63153
Update exchange/websocket/manager.go
shazbert 9f5fbda
rm: check for nil datahandler
ca24dc1
gk: rm rpcContextToLongLivedSession
db647bf
gk: rn package
abd83f7
thrasher-: nits
bd8cd65
Merge branch 'master' into websock_read_ctx
8a5069f
what a silly billy
478b2eb
Merge branch 'master' into websock_read_ctx
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,48 @@ | ||
| package stream | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/thrasher-corp/gocryptotrader/common" | ||
| ) | ||
|
|
||
| var errChannelBufferFull = errors.New("channel buffer is full") | ||
|
|
||
gbjk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Relay defines a channel relay for messages | ||
| type Relay struct { | ||
| C <-chan Payload | ||
gbjk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| comm chan Payload | ||
| } | ||
|
|
||
| // Payload represents a relayed message with a context | ||
| type Payload struct { | ||
| Ctx common.FrozenContext | ||
| Data any | ||
| } | ||
|
|
||
| // NewRelay creates a new Relay instance with a specified buffer size | ||
| func NewRelay(buffer uint) *Relay { | ||
| if buffer == 0 { | ||
| panic("buffer size must be greater than 0") | ||
| } | ||
| comm := make(chan Payload, buffer) | ||
| return &Relay{comm: comm, C: comm} | ||
| } | ||
|
|
||
| // Send sends a message to the channel receiver | ||
| // This is non-blocking and returns an error if the channel buffer is full | ||
| func (r *Relay) Send(ctx context.Context, data any) error { | ||
| select { | ||
| case r.comm <- Payload{Ctx: common.FreezeContext(ctx), Data: data}: | ||
| return nil | ||
| default: | ||
| return fmt.Errorf("%w: failed to relay <%T>", errChannelBufferFull, data) | ||
| } | ||
| } | ||
|
|
||
| // Close closes the relay channel | ||
| func (r *Relay) Close() { | ||
gloriousCode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| close(r.comm) | ||
| } | ||
This file contains hidden or 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,43 @@ | ||
| package stream | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestNewRelay(t *testing.T) { | ||
| t.Parallel() | ||
| assert.Panics(t, func() { NewRelay(0) }, "buffer size should be greater than 0") | ||
| r := NewRelay(5) | ||
| require.NotNil(t, r) | ||
| assert.Equal(t, 5, cap(r.comm)) | ||
| } | ||
|
|
||
| func TestSend(t *testing.T) { | ||
| t.Parallel() | ||
| r := NewRelay(1) | ||
| require.NotNil(t, r) | ||
| assert.NoError(t, r.Send(t.Context(), "test")) | ||
| assert.ErrorIs(t, r.Send(t.Context(), "overflow"), errChannelBufferFull) | ||
| } | ||
|
|
||
| func TestRead(t *testing.T) { | ||
| t.Parallel() | ||
| r := NewRelay(1) | ||
| require.NotNil(t, r) | ||
| require.Empty(t, r.C) | ||
| assert.NoError(t, r.Send(t.Context(), "test")) | ||
| require.Len(t, r.C, 1) | ||
| assert.Equal(t, "test", (<-r.C).Data) | ||
| } | ||
|
|
||
| func TestClose(t *testing.T) { | ||
| t.Parallel() | ||
| r := NewRelay(1) | ||
| require.NotNil(t, r) | ||
| r.Close() | ||
| _, ok := <-r.C | ||
| assert.False(t, ok) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.