mirror of
https://github.com/hanzoai/psrpc.git
synced 2026-09-30 23:47:22 +00:00
generate client and server ids (#73)
* generate client and server ids * add option to supply ids
This commit is contained in:
@@ -30,6 +30,7 @@ const (
|
||||
type ClientOption func(*ClientOpts)
|
||||
|
||||
type ClientOpts struct {
|
||||
ClientID string
|
||||
Timeout time.Duration
|
||||
ChannelSize int
|
||||
EnableStreams bool
|
||||
@@ -40,6 +41,12 @@ type ClientOpts struct {
|
||||
StreamInterceptors []StreamInterceptor
|
||||
}
|
||||
|
||||
func WithClientID(id string) ClientOption {
|
||||
return func(o *ClientOpts) {
|
||||
o.ClientID = id
|
||||
}
|
||||
}
|
||||
|
||||
func WithClientTimeout(timeout time.Duration) ClientOption {
|
||||
return func(o *ClientOpts) {
|
||||
o.Timeout = timeout
|
||||
|
||||
@@ -24,7 +24,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/livekit/psrpc/internal"
|
||||
"github.com/livekit/psrpc/internal/rand"
|
||||
"github.com/livekit/psrpc/pkg/rand"
|
||||
)
|
||||
|
||||
const defaultClientTimeout = time.Second * 3
|
||||
@@ -57,7 +57,7 @@ func TestMessageBus(t *testing.T) {
|
||||
func testSubscribe(t *testing.T, bus MessageBus) {
|
||||
ctx := context.Background()
|
||||
|
||||
channel := rand.String()
|
||||
channel := rand.NewString()
|
||||
subA, err := Subscribe[*internal.Request](ctx, bus, channel, DefaultChannelSize)
|
||||
require.NoError(t, err)
|
||||
subB, err := Subscribe[*internal.Request](ctx, bus, channel, DefaultChannelSize)
|
||||
@@ -79,7 +79,7 @@ func testSubscribe(t *testing.T, bus MessageBus) {
|
||||
func testSubscribeQueue(t *testing.T, bus MessageBus) {
|
||||
ctx := context.Background()
|
||||
|
||||
channel := rand.String()
|
||||
channel := rand.NewString()
|
||||
subA, err := SubscribeQueue[*internal.Request](ctx, bus, channel, DefaultChannelSize)
|
||||
require.NoError(t, err)
|
||||
subB, err := SubscribeQueue[*internal.Request](ctx, bus, channel, DefaultChannelSize)
|
||||
@@ -115,7 +115,7 @@ func testSubscribeQueue(t *testing.T, bus MessageBus) {
|
||||
func testSubscribeClose(t *testing.T, bus MessageBus) {
|
||||
ctx := context.Background()
|
||||
|
||||
channel := rand.String()
|
||||
channel := rand.NewString()
|
||||
sub, err := Subscribe[*internal.Request](ctx, bus, channel, DefaultChannelSize)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
@@ -27,8 +27,8 @@ import (
|
||||
"github.com/livekit/psrpc/internal/bus"
|
||||
"github.com/livekit/psrpc/internal/interceptors"
|
||||
"github.com/livekit/psrpc/internal/logger"
|
||||
"github.com/livekit/psrpc/internal/rand"
|
||||
"github.com/livekit/psrpc/pkg/info"
|
||||
"github.com/livekit/psrpc/pkg/rand"
|
||||
)
|
||||
|
||||
type Stream[SendType, RecvType proto.Message] interface {
|
||||
|
||||
@@ -25,8 +25,8 @@ import (
|
||||
|
||||
"github.com/livekit/psrpc"
|
||||
"github.com/livekit/psrpc/internal"
|
||||
"github.com/livekit/psrpc/internal/rand"
|
||||
"github.com/livekit/psrpc/pkg/info"
|
||||
"github.com/livekit/psrpc/pkg/rand"
|
||||
)
|
||||
|
||||
func TestClosePendingSend(t *testing.T) {
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: empty_service.proto
|
||||
|
||||
package empty_service
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: service.proto
|
||||
|
||||
package google_protobuf_imports
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: importable.proto
|
||||
|
||||
// Test to make sure that importing other packages doesnt break
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: importer.proto
|
||||
|
||||
// Test to make sure that importing other packages doesnt break
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: importer_local.proto
|
||||
|
||||
package importer_local
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: importer_local_msgdef.proto
|
||||
|
||||
package importer_local
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: x/x.proto
|
||||
|
||||
package x
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: y/y.proto
|
||||
|
||||
package y
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: multiple1.proto
|
||||
|
||||
// Multiple proto files in one package
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: multiple2.proto
|
||||
|
||||
// Multiple proto files in one package
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: my_service.proto
|
||||
|
||||
package my_service
|
||||
|
||||
@@ -27,7 +27,6 @@ import (
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/psrpc"
|
||||
r "github.com/livekit/psrpc/internal/rand"
|
||||
)
|
||||
|
||||
func TestGeneratedService(t *testing.T) {
|
||||
@@ -225,14 +224,14 @@ func createServer(t *testing.T, bus psrpc.MessageBus) *MyService {
|
||||
svc := &MyService{
|
||||
counts: make(map[string]int),
|
||||
}
|
||||
server, err := NewMyServiceServer(r.String(), svc, bus)
|
||||
server, err := NewMyServiceServer(svc, bus)
|
||||
require.NoError(t, err)
|
||||
svc.server = server
|
||||
return svc
|
||||
}
|
||||
|
||||
func createClient(t *testing.T, bus psrpc.MessageBus, opts ...psrpc.ClientOption) MyServiceClient {
|
||||
client, err := NewMyServiceClient(r.String(), bus, opts...)
|
||||
client, err := NewMyServiceClient(bus, opts...)
|
||||
require.NoError(t, err)
|
||||
return client
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: no_package_name.proto
|
||||
|
||||
package no_package_name
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: no_package_name_importer/no_package_name_importer.proto
|
||||
|
||||
package no_package_name_importer
|
||||
|
||||
@@ -27,9 +27,9 @@ import (
|
||||
|
||||
"github.com/livekit/psrpc"
|
||||
"github.com/livekit/psrpc/internal"
|
||||
"github.com/livekit/psrpc/internal/rand"
|
||||
"github.com/livekit/psrpc/pkg/client"
|
||||
"github.com/livekit/psrpc/pkg/info"
|
||||
"github.com/livekit/psrpc/pkg/rand"
|
||||
"github.com/livekit/psrpc/pkg/server"
|
||||
)
|
||||
|
||||
@@ -74,15 +74,15 @@ func testRPC(t *testing.T, bus psrpc.MessageBus) {
|
||||
|
||||
serverA := server.NewRPCServer(&info.ServiceDefinition{
|
||||
Name: serviceName,
|
||||
ID: rand.String(),
|
||||
ID: rand.NewString(),
|
||||
}, bus)
|
||||
serverB := server.NewRPCServer(&info.ServiceDefinition{
|
||||
Name: serviceName,
|
||||
ID: rand.String(),
|
||||
ID: rand.NewString(),
|
||||
}, bus)
|
||||
serverC := server.NewRPCServer(&info.ServiceDefinition{
|
||||
Name: serviceName,
|
||||
ID: rand.String(),
|
||||
ID: rand.NewString(),
|
||||
}, bus)
|
||||
|
||||
t.Cleanup(func() {
|
||||
@@ -93,7 +93,7 @@ func testRPC(t *testing.T, bus psrpc.MessageBus) {
|
||||
|
||||
c, err := client.NewRPCClient(&info.ServiceDefinition{
|
||||
Name: serviceName,
|
||||
ID: rand.String(),
|
||||
ID: rand.NewString(),
|
||||
}, bus)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -173,7 +173,7 @@ func testStream(t *testing.T, bus psrpc.MessageBus) {
|
||||
|
||||
serverA := server.NewRPCServer(&info.ServiceDefinition{
|
||||
Name: serviceName,
|
||||
ID: rand.String(),
|
||||
ID: rand.NewString(),
|
||||
}, bus)
|
||||
|
||||
t.Cleanup(func() {
|
||||
@@ -182,7 +182,7 @@ func testStream(t *testing.T, bus psrpc.MessageBus) {
|
||||
|
||||
c, err := client.NewRPCClientWithStreams(&info.ServiceDefinition{
|
||||
Name: serviceName,
|
||||
ID: rand.String(),
|
||||
ID: rand.NewString(),
|
||||
}, bus)
|
||||
require.NoError(t, err)
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: service_method_same_name.proto
|
||||
|
||||
package service_method_same_name
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.31.0
|
||||
// protoc v4.23.4
|
||||
// protoc v3.21.12
|
||||
// source: snake_case_names.proto
|
||||
|
||||
// Test that protoc-gen-psrpc follows the same behavior as protoc-gen-go
|
||||
|
||||
@@ -61,6 +61,9 @@ func NewRPCClient(
|
||||
streamChannels: make(map[string]chan *internal.Stream),
|
||||
closed: core.NewFuse(),
|
||||
}
|
||||
if c.ClientID != "" {
|
||||
c.ID = c.ClientID
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
responses, err := bus.Subscribe[*internal.Response](
|
||||
|
||||
+1
-1
@@ -24,9 +24,9 @@ import (
|
||||
"github.com/livekit/psrpc/internal"
|
||||
"github.com/livekit/psrpc/internal/bus"
|
||||
"github.com/livekit/psrpc/internal/interceptors"
|
||||
"github.com/livekit/psrpc/internal/rand"
|
||||
"github.com/livekit/psrpc/pkg/info"
|
||||
"github.com/livekit/psrpc/pkg/metadata"
|
||||
"github.com/livekit/psrpc/pkg/rand"
|
||||
)
|
||||
|
||||
func RequestMulti[ResponseType proto.Message](
|
||||
|
||||
+1
-1
@@ -25,9 +25,9 @@ import (
|
||||
"github.com/livekit/psrpc/internal"
|
||||
"github.com/livekit/psrpc/internal/bus"
|
||||
"github.com/livekit/psrpc/internal/interceptors"
|
||||
"github.com/livekit/psrpc/internal/rand"
|
||||
"github.com/livekit/psrpc/pkg/info"
|
||||
"github.com/livekit/psrpc/pkg/metadata"
|
||||
"github.com/livekit/psrpc/pkg/rand"
|
||||
)
|
||||
|
||||
func RequestSingle[ResponseType proto.Message](
|
||||
|
||||
@@ -24,10 +24,10 @@ import (
|
||||
"github.com/livekit/psrpc"
|
||||
"github.com/livekit/psrpc/internal"
|
||||
"github.com/livekit/psrpc/internal/logger"
|
||||
"github.com/livekit/psrpc/internal/rand"
|
||||
"github.com/livekit/psrpc/internal/stream"
|
||||
"github.com/livekit/psrpc/pkg/info"
|
||||
"github.com/livekit/psrpc/pkg/metadata"
|
||||
"github.com/livekit/psrpc/pkg/rand"
|
||||
)
|
||||
|
||||
func OpenStream[SendType, RecvType proto.Message](
|
||||
|
||||
@@ -1,17 +1,3 @@
|
||||
// Copyright 2023 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 rand
|
||||
|
||||
import (
|
||||
@@ -41,6 +27,14 @@ func (s *lockedRandSource) Seed(seed int64) {
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func NewClientID() string {
|
||||
return formatID("CLI_")
|
||||
}
|
||||
|
||||
func NewServerID() string {
|
||||
return formatID("SRV_")
|
||||
}
|
||||
|
||||
func NewRequestID() string {
|
||||
return formatID("REQ_")
|
||||
}
|
||||
@@ -49,7 +43,7 @@ func NewStreamID() string {
|
||||
return formatID("STR_")
|
||||
}
|
||||
|
||||
func String() string {
|
||||
func NewString() string {
|
||||
return formatID("")
|
||||
}
|
||||
|
||||
@@ -52,6 +52,9 @@ func NewRPCServer(sd *info.ServiceDefinition, b bus.MessageBus, opts ...psrpc.Se
|
||||
handlers: make(map[string]rpcHandler),
|
||||
shutdown: core.NewFuse(),
|
||||
}
|
||||
if s.ServerID != "" {
|
||||
s.ID = s.ServerID
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -102,6 +102,7 @@ func (t *psrpc) Generate(in *plugin.CodeGeneratorRequest) *plugin.CodeGeneratorR
|
||||
t.registerPackageName("context")
|
||||
t.registerPackageName("info")
|
||||
t.registerPackageName("psrpc")
|
||||
t.registerPackageName("rand")
|
||||
t.registerPackageName("server")
|
||||
t.registerPackageName("version")
|
||||
|
||||
@@ -278,6 +279,7 @@ func (t *psrpc) generateImports(file *descriptor.FileDescriptorProto) {
|
||||
t.P(` "github.com/livekit/psrpc"`)
|
||||
t.P(` "github.com/livekit/psrpc/pkg/client"`)
|
||||
t.P(` "github.com/livekit/psrpc/pkg/info"`)
|
||||
t.P(` "github.com/livekit/psrpc/pkg/rand"`)
|
||||
t.P(` "github.com/livekit/psrpc/pkg/server"`)
|
||||
t.P(` "github.com/livekit/psrpc/version"`)
|
||||
t.P(`)`)
|
||||
@@ -463,10 +465,10 @@ func (t *psrpc) generateClient(service *descriptor.ServiceDescriptorProto) {
|
||||
t.P()
|
||||
|
||||
t.P(`// `, newClientFunc, ` creates a psrpc client that implements the `, servName, `Client interface.`)
|
||||
t.P(`func `, newClientFunc, servTopics.FormatTypeParamConstraints(), `(clientID string, bus `, t.pkgs["psrpc"], `.MessageBus, opts ...`, t.pkgs["psrpc"], `.ClientOption) (`, servName, `Client`, servTopics.FormatTypeParams(), `, error) {`)
|
||||
t.P(`func `, newClientFunc, servTopics.FormatTypeParamConstraints(), `(bus `, t.pkgs["psrpc"], `.MessageBus, opts ...`, t.pkgs["psrpc"], `.ClientOption) (`, servName, `Client`, servTopics.FormatTypeParams(), `, error) {`)
|
||||
t.P(` sd := &`, t.pkgs["info"], `.ServiceDefinition{`)
|
||||
t.P(` Name: "`, servName, `",`)
|
||||
t.P(` ID: clientID,`)
|
||||
t.P(` ID: `, t.pkgs["rand"], `.NewClientID(),`)
|
||||
t.P(` }`)
|
||||
t.P()
|
||||
|
||||
@@ -606,10 +608,10 @@ func (t *psrpc) generateServer(service *descriptor.ServiceDescriptorProto) {
|
||||
// Constructor for server implementation
|
||||
t.P(`// New`, servName, `Server builds a RPCServer that will route requests`)
|
||||
t.P(`// to the corresponding method in the provided svc implementation.`)
|
||||
t.P(`func New`, servName, `Server`, servTopics.FormatTypeParamConstraints(), `(serverID string, svc `, servName, `ServerImpl, bus `, t.pkgs["psrpc"], `.MessageBus, opts ...`, t.pkgs["psrpc"], `.ServerOption) (`, servName, `Server`, servTopics.FormatTypeParams(), `, error) {`)
|
||||
t.P(`func New`, servName, `Server`, servTopics.FormatTypeParamConstraints(), `(svc `, servName, `ServerImpl, bus `, t.pkgs["psrpc"], `.MessageBus, opts ...`, t.pkgs["psrpc"], `.ServerOption) (`, servName, `Server`, servTopics.FormatTypeParams(), `, error) {`)
|
||||
t.P(` sd := &`, t.pkgs["info"], `.ServiceDefinition{`)
|
||||
t.P(` Name: "`, servName, `",`)
|
||||
t.P(` ID: serverID,`)
|
||||
t.P(` ID: `, t.pkgs["rand"], `.NewServerID(),`)
|
||||
t.P(` }`)
|
||||
t.P()
|
||||
t.P(` s := `, t.pkgs["server"], `.NewRPCServer(sd, bus, opts...)`)
|
||||
|
||||
@@ -26,6 +26,7 @@ const DefaultServerTimeout = time.Second * 3
|
||||
type ServerOption func(*ServerOpts)
|
||||
|
||||
type ServerOpts struct {
|
||||
ServerID string
|
||||
Timeout time.Duration
|
||||
ChannelSize int
|
||||
Interceptors []ServerRPCInterceptor
|
||||
@@ -33,6 +34,12 @@ type ServerOpts struct {
|
||||
ChainedInterceptor ServerRPCInterceptor
|
||||
}
|
||||
|
||||
func WithServerID(id string) ServerOption {
|
||||
return func(o *ServerOpts) {
|
||||
o.ServerID = id
|
||||
}
|
||||
}
|
||||
|
||||
func WithServerTimeout(timeout time.Duration) ServerOption {
|
||||
return func(o *ServerOpts) {
|
||||
o.Timeout = timeout
|
||||
|
||||
+2
-2
@@ -15,6 +15,6 @@
|
||||
package version
|
||||
|
||||
const (
|
||||
Version = "v0.4.0"
|
||||
PsrpcVersion_0_4 = true
|
||||
Version = "v0.5.0"
|
||||
PsrpcVersion_0_5 = true
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user