forked from uber-go/cadence-client
/
internal_utils.go
217 lines (189 loc) · 6.51 KB
/
internal_utils.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package internal
// All code in this file is private to the package.
import (
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/pborman/uuid"
s "go.uber.org/cadence/.gen/go/shared"
"go.uber.org/cadence/internal/common"
"go.uber.org/yarpc"
"golang.org/x/net/context"
)
const (
// libraryVersionHeaderName refers to the name of the
// tchannel / http header that contains the client
// library version
libraryVersionHeaderName = "cadence-client-library-version"
// featureVersionHeaderName refers to the name of the
// tchannel / http header that contains the client
// feature version
featureVersionHeaderName = "cadence-client-feature-version"
// clientImplHeaderName refers to the name of the
// header that contains the client implementation
clientImplHeaderName = "cadence-client-name"
clientImplHeaderValue = "uber-go"
// defaultRPCTimeout is the default tchannel rpc call timeout
defaultRPCTimeout = 10 * time.Second
)
var (
// call header to cadence server
yarpcCallOptions = []yarpc.CallOption{
yarpc.WithHeader(libraryVersionHeaderName, LibraryVersion),
yarpc.WithHeader(featureVersionHeaderName, FeatureVersion),
yarpc.WithHeader(clientImplHeaderName, clientImplHeaderValue),
}
)
// ContextBuilder stores all Channel-specific parameters that will
// be stored inside of a context.
type contextBuilder struct {
// If Timeout is zero, Build will default to defaultTimeout.
Timeout time.Duration
// ParentContext to build the new context from. If empty, context.Background() is used.
// The new (child) context inherits a number of properties from the parent context:
// - context fields, accessible via `ctx.Value(key)`
ParentContext context.Context
}
func (cb *contextBuilder) Build() (context.Context, context.CancelFunc) {
parent := cb.ParentContext
if parent == nil {
parent = context.Background()
}
return context.WithTimeout(parent, cb.Timeout)
}
// sets the rpc timeout for a context
func chanTimeout(timeout time.Duration) func(builder *contextBuilder) {
return func(b *contextBuilder) {
b.Timeout = timeout
}
}
// newChannelContext - Get a rpc channel context
func newChannelContext(ctx context.Context, options ...func(builder *contextBuilder)) (context.Context, context.CancelFunc, []yarpc.CallOption) {
builder := &contextBuilder{Timeout: defaultRPCTimeout}
if ctx != nil {
builder.ParentContext = ctx
}
for _, opt := range options {
opt(builder)
}
ctx, cancelFn := builder.Build()
return ctx, cancelFn, yarpcCallOptions
}
// GetWorkerIdentity gets a default identity for the worker.
func getWorkerIdentity(tasklistName string) string {
return fmt.Sprintf("%d@%s@%s", os.Getpid(), getHostName(), tasklistName)
}
func getHostName() string {
hostName, err := os.Hostname()
if err != nil {
hostName = "UnKnown"
}
return hostName
}
// worker uuid per process
var workerUUID = uuid.New()
func getWorkerTaskList() string {
// includes hostname for debuggability, workerUUID guarantees the uniqueness
return fmt.Sprintf("%s:%s", getHostName(), workerUUID)
}
// ActivityTypePtr makes a copy and returns the pointer to a ActivityType.
func activityTypePtr(v ActivityType) *s.ActivityType {
return &s.ActivityType{Name: common.StringPtr(v.Name)}
}
func flowWorkflowTypeFrom(v s.WorkflowType) WorkflowType {
return WorkflowType{Name: v.GetName()}
}
// WorkflowTypePtr makes a copy and returns the pointer to a WorkflowType.
func workflowTypePtr(t WorkflowType) *s.WorkflowType {
return &s.WorkflowType{Name: common.StringPtr(t.Name)}
}
// getErrorDetails gets reason and details.
func getErrorDetails(err error) (string, []byte) {
switch err := err.(type) {
case *CustomError:
return err.Reason(), err.details
case *CanceledError:
return errReasonCanceled, err.details
case *PanicError:
data, gobErr := getHostEnvironment().encodeArgs([]interface{}{err.Error(), err.StackTrace()})
if gobErr != nil {
panic(gobErr)
}
return errReasonPanic, data
case *TimeoutError:
data, gobErr := getHostEnvironment().encodeArgs([]interface{}{err.timeoutType})
if gobErr != nil {
panic(gobErr)
}
return errReasonTimeout, data
default:
// will be convert to GenericError when receiving from server.
return errReasonGeneric, []byte(err.Error())
}
}
// constructError construct error from reason and details sending down from server.
func constructError(reason string, details []byte) error {
switch reason {
case errReasonPanic:
// panic error
var msg, st string
details := EncodedValues(details)
details.Get(&msg, &st)
return newPanicError(msg, st)
case errReasonGeneric:
// errors created other than using NewCustomError() API.
return &GenericError{err: string(details)}
case errReasonCanceled:
return NewCanceledError(details)
case errReasonTimeout:
var timeoutType s.TimeoutType
details := EncodedValues(details)
details.Get(&timeoutType)
return NewTimeoutError(timeoutType)
default:
return NewCustomError(reason, details)
}
}
// AwaitWaitGroup calls Wait on the given wait
// Returns true if the Wait() call succeeded before the timeout
// Returns false if the Wait() did not return before the timeout
func awaitWaitGroup(wg *sync.WaitGroup, timeout time.Duration) bool {
doneC := make(chan struct{})
go func() {
wg.Wait()
close(doneC)
}()
select {
case <-doneC:
return true
case <-time.After(timeout):
return false
}
}
func getKillSignal() <-chan os.Signal {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
return c
}