Files
psrpc/helpers.go
T
Paul WellsandGitHub 5338837a3c add streaming rpc (#13)
* add streaming rpc

* fix active handler accounting

* add service test

* cleanup request context

* preserve stream order

* add stream interceptors

* add stream send options

* ensure stream is cleaned up after context cancel/open failure

* changes from feedback

* close stream with error on malformed message

* cleanup

* yield to service goroutine to fix flaky test

* add streaming client constructor

* update test client

* more docs
2023-01-27 10:42:15 -08:00

86 lines
1.8 KiB
Go

package psrpc
import (
"fmt"
"github.com/lithammer/shortuuid/v3"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
func newRequestID() string {
return "REQ_" + shortuuid.New()[:12]
}
func newStreamID() string {
return "STR_" + shortuuid.New()[:12]
}
func getRPCChannel(serviceName, rpc, topic string) string {
if topic != "" {
return fmt.Sprintf("%s|%s|%s|REQ", serviceName, rpc, topic)
} else {
return fmt.Sprintf("%s|%s|REQ", serviceName, rpc)
}
}
func getHandlerKey(rpc, topic string) string {
if topic != "" {
return fmt.Sprintf("%s|%s", rpc, topic)
} else {
return rpc
}
}
func getResponseChannel(serviceName, clientID string) string {
return fmt.Sprintf("%s|%s|RES", serviceName, clientID)
}
func getClaimRequestChannel(serviceName, clientID string) string {
return fmt.Sprintf("%s|%s|CLAIM", serviceName, clientID)
}
func getClaimResponseChannel(serviceName, rpc, topic string) string {
if topic != "" {
return fmt.Sprintf("%s|%s|%s|RCLAIM", serviceName, rpc, topic)
} else {
return fmt.Sprintf("%s|%s|RCLAIM", serviceName, rpc)
}
}
func getStreamChannel(serviceName, nodeID string) string {
return fmt.Sprintf("%s|%s|STR", serviceName, nodeID)
}
func getStreamServerChannel(serviceName, rpc, topic string) string {
if topic != "" {
return fmt.Sprintf("%s|%s|%s|STR", serviceName, rpc, topic)
} else {
return fmt.Sprintf("%s|%s|STR", serviceName, rpc)
}
}
func serialize(msg proto.Message) ([]byte, error) {
a, err := anypb.New(msg)
if err != nil {
return nil, err
}
b, err := proto.Marshal(a)
if err != nil {
return nil, err
}
return b, nil
}
func deserialize(b []byte) (proto.Message, error) {
a := &anypb.Any{}
err := proto.Unmarshal(b, a)
if err != nil {
return nil, err
}
return a.UnmarshalNew()
}