starpc 0.11.2 → 0.11.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starpc",
3
- "version": "0.11.2",
3
+ "version": "0.11.3",
4
4
  "description": "Streaming protobuf RPC service protocol over any two-way channel.",
5
5
  "license": "MIT",
6
6
  "author": {
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,55 @@
1
+ package srpc
2
+
3
+ import "strings"
4
+
5
+ // Invoker is a function for invoking SRPC service methods.
6
+ type Invoker interface {
7
+ // InvokeMethod invokes the method matching the service & method ID.
8
+ // Returns false, nil if not found.
9
+ // If service string is empty, ignore it.
10
+ InvokeMethod(serviceID, methodID string, strm Stream) (bool, error)
11
+ }
12
+
13
+ // PrefixInvoker checks for and strips a set of prefixes from a Invoker.
14
+ type PrefixInvoker struct {
15
+ // inv is the underlying invoker
16
+ inv Invoker
17
+ // serviceIDPrefixes is the list of service id prefixes to match.
18
+ // strips the prefix before calling the underlying Invoke
19
+ // if empty: forwards all services w/o stripping any prefix.
20
+ serviceIDPrefixes []string
21
+ }
22
+
23
+ // NewPrefixInvoker constructs a new PrefixInvoker.
24
+ func NewPrefixInvoker(inv Invoker, serviceIDPrefixes []string) *PrefixInvoker {
25
+ return &PrefixInvoker{
26
+ inv: inv,
27
+ serviceIDPrefixes: serviceIDPrefixes,
28
+ }
29
+ }
30
+
31
+ // InvokeMethod invokes the method matching the service & method ID.
32
+ // Returns false, nil if not found.
33
+ // If service string is empty, ignore it.
34
+ func (i *PrefixInvoker) InvokeMethod(serviceID, methodID string, strm Stream) (bool, error) {
35
+ if serviceIDPrefixes := i.serviceIDPrefixes; len(serviceIDPrefixes) != 0 {
36
+ var matched bool
37
+ var stripPrefix string
38
+ for _, prefix := range serviceIDPrefixes {
39
+ matched = strings.HasPrefix(serviceID, prefix)
40
+ if matched {
41
+ stripPrefix = prefix
42
+ break
43
+ }
44
+ }
45
+ if !matched {
46
+ return false, nil
47
+ }
48
+ serviceID = serviceID[len(stripPrefix):]
49
+ }
50
+
51
+ return i.inv.InvokeMethod(serviceID, methodID, strm)
52
+ }
53
+
54
+ // _ is a type assertion
55
+ var _ Invoker = ((*PrefixInvoker)(nil))