More browspr fixes:
* Move the EndpointToWs function into its own file.
* namespace.SetRoots method must use EndpointToWs.
* Add a sleep with a TODO to prevent a race condition.
* Bump the wspr retry time to 2 seconds.
Change-Id: I47e2938fe4a2bf920cb006ea1e6afc5a71db55d0
diff --git a/services/wsprd/app/app.go b/services/wsprd/app/app.go
index ed321a0..3322afb 100644
--- a/services/wsprd/app/app.go
+++ b/services/wsprd/app/app.go
@@ -54,7 +54,7 @@
// TODO(bjornick,nlacasse): Remove the retryTimeout flag once we able
// to pass it in from javascript. For now all RPCs have the same
// retryTimeout, set by command line flag.
- retryTimeout = flag.Int("retry-timeout", 0, "Duration in seconds to retry starting an RPC call. 0 means never retry.")
+ retryTimeout = flag.Int("retry-timeout", 2, "Duration in seconds to retry starting an RPC call. 0 means never retry.")
}
// Temporary holder of RPC so that we can store the unprocessed args.
diff --git a/services/wsprd/browspr/browspr.go b/services/wsprd/browspr/browspr.go
index 163ac55..f9a8fd0 100644
--- a/services/wsprd/browspr/browspr.go
+++ b/services/wsprd/browspr/browspr.go
@@ -2,18 +2,16 @@
package browspr
import (
- "fmt"
"net"
- "regexp"
"sync"
"time"
"veyron.io/veyron/veyron2"
"veyron.io/veyron/veyron2/ipc"
- "veyron.io/veyron/veyron2/naming"
"veyron.io/veyron/veyron2/rt"
"veyron.io/veyron/veyron2/vlog"
"veyron.io/wspr/veyron/services/wsprd/account"
+ "veyron.io/wspr/veyron/services/wsprd/lib"
"veyron.io/wspr/veyron/services/wsprd/principal"
)
@@ -47,7 +45,7 @@
vlog.Fatalf("rt.New failed: %s", err)
}
- wsNamespaceRoots, err := wsNames(namespaceRoots)
+ wsNamespaceRoots, err := lib.EndpointsToWs(runtime, namespaceRoots)
if err != nil {
vlog.Fatal(err)
}
@@ -146,33 +144,3 @@
}
return nil
}
-
-// Turns a list of names into a list of names that use the "ws" protocol.
-func wsNames(names []string) ([]string, error) {
- runtime, err := rt.New()
- if err != nil {
- return nil, fmt.Errorf("rt.New() failed: %v", err)
- }
- outNames := []string{}
- tcpRegexp := regexp.MustCompile(`@tcp\d*@`)
- for _, name := range names {
- addr, suff := naming.SplitAddressName(name)
- ep, err := runtime.NewEndpoint(addr)
- if err != nil {
- return nil, fmt.Errorf("runtime.NewEndpoint(%v) failed: %v", addr, err)
- }
- // Replace only the first match.
- first := true
- wsEp := tcpRegexp.ReplaceAllFunc([]byte(ep.String()), func(s []byte) []byte {
- if first {
- first = false
- return []byte("@ws@")
- }
- return s
- })
- wsName := naming.JoinAddressName(string(wsEp), suff)
-
- outNames = append(outNames, wsName)
- }
- return outNames, nil
-}
diff --git a/services/wsprd/browspr/browspr_test.go b/services/wsprd/browspr/browspr_test.go
index cd7c7f9..fa57558 100644
--- a/services/wsprd/browspr/browspr_test.go
+++ b/services/wsprd/browspr/browspr_test.go
@@ -253,26 +253,3 @@
t.Errorf("got %s with err: %v, expected %s", result[0], err, "[InputValue]")
}
}
-
-func TestWsNames(t *testing.T) {
- testdata := map[string]string{
- "/@3@tcp@127.0.0.1:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@": "/@3@ws@127.0.0.1:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@",
- "/@3@tcp4@example.com:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@/more/stuff": "/@3@ws@example.com:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@/more/stuff",
- "/@3@ws@example.com:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@/more/stuff": "/@3@ws@example.com:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@/more/stuff",
- "/@3@tcp@[::]:60624@21ba0c2508adfe8507eb953e526bd5a2@4@6@m@@": "/@3@ws@[::]:60624@21ba0c2508adfe8507eb953e526bd5a2@4@6@m@@",
- "/example.com:12345": "/@3@ws@example.com:12345@00000000000000000000000000000000@@@m@@",
- "/example.com:12345/more/stuff/in/suffix": "/@3@ws@example.com:12345@00000000000000000000000000000000@@@m@@/more/stuff/in/suffix",
- }
-
- for name, expectedWsName := range testdata {
- inputNames := []string{name}
- actualWsNames, err := wsNames(inputNames)
- if err != nil {
- t.Fatal(err)
- }
- expectedWsNames := []string{expectedWsName}
- if len(actualWsNames) != 1 || actualWsNames[0] != expectedWsNames[0] {
- t.Errorf("expected wsNames(%v) to be %v but got %v", inputNames, expectedWsNames, actualWsNames)
- }
- }
-}
diff --git a/services/wsprd/ipc/server/server.go b/services/wsprd/ipc/server/server.go
index ab263f6..7ad5048 100644
--- a/services/wsprd/ipc/server/server.go
+++ b/services/wsprd/ipc/server/server.go
@@ -307,6 +307,11 @@
if err := s.server.ServeDispatcher(name, s.dispatcher); err != nil {
return err
}
+ // TODO(nlacasse): This Sleep is necessary to prevent the client from
+ // making calls to the name before it has been mounted in the
+ // mounttable. Remove this once
+ // https://veyron-review.googlesource.com/#/c/7358/ has landed.
+ time.Sleep(1 * time.Second)
return nil
}
diff --git a/services/wsprd/lib/ws_endpoint.go b/services/wsprd/lib/ws_endpoint.go
new file mode 100644
index 0000000..d839948
--- /dev/null
+++ b/services/wsprd/lib/ws_endpoint.go
@@ -0,0 +1,35 @@
+package lib
+
+import (
+ "fmt"
+ "regexp"
+
+ "veyron.io/veyron/veyron2"
+ "veyron.io/veyron/veyron2/naming"
+)
+
+// Turns a list of names into a list of names that use the "ws" protocol.
+func EndpointsToWs(rt veyron2.Runtime, names []string) ([]string, error) {
+ outNames := []string{}
+ tcpRegexp := regexp.MustCompile(`@tcp\d*@`)
+ for _, name := range names {
+ addr, suff := naming.SplitAddressName(name)
+ ep, err := rt.NewEndpoint(addr)
+ if err != nil {
+ return nil, fmt.Errorf("rt.NewEndpoint(%v) failed: %v", addr, err)
+ }
+ // Replace only the first match.
+ first := true
+ wsEp := tcpRegexp.ReplaceAllFunc([]byte(ep.String()), func(s []byte) []byte {
+ if first {
+ first = false
+ return []byte("@ws@")
+ }
+ return s
+ })
+ wsName := naming.JoinAddressName(string(wsEp), suff)
+
+ outNames = append(outNames, wsName)
+ }
+ return outNames, nil
+}
diff --git a/services/wsprd/lib/ws_endpoint_test.go b/services/wsprd/lib/ws_endpoint_test.go
new file mode 100644
index 0000000..ccab819
--- /dev/null
+++ b/services/wsprd/lib/ws_endpoint_test.go
@@ -0,0 +1,34 @@
+package lib
+
+import (
+ "testing"
+ "veyron.io/veyron/veyron2/rt"
+)
+
+func TestWsNames(t *testing.T) {
+ runtime, err := rt.New()
+ if err != nil {
+ t.Fatalf("rt.New() failed: %v", err)
+ }
+ testdata := map[string]string{
+ "/@3@tcp@127.0.0.1:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@": "/@3@ws@127.0.0.1:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@",
+ "/@3@tcp4@example.com:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@/more/stuff": "/@3@ws@example.com:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@/more/stuff",
+ "/@3@ws@example.com:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@/more/stuff": "/@3@ws@example.com:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@/more/stuff",
+ "/@3@tcp@[::]:60624@21ba0c2508adfe8507eb953e526bd5a2@4@6@m@@": "/@3@ws@[::]:60624@21ba0c2508adfe8507eb953e526bd5a2@4@6@m@@",
+ "/@3@tcp4@tcpexampletcp.com:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@/more/stuff": "/@3@ws@tcpexampletcp.com:46504@d7b41510a6e78033ed86e38efb61ef52@4@6@m@@/more/stuff",
+ "/example.com:12345": "/@3@ws@example.com:12345@00000000000000000000000000000000@@@m@@",
+ "/example.com:12345/more/stuff/in/suffix/tcp": "/@3@ws@example.com:12345@00000000000000000000000000000000@@@m@@/more/stuff/in/suffix/tcp",
+ }
+
+ for name, expectedWsName := range testdata {
+ inputNames := []string{name}
+ actualWsNames, err := EndpointsToWs(runtime, inputNames)
+ if err != nil {
+ t.Fatal(err)
+ }
+ expectedWsNames := []string{expectedWsName}
+ if len(actualWsNames) != 1 || actualWsNames[0] != expectedWsNames[0] {
+ t.Errorf("expected wsNames(%v) to be %v but got %v", inputNames, expectedWsNames, actualWsNames)
+ }
+ }
+}
diff --git a/services/wsprd/namespace/request_handler.go b/services/wsprd/namespace/request_handler.go
index 1443d0f..c2f7150 100644
--- a/services/wsprd/namespace/request_handler.go
+++ b/services/wsprd/namespace/request_handler.go
@@ -110,7 +110,7 @@
case methodRoots:
roots(ctx, ns, w)
case methodSetRoots:
- setRoots(ctx, ns, w, req.Args)
+ setRoots(ctx, ns, rt, w, req.Args)
default:
w.Error(verror2.Make(verror2.NoExist, ctx, req.Method))
}
@@ -291,14 +291,19 @@
}
}
-func setRoots(ctx context.T, ns naming.Namespace, w lib.ClientWriter, rawArgs json.RawMessage) {
+func setRoots(ctx context.T, ns naming.Namespace, rt veyron2.Runtime, w lib.ClientWriter, rawArgs json.RawMessage) {
var args setRootsArgs
if err := json.Unmarshal([]byte(rawArgs), &args); err != nil {
w.Error(verror2.Convert(verror2.Internal, ctx, err))
return
}
- if err := ns.SetRoots(args.Roots...); err != nil {
+ wsRoots, err := lib.EndpointsToWs(rt, args.Roots)
+ if err != nil {
+ w.Error(verror2.Convert(verror2.Internal, ctx, err))
+ }
+
+ if err := ns.SetRoots(wsRoots...); err != nil {
w.Error(verror2.Convert(verror2.Internal, ctx, err))
return
}