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

simplify psrpc options #811

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions rpc/typed_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils/must"
"github.com/livekit/protocol/utils/options"
"github.com/livekit/psrpc"
"github.com/livekit/psrpc/pkg/middleware"
)
Expand Down Expand Up @@ -82,6 +83,32 @@ func (p *ClientParams) Options() []psrpc.ClientOption {
return opts
}

func (p *ClientParams) Args() (psrpc.MessageBus, psrpc.ClientOption) {
return p.Bus, WithClientOptions(p.Options()...)
}

func WithClientOptions(opts ...psrpc.ClientOption) psrpc.ClientOption {
return func(o *psrpc.ClientOpts) { options.Apply(o, opts) }
}

func WithServerOptions(opts ...psrpc.ServerOption) psrpc.ServerOption {
return func(o *psrpc.ServerOpts) { options.Apply(o, opts) }
}

func WithServerObservability(logger logger.Logger) psrpc.ServerOption {
return WithServerOptions(
middleware.WithServerMetrics(PSRPCMetricsObserver{}),
WithServerLogger(logger),
)
}

func WithDefaultServerOptions(psrpcConfig PSRPCConfig, logger logger.Logger) psrpc.ServerOption {
return WithServerOptions(
psrpc.WithServerChannelSize(psrpcConfig.BufferSize),
WithServerObservability(logger),
)
}

//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate

type TypedSignalClient = SignalClient[livekit.NodeID]
Expand Down Expand Up @@ -141,7 +168,7 @@ type TypedRoomClient = RoomClient[RoomTopic]
type TypedRoomServer = RoomServer[RoomTopic]

func NewTypedRoomClient(params ClientParams) (TypedRoomClient, error) {
return NewRoomClient[RoomTopic](params.Bus, params.Options()...)
return NewRoomClient[RoomTopic](params.Args())
}

func NewTypedRoomServer(svc RoomServerImpl, bus psrpc.MessageBus, opts ...psrpc.ServerOption) (TypedRoomServer, error) {
Expand All @@ -153,7 +180,7 @@ type TypedParticipantClient = ParticipantClient[ParticipantTopic]
type TypedParticipantServer = ParticipantServer[ParticipantTopic]

func NewTypedParticipantClient(params ClientParams) (TypedParticipantClient, error) {
return NewParticipantClient[ParticipantTopic](params.Bus, params.Options()...)
return NewParticipantClient[ParticipantTopic](params.Args())
}

func NewTypedParticipantServer(svc ParticipantServerImpl, bus psrpc.MessageBus, opts ...psrpc.ServerOption) (TypedParticipantServer, error) {
Expand All @@ -165,7 +192,7 @@ type TypedAgentDispatchInternalClient = AgentDispatchInternalClient[RoomTopic]
type TypedAgentDispatchInternalServer = AgentDispatchInternalServer[RoomTopic]

func NewTypedAgentDispatchInternalClient(params ClientParams) (TypedAgentDispatchInternalClient, error) {
return NewAgentDispatchInternalClient[RoomTopic](params.Bus, params.Options()...)
return NewAgentDispatchInternalClient[RoomTopic](params.Args())
}

func NewTypedAgentDispatchInternalServer(svc AgentDispatchInternalServerImpl, bus psrpc.MessageBus, opts ...psrpc.ServerOption) (TypedAgentDispatchInternalServer, error) {
Expand All @@ -184,7 +211,7 @@ type keepalivePubSub struct {
}

func NewKeepalivePubSub(params ClientParams) (KeepalivePubSub, error) {
client, err := NewKeepaliveClient[livekit.NodeID](params.Bus, params.Options()...)
client, err := NewKeepaliveClient[livekit.NodeID](params.Args())
if err != nil {
return nil, err
}
Expand Down
Loading