Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | package rt |
| 2 | |
| 3 | import ( |
Asim Shankar | 6a65d28 | 2014-06-26 12:12:50 -0700 | [diff] [blame] | 4 | "fmt" |
Matt Rosencrantz | 3e76f28 | 2014-11-10 09:38:57 -0800 | [diff] [blame] | 5 | "math/rand" |
Cosmos Nicolaou | 9388ae4 | 2014-11-10 10:57:15 -0800 | [diff] [blame] | 6 | "time" |
Asim Shankar | 6a65d28 | 2014-06-26 12:12:50 -0700 | [diff] [blame] | 7 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 8 | iipc "v.io/core/veyron/runtimes/google/ipc" |
| 9 | imanager "v.io/core/veyron/runtimes/google/ipc/stream/manager" |
| 10 | "v.io/core/veyron/runtimes/google/ipc/stream/vc" |
| 11 | ivtrace "v.io/core/veyron/runtimes/google/vtrace" |
Cosmos Nicolaou | 416ca97 | 2014-05-28 22:03:06 -0700 | [diff] [blame] | 12 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 13 | "v.io/core/veyron2/context" |
| 14 | "v.io/core/veyron2/i18n" |
| 15 | "v.io/core/veyron2/ipc" |
| 16 | "v.io/core/veyron2/ipc/stream" |
| 17 | "v.io/core/veyron2/naming" |
| 18 | "v.io/core/veyron2/options" |
| 19 | "v.io/core/veyron2/verror2" |
| 20 | "v.io/core/veyron2/vtrace" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | func (rt *vrt) NewClient(opts ...ipc.ClientOpt) (ipc.Client, error) { |
Bogdan Caprita | fbcfe89 | 2014-09-30 16:50:45 -0700 | [diff] [blame] | 24 | rt.mu.Lock() |
| 25 | sm := rt.sm[0] |
| 26 | rt.mu.Unlock() |
Cosmos Nicolaou | 4e02997 | 2014-06-13 14:53:08 -0700 | [diff] [blame] | 27 | ns := rt.ns |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 28 | var otherOpts []ipc.ClientOpt |
| 29 | for _, opt := range opts { |
| 30 | switch topt := opt.(type) { |
Asim Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 31 | case options.StreamManager: |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 32 | sm = topt.Manager |
Asim Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 33 | case options.Namespace: |
Cosmos Nicolaou | 4e02997 | 2014-06-13 14:53:08 -0700 | [diff] [blame] | 34 | ns = topt.Namespace |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 35 | default: |
| 36 | otherOpts = append(otherOpts, opt) |
| 37 | } |
| 38 | } |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 39 | otherOpts = append(otherOpts, vc.LocalPrincipal{rt.principal}, &imanager.DialTimeout{5 * time.Minute}, rt.preferredProtocols) |
Suharsh Sivakumar | 1b6683e | 2014-12-30 13:00:38 -0800 | [diff] [blame] | 40 | return iipc.InternalNewClient(sm, ns, otherOpts...) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | func (rt *vrt) Client() ipc.Client { |
| 44 | return rt.client |
| 45 | } |
| 46 | |
Matt Rosencrantz | 4f8ac60 | 2014-12-29 14:42:48 -0800 | [diff] [blame] | 47 | func (rt *vrt) NewContext() *context.T { |
| 48 | ctx := context.NewUninitializedContext(rt) |
Mike Burrows | 110e22f | 2014-10-10 10:23:37 -0700 | [diff] [blame] | 49 | ctx = i18n.ContextWithLangID(ctx, rt.lang) |
| 50 | ctx = verror2.ContextWithComponentName(ctx, rt.program) |
Matt Rosencrantz | 3e76f28 | 2014-11-10 09:38:57 -0800 | [diff] [blame] | 51 | |
| 52 | sr := rt.flags.Vtrace.SampleRate |
| 53 | forceCollect := sr > 0.0 && (sr >= 1.0 || rand.Float64() < sr) |
| 54 | ctx, _ = ivtrace.WithNewRootSpan(ctx, rt.traceStore, forceCollect) |
| 55 | |
Matt Rosencrantz | 5845bf1 | 2015-01-06 13:05:21 -0800 | [diff] [blame^] | 56 | return rt.initRuntimeXContext(ctx) |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Matt Rosencrantz | 4f8ac60 | 2014-12-29 14:42:48 -0800 | [diff] [blame] | 59 | func (rt *vrt) WithNewSpan(ctx *context.T, name string) (*context.T, vtrace.Span) { |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 60 | return ivtrace.WithNewSpan(ctx, name) |
| 61 | } |
| 62 | |
Matt Rosencrantz | 4f8ac60 | 2014-12-29 14:42:48 -0800 | [diff] [blame] | 63 | func (rt *vrt) SpanFromContext(ctx *context.T) vtrace.Span { |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 64 | return ivtrace.FromContext(ctx) |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 67 | func (rt *vrt) NewServer(opts ...ipc.ServerOpt) (ipc.Server, error) { |
Asim Shankar | 6a65d28 | 2014-06-26 12:12:50 -0700 | [diff] [blame] | 68 | rt.mu.Lock() |
Bogdan Caprita | fbcfe89 | 2014-09-30 16:50:45 -0700 | [diff] [blame] | 69 | // Create a new RoutingID (and StreamManager) for each server, except |
| 70 | // the first one. The reasoning is for the first server to share the |
| 71 | // RoutingID (and StreamManager) with Clients. |
| 72 | // |
| 73 | // TODO(ashankar/caprita): special-casing the first server is ugly, and |
| 74 | // has diminished practical benefits since the first server in practice |
| 75 | // is the app cycle manager server. If the goal of sharing connections |
| 76 | // between Clients and Servers in the same Runtime is still important, |
| 77 | // we need to think of other ways to achieve it. |
| 78 | sm := rt.sm[0] |
Asim Shankar | 6a65d28 | 2014-06-26 12:12:50 -0700 | [diff] [blame] | 79 | rt.nServers++ |
Bogdan Caprita | fbcfe89 | 2014-09-30 16:50:45 -0700 | [diff] [blame] | 80 | nServers := rt.nServers |
Asim Shankar | 6a65d28 | 2014-06-26 12:12:50 -0700 | [diff] [blame] | 81 | rt.mu.Unlock() |
Bogdan Caprita | fbcfe89 | 2014-09-30 16:50:45 -0700 | [diff] [blame] | 82 | if nServers > 1 { |
| 83 | var err error |
| 84 | sm, err = rt.NewStreamManager() |
| 85 | if err != nil { |
| 86 | return nil, fmt.Errorf("failed to create ipc/stream/Manager: %v", err) |
| 87 | } |
Asim Shankar | 6a65d28 | 2014-06-26 12:12:50 -0700 | [diff] [blame] | 88 | } |
Cosmos Nicolaou | 4e02997 | 2014-06-13 14:53:08 -0700 | [diff] [blame] | 89 | ns := rt.ns |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 90 | var otherOpts []ipc.ServerOpt |
| 91 | for _, opt := range opts { |
| 92 | switch topt := opt.(type) { |
Asim Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 93 | case options.Namespace: |
Cosmos Nicolaou | 4e02997 | 2014-06-13 14:53:08 -0700 | [diff] [blame] | 94 | ns = topt |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 95 | default: |
| 96 | otherOpts = append(otherOpts, opt) |
| 97 | } |
| 98 | } |
Asim Shankar | 220a015 | 2014-10-30 21:21:09 -0700 | [diff] [blame] | 99 | // Add the option that provides the principal to the server. |
| 100 | otherOpts = append(otherOpts, vc.LocalPrincipal{rt.principal}) |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 101 | if rt.reservedDisp != nil { |
Cosmos Nicolaou | 6413c99 | 2014-11-06 11:00:54 -0800 | [diff] [blame] | 102 | ropts := options.ReservedNameDispatcher{rt.reservedDisp} |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 103 | otherOpts = append(otherOpts, ropts) |
| 104 | otherOpts = append(otherOpts, rt.reservedOpts...) |
| 105 | } |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 106 | |
Nicolas LaCasse | 55a10f3 | 2014-11-26 13:25:53 -0800 | [diff] [blame] | 107 | // Add the preferred protocols from the runtime if there are any. |
| 108 | if len(rt.preferredProtocols) > 0 { |
| 109 | otherOpts = append(otherOpts, iipc.PreferredServerResolveProtocols(rt.preferredProtocols)) |
| 110 | } |
Asim Shankar | f4864f4 | 2014-11-25 18:53:05 -0800 | [diff] [blame] | 111 | ctx := rt.NewContext() |
Suharsh Sivakumar | 1b6683e | 2014-12-30 13:00:38 -0800 | [diff] [blame] | 112 | return iipc.InternalNewServer(ctx, sm, ns, rt.traceStore, otherOpts...) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | func (rt *vrt) NewStreamManager(opts ...stream.ManagerOpt) (stream.Manager, error) { |
| 116 | rid, err := naming.NewRoutingID() |
| 117 | if err != nil { |
| 118 | return nil, err |
| 119 | } |
Asim Shankar | 6a65d28 | 2014-06-26 12:12:50 -0700 | [diff] [blame] | 120 | sm := imanager.InternalNew(rid) |
Bogdan Caprita | fbcfe89 | 2014-09-30 16:50:45 -0700 | [diff] [blame] | 121 | rt.mu.Lock() |
| 122 | defer rt.mu.Unlock() |
| 123 | if rt.cleaningUp { |
| 124 | sm.Shutdown() // For whatever it's worth. |
Bogdan Caprita | fbcfe89 | 2014-09-30 16:50:45 -0700 | [diff] [blame] | 125 | return nil, errCleaningUp |
| 126 | } |
| 127 | rt.sm = append(rt.sm, sm) |
Asim Shankar | 6a65d28 | 2014-06-26 12:12:50 -0700 | [diff] [blame] | 128 | return sm, nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 129 | } |