forked from ServiceWeaver/weaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
87 lines (73 loc) · 3.38 KB
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package weaver
import (
"context"
"net"
"github.com/ServiceWeaver/weaver/internal/logtype"
"github.com/ServiceWeaver/weaver/internal/net/call"
"github.com/ServiceWeaver/weaver/runtime"
"github.com/ServiceWeaver/weaver/runtime/protos"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
// env provides the API by which a Service Weaver application process communicates with
// its execution environment, e.g., to do thing like starting processes etc.
type env interface {
// WeaveletSetupInfo returns the weavelet's setup information sent by
// the deployer.
WeaveletSetupInfo() *protos.WeaveletSetupInfo
// WeaveletListener returns the internal listener the weavelet should
// listen on to receive messages from other weavelets.
WeaveletListener() net.Listener
// RegisterComponentToStart registers a component to start in a given
// target colocation group.
//
// The target colocation group periodically watches the set of registered
// components and starts the components that haven't already been started.
RegisterComponentToStart(ctx context.Context, targetGroup string,
component string, isRouted bool) error
// GetComponentsToStart returns the set of components that should be
// started, given a version. It also returns the version that corresponds
// to the set of components returned.
GetComponentsToStart(ctx context.Context, version *call.Version) ([]string, *call.Version, error)
// GetRoutingInfo returns the routing info for the provided colocation
// group, including the set of replicas and the current routing assignments
// (if any).
GetRoutingInfo(ctx context.Context, component string, version *call.Version) (*protos.RoutingInfo, *call.Version, error)
// GetAddress returns the address a weavelet should listen on for a
// listener.
GetAddress(ctx context.Context, listener string, opts ListenerOptions) (*protos.GetAddressReply, error)
// ExportListener exports a listener.
ExportListener(ctx context.Context, lis *protos.Listener, opts ListenerOptions) (*protos.ExportListenerReply, error)
// CreateLogSaver creates and returns a function that saves log entries
// to the environment.
CreateLogSaver(ctx context.Context, component string) func(entry *protos.LogEntry)
// SystemLogger returns the Logger for system messages.
SystemLogger() logtype.Logger
// CreateTraceExporter returns an exporter that should be used for
// exporting trace spans. A nil exporter value means that no traces
// should be exported.
CreateTraceExporter() (sdktrace.SpanExporter, error)
}
// getEnv returns the env to use for this weavelet.
func getEnv(ctx context.Context, handler WeaveletHandler) (env, error) {
bootstrap, err := runtime.GetBootstrap(ctx)
if err != nil {
return nil, err
}
if !bootstrap.HasPipes() {
return newSingleprocessEnv(bootstrap)
}
return newRemoteEnv(ctx, bootstrap, handler)
}