starpc 0.11.2 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,7 +41,7 @@ func (c *srpcEchoerClient) SRPCClient() srpc.Client { return c.cc }
41
41
 
42
42
  func (c *srpcEchoerClient) Echo(ctx context.Context, in *EchoMsg) (*EchoMsg, error) {
43
43
  out := new(EchoMsg)
44
- err := c.cc.Invoke(ctx, c.serviceID, "Echo", in, out)
44
+ err := c.cc.ExecCall(ctx, c.serviceID, "Echo", in, out)
45
45
  if err != nil {
46
46
  return nil, err
47
47
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starpc",
3
- "version": "0.11.2",
3
+ "version": "0.12.0",
4
4
  "description": "Streaming protobuf RPC service protocol over any two-way channel.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -0,0 +1,58 @@
1
+ package srpc
2
+
3
+ import "context"
4
+
5
+ // PrefixClient checks for and strips a set of prefixes from a Client.
6
+ type PrefixClient struct {
7
+ // client is the underlying client
8
+ client Client
9
+ // serviceIDPrefixes is the list of service id prefixes to match.
10
+ serviceIDPrefixes []string
11
+ }
12
+
13
+ // NewPrefixClient constructs a new PrefixClient.
14
+ //
15
+ // serviceIDPrefixes is the list of service id prefixes to match.
16
+ // strips the prefix before calling the underlying Invoke function.
17
+ // if none of the prefixes match, returns unimplemented.
18
+ // if empty: forwards all services w/o stripping any prefix.
19
+ func NewPrefixClient(client Client, serviceIDPrefixes []string) *PrefixClient {
20
+ return &PrefixClient{
21
+ client: client,
22
+ serviceIDPrefixes: serviceIDPrefixes,
23
+ }
24
+ }
25
+
26
+ // ExecCall executes a request/reply RPC with the remote.
27
+ func (i *PrefixClient) ExecCall(ctx context.Context, service, method string, in, out Message) error {
28
+ service, err := i.stripCheckServiceIDPrefix(service)
29
+ if err != nil {
30
+ return err
31
+ }
32
+ return i.client.ExecCall(ctx, service, method, in, out)
33
+ }
34
+
35
+ // NewStream starts a streaming RPC with the remote & returns the stream.
36
+ // firstMsg is optional.
37
+ func (i *PrefixClient) NewStream(ctx context.Context, service, method string, firstMsg Message) (Stream, error) {
38
+ service, err := i.stripCheckServiceIDPrefix(service)
39
+ if err != nil {
40
+ return nil, err
41
+ }
42
+ return i.client.NewStream(ctx, service, method, firstMsg)
43
+ }
44
+
45
+ // stripCheckServiceIDPrefix strips the prefix & returns unimplemented if necessary.
46
+ func (i *PrefixClient) stripCheckServiceIDPrefix(service string) (string, error) {
47
+ if len(i.serviceIDPrefixes) != 0 {
48
+ strippedID, matchedPrefix := CheckStripPrefix(service, i.serviceIDPrefixes)
49
+ if len(matchedPrefix) == 0 {
50
+ return service, ErrUnimplemented
51
+ }
52
+ return strippedID, nil
53
+ }
54
+ return service, nil
55
+ }
56
+
57
+ // _ is a type assertion
58
+ var _ Client = ((*PrefixClient)(nil))
package/srpc/client.go CHANGED
@@ -8,8 +8,8 @@ import (
8
8
 
9
9
  // Client implements a SRPC client which can initiate RPC streams.
10
10
  type Client interface {
11
- // Invoke executes a unary RPC with the remote.
12
- Invoke(ctx context.Context, service, method string, in, out Message) error
11
+ // ExecCall executes a request/reply RPC with the remote.
12
+ ExecCall(ctx context.Context, service, method string, in, out Message) error
13
13
 
14
14
  // NewStream starts a streaming RPC with the remote & returns the stream.
15
15
  // firstMsg is optional.
@@ -37,8 +37,8 @@ func NewClient(openStream OpenStreamFunc) Client {
37
37
  }
38
38
  }
39
39
 
40
- // Invoke executes a unary RPC with the remote.
41
- func (c *client) Invoke(rctx context.Context, service, method string, in, out Message) error {
40
+ // ExecCall executes a request/reply RPC with the remote.
41
+ func (c *client) ExecCall(rctx context.Context, service, method string, in, out Message) error {
42
42
  ctx, ctxCancel := context.WithCancel(rctx)
43
43
  defer ctxCancel()
44
44
 
package/srpc/handler.go CHANGED
@@ -1,13 +1,5 @@
1
1
  package srpc
2
2
 
3
- // Invoker describes a SRPC service invoker.
4
- type Invoker interface {
5
- // InvokeMethod invokes the method matching the service & method ID.
6
- // Returns false, nil if not found.
7
- // If service string is empty, ignore it.
8
- InvokeMethod(serviceID, methodID string, strm Stream) (bool, error)
9
- }
10
-
11
3
  // Handler describes a SRPC call handler implementation.
12
4
  type Handler interface {
13
5
  // Invoker invokes the methods.
@@ -0,0 +1,40 @@
1
+ package srpc
2
+
3
+ // PrefixInvoker checks for and strips a set of prefixes from a Invoker.
4
+ type PrefixInvoker struct {
5
+ // inv is the underlying invoker
6
+ inv Invoker
7
+ // serviceIDPrefixes is the list of service id prefixes to match.
8
+ serviceIDPrefixes []string
9
+ }
10
+
11
+ // NewPrefixInvoker constructs a new PrefixInvoker.
12
+ //
13
+ // serviceIDPrefixes is the list of service id prefixes to match.
14
+ // strips the prefix before calling the underlying Invoke function.
15
+ // if none of the prefixes match, returns unimplemented.
16
+ // if empty: forwards all services w/o stripping any prefix.
17
+ func NewPrefixInvoker(inv Invoker, serviceIDPrefixes []string) *PrefixInvoker {
18
+ return &PrefixInvoker{
19
+ inv: inv,
20
+ serviceIDPrefixes: serviceIDPrefixes,
21
+ }
22
+ }
23
+
24
+ // InvokeMethod invokes the method matching the service & method ID.
25
+ // Returns false, nil if not found.
26
+ // If service string is empty, ignore it.
27
+ func (i *PrefixInvoker) InvokeMethod(serviceID, methodID string, strm Stream) (bool, error) {
28
+ if len(i.serviceIDPrefixes) != 0 {
29
+ strippedID, matchedPrefix := CheckStripPrefix(serviceID, i.serviceIDPrefixes)
30
+ if len(matchedPrefix) == 0 {
31
+ return false, nil
32
+ }
33
+ serviceID = strippedID
34
+ }
35
+
36
+ return i.inv.InvokeMethod(serviceID, methodID, strm)
37
+ }
38
+
39
+ // _ is a type assertion
40
+ var _ Invoker = ((*PrefixInvoker)(nil))
@@ -0,0 +1,9 @@
1
+ package srpc
2
+
3
+ // Invoker is a function for invoking SRPC service methods.
4
+ type Invoker interface {
5
+ // InvokeMethod invokes the method matching the service & method ID.
6
+ // Returns false, nil if not found.
7
+ // If service string is empty, ignore it.
8
+ InvokeMethod(serviceID, methodID string, strm Stream) (bool, error)
9
+ }
@@ -0,0 +1,26 @@
1
+ package srpc
2
+
3
+ import "strings"
4
+
5
+ // CheckStripPrefix checks if the string has any of the given prefixes and
6
+ // strips the matched prefix if any.
7
+ //
8
+ // if len(matchPrefixes) == 0 returns the ID without changing it.
9
+ func CheckStripPrefix(id string, matchPrefixes []string) (strippedID string, matchedPrefix string) {
10
+ if len(matchPrefixes) == 0 {
11
+ return id, ""
12
+ }
13
+
14
+ var matched bool
15
+ for _, prefix := range matchPrefixes {
16
+ matched = strings.HasPrefix(id, prefix)
17
+ if matched {
18
+ matchedPrefix = prefix
19
+ break
20
+ }
21
+ }
22
+ if !matched {
23
+ return id, ""
24
+ }
25
+ return id[len(matchedPrefix):], matchedPrefix
26
+ }