Repository navigation
Support for request/response signaling messages #984
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
9 commits
Select commit
Hold shift + click to select a range
e730745
Handle RequestResponse signal messages
ladvoc d17a9e9
Track pending signal requests by request id
ladvoc 4b03238
Forward unmatched request responses to the engine handler
ladvoc a024206
Reserve request ids under the pending request lock
ladvoc c7644f7
Deliver typed responses to pending requests
ladvoc 6cdebae
Add default timeout helper for signal requests
ladvoc 7308925
Abort pending requests on full reconnect
ladvoc 5837d59
Move non-struct methods to the end
ladvoc 816142d
Keep engine methods together
ladvoc 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Copyright 2026 LiveKit, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package lksdk | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/livekit/protocol/livekit" | ||
| "google.golang.org/protobuf/proto" | ||
| ) | ||
|
|
||
| // SignalRequestError is returned when the server rejects a signal request. | ||
| type SignalRequestError struct { | ||
| RequestID uint32 | ||
| Reason livekit.RequestResponse_Reason | ||
| Message string | ||
| } | ||
|
|
||
| func (e *SignalRequestError) Error() string { | ||
| return fmt.Sprintf("signal request rejected (%s): %s", e.Reason, e.Message) | ||
| } | ||
|
|
||
| // pendingRequest reserves the response to a signal request that carries a request id: create it | ||
| // before sending the request, put ID() in the request_id field, then Await the response. The | ||
| // engine owns the registry these are tracked in, see (*RTCEngine).newPendingRequest. | ||
| type pendingRequest struct { | ||
| engine *RTCEngine | ||
| id uint32 | ||
| ch chan proto.Message | ||
| } | ||
|
|
||
| func (p *pendingRequest) ID() uint32 { | ||
| return p.id | ||
| } | ||
|
|
||
| // Await blocks until a response arrives, ctx is done, or the engine is closed. It has no timeout | ||
| // of its own, see withDefaultTimeout. A RequestResponse with a reason other than OK is returned | ||
| // as a *SignalRequestError. | ||
| func (p *pendingRequest) Await(ctx context.Context) (proto.Message, error) { | ||
| defer p.engine.removePendingRequest(p.id) | ||
|
|
||
| select { | ||
| case res, ok := <-p.ch: | ||
| if !ok { | ||
| return nil, ErrAborted | ||
| } | ||
| if rr, isRequestResponse := res.(*livekit.RequestResponse); isRequestResponse && rr.GetReason() != livekit.RequestResponse_OK { | ||
| return nil, &SignalRequestError{ | ||
| RequestID: rr.GetRequestId(), | ||
| Reason: rr.GetReason(), | ||
| Message: rr.GetMessage(), | ||
| } | ||
| } | ||
| return res, nil | ||
|
|
||
| case <-ctx.Done(): | ||
| return nil, ctx.Err() | ||
| } | ||
| } | ||
|
|
||
| // awaitResponse is Await for requests whose success is reported by a dedicated message type. | ||
| func awaitResponse[T proto.Message](ctx context.Context, p *pendingRequest) (T, error) { | ||
| var zero T | ||
|
|
||
| res, err := p.Await(ctx) | ||
| if err != nil { | ||
| return zero, err | ||
| } | ||
| typed, ok := res.(T) | ||
| if !ok { | ||
| return zero, fmt.Errorf("unexpected response %T to signal request %d", res, p.id) | ||
| } | ||
| return typed, nil | ||
| } | ||
|
|
||
| // withDefaultTimeout applies deadline d to ctx when it has none. A deadline the caller set is | ||
| // never shortened. The returned cancel must be called. | ||
| func withDefaultTimeout(ctx context.Context, d time.Duration) (context.Context, context.CancelFunc) { | ||
| if _, ok := ctx.Deadline(); ok { | ||
| return ctx, func() {} | ||
| } | ||
| return context.WithTimeout(ctx, d) | ||
| } | ||
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.
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.
:nit: group all the methods of
pendingRequestand all method of engine (if it can be moved to engine.go, that would be great too) together. Would be good to avoid interspersed methods.Also, methods which are not struct methods, move them to the end.
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.
Addressed in e3e4069 and 63c0cc2 respectively.