-
Notifications
You must be signed in to change notification settings - Fork 14
/
protocol_grpc.go
643 lines (581 loc) · 19.5 KB
/
protocol_grpc.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// Copyright 2023-2024 Buf Technologies, Inc.
//
// 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://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 vanguard
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"net/http"
"net/textproto"
"strconv"
"strings"
"time"
"connectrpc.com/connect"
"google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
// grpcClientProtocol implements the gRPC protocol for
// processing RPCs received from the client.
type grpcClientProtocol struct{}
var _ clientProtocolHandler = grpcClientProtocol{}
var _ envelopedProtocolHandler = grpcClientProtocol{}
func (g grpcClientProtocol) protocol() Protocol {
return ProtocolGRPC
}
func (g grpcClientProtocol) acceptsStreamType(_ *operation, _ connect.StreamType) bool {
return true
}
func (g grpcClientProtocol) extractProtocolRequestHeaders(_ *operation, headers http.Header) (requestMeta, error) {
headers.Del("Te") // no need to propagate "te: trailers" to requests in different protocols
return grpcExtractRequestMeta("application/grpc", "application/grpc+", headers)
}
func (g grpcClientProtocol) addProtocolResponseHeaders(meta responseMeta, headers http.Header) int {
statusCode := grpcAddResponseMeta("application/grpc+", meta, headers)
if len(meta.pendingTrailers) > 0 {
if meta.pendingTrailerKeys == nil {
meta.pendingTrailerKeys = make(headerKeys, len(meta.pendingTrailers))
}
for k := range meta.pendingTrailers {
meta.pendingTrailerKeys.add(k)
}
}
for k := range meta.pendingTrailerKeys {
headers.Add("Trailer", textproto.CanonicalMIMEHeaderKey(k))
}
if !meta.pendingTrailerKeys.contains("Grpc-Status") {
headers.Add("Trailer", "Grpc-Status")
}
if !meta.pendingTrailerKeys.contains("Grpc-Message") {
headers.Add("Trailer", "Grpc-Message")
}
return statusCode
}
func (g grpcClientProtocol) encodeEnd(_ *operation, end *responseEnd, _ io.Writer, wasInHeaders bool) http.Header {
if wasInHeaders {
// already recorded this in call to addProtocolResponseHeaders
return nil
}
trailers := make(http.Header, len(end.trailers)+3)
grpcWriteEndToTrailers(end, trailers)
return trailers
}
func (g grpcClientProtocol) decodeEnvelope(bytes envelopeBytes) (envelope, error) {
return grpcServerProtocol{}.decodeEnvelope(bytes)
}
func (g grpcClientProtocol) encodeEnvelope(env envelope) envelopeBytes {
return grpcServerProtocol{}.encodeEnvelope(env)
}
func (g grpcClientProtocol) String() string {
return g.protocol().String()
}
// grpcServerProtocol implements the gRPC protocol for
// sending RPCs to the server handler.
type grpcServerProtocol struct{}
var _ serverProtocolHandler = grpcServerProtocol{}
var _ serverEnvelopedProtocolHandler = grpcServerProtocol{}
func (g grpcServerProtocol) protocol() Protocol {
return ProtocolGRPC
}
func (g grpcServerProtocol) addProtocolRequestHeaders(meta requestMeta, headers http.Header) {
grpcAddRequestMeta("application/grpc+", meta, headers)
headers.Set("Te", "trailers")
}
func (g grpcServerProtocol) extractProtocolResponseHeaders(statusCode int, headers http.Header) (responseMeta, responseEndUnmarshaller, error) {
return grpcExtractResponseMeta("application/grpc", "application/grpc+", statusCode, headers), nil, nil
}
func (g grpcServerProtocol) extractEndFromTrailers(_ *operation, trailers http.Header) (responseEnd, error) {
return responseEnd{
err: grpcExtractErrorFromTrailer(trailers),
trailers: trailers,
}, nil
}
func (g grpcServerProtocol) decodeEnvelope(envBytes envelopeBytes) (envelope, error) {
flags := envBytes[0]
if flags != 0 && flags != 1 {
return envelope{}, fmt.Errorf("invalid compression flag: must be 0 or 1; instead got %d", flags)
}
return envelope{
compressed: flags == 1,
length: binary.BigEndian.Uint32(envBytes[1:]),
}, nil
}
func (g grpcServerProtocol) encodeEnvelope(env envelope) envelopeBytes {
var envBytes envelopeBytes
if env.compressed {
envBytes[0] = 1
}
binary.BigEndian.PutUint32(envBytes[1:], env.length)
return envBytes
}
func (g grpcServerProtocol) decodeEndFromMessage(_ *operation, _ *bytes.Buffer) (responseEnd, error) {
return responseEnd{}, errors.New("gRPC protocol does not allow embedding result/trailers in body")
}
func (g grpcServerProtocol) String() string {
return g.protocol().String()
}
// grpcClientProtocol implements the gRPC protocol for
// processing RPCs received from the client.
type grpcWebClientProtocol struct{}
var _ clientProtocolHandler = grpcWebClientProtocol{}
var _ envelopedProtocolHandler = grpcWebClientProtocol{}
func (g grpcWebClientProtocol) protocol() Protocol {
return ProtocolGRPCWeb
}
func (g grpcWebClientProtocol) acceptsStreamType(_ *operation, _ connect.StreamType) bool {
return true
}
func (g grpcWebClientProtocol) extractProtocolRequestHeaders(_ *operation, headers http.Header) (requestMeta, error) {
return grpcExtractRequestMeta("application/grpc-web", "application/grpc-web+", headers)
}
func (g grpcWebClientProtocol) addProtocolResponseHeaders(meta responseMeta, headers http.Header) int {
return grpcAddResponseMeta("application/grpc-web+", meta, headers)
}
func (g grpcWebClientProtocol) encodeEnd(op *operation, end *responseEnd, writer io.Writer, wasInHeaders bool) http.Header {
if wasInHeaders {
// already recorded this in call to addProtocolResponseHeaders
return nil
}
trailers := make(http.Header, len(end.trailers)+3)
grpcWriteEndToTrailers(end, trailers)
buffer := op.bufferPool.Get()
defer op.bufferPool.Put(buffer)
_ = trailers.Write(buffer)
// TODO: Send envelope compressed if possible.
env := envelope{trailer: true, length: uint32(buffer.Len())}
envBytes := g.encodeEnvelope(env)
_, _ = writer.Write(envBytes[:])
_, _ = buffer.WriteTo(writer)
return nil
}
func (g grpcWebClientProtocol) decodeEnvelope(bytes envelopeBytes) (envelope, error) {
return grpcServerProtocol{}.decodeEnvelope(bytes)
}
func (g grpcWebClientProtocol) encodeEnvelope(env envelope) envelopeBytes {
var envBytes envelopeBytes
if env.compressed {
envBytes[0] = 1
}
if env.trailer {
envBytes[0] |= 0x80
}
binary.BigEndian.PutUint32(envBytes[1:], env.length)
return envBytes
}
func (g grpcWebClientProtocol) String() string {
return g.protocol().String()
}
// grpcServerProtocol implements the gRPC-Web protocol for
// sending RPCs to the server handler.
type grpcWebServerProtocol struct{}
var _ serverProtocolHandler = grpcWebServerProtocol{}
var _ serverEnvelopedProtocolHandler = grpcWebServerProtocol{}
func (g grpcWebServerProtocol) protocol() Protocol {
return ProtocolGRPCWeb
}
func (g grpcWebServerProtocol) addProtocolRequestHeaders(meta requestMeta, headers http.Header) {
grpcAddRequestMeta("application/grpc-web+", meta, headers)
}
func (g grpcWebServerProtocol) extractProtocolResponseHeaders(statusCode int, headers http.Header) (responseMeta, responseEndUnmarshaller, error) {
return grpcExtractResponseMeta("application/grpc-web", "application/grpc-web+", statusCode, headers), nil, nil
}
func (g grpcWebServerProtocol) extractEndFromTrailers(_ *operation, _ http.Header) (responseEnd, error) {
return responseEnd{}, errors.New("gRPC-Web protocol does not use HTTP trailers")
}
func (g grpcWebServerProtocol) decodeEnvelope(envBytes envelopeBytes) (envelope, error) {
flags := envBytes[0]
if flags&0b0111_1110 != 0 {
// invalid bits are set
return envelope{}, fmt.Errorf("invalid frame flags: only highest and lowest bits may be set; instead got %d", flags)
}
return envelope{
compressed: flags&1 != 0,
trailer: flags&0x80 != 0,
length: binary.BigEndian.Uint32(envBytes[1:]),
}, nil
}
func (g grpcWebServerProtocol) encodeEnvelope(env envelope) envelopeBytes {
// Request streams don't have trailers, so we can re-use the gRPC implementation
// without worrying about gRPC-Web's in-body trailers.
return grpcServerProtocol{}.encodeEnvelope(env)
}
func (g grpcWebServerProtocol) decodeEndFromMessage(_ *operation, buffer *bytes.Buffer) (responseEnd, error) {
headerLines := bytes.Split(buffer.Bytes(), []byte{'\r', '\n'})
trailers := make(http.Header, len(headerLines))
for i, headerLine := range headerLines {
// may have trailing newline, so ignore resulting trailing empty line
if len(headerLine) == 0 {
continue
}
pos := bytes.IndexByte(headerLine, ':')
if pos == -1 {
return responseEnd{}, fmt.Errorf("response body included malformed trailer at line %d", i+1)
}
trailers.Add(string(headerLine[:pos]), strings.TrimSpace(string(headerLine[pos+1:])))
}
return responseEnd{
err: grpcExtractErrorFromTrailer(trailers),
trailers: trailers,
}, nil
}
func (g grpcWebServerProtocol) String() string {
return g.protocol().String()
}
func grpcExtractRequestMeta(contentTypeShort, contentTypePrefix string, headers http.Header) (requestMeta, error) {
var reqMeta requestMeta
if err := grpcExtractTimeoutFromHeaders(headers, &reqMeta); err != nil {
return reqMeta, err
}
contentType := headers.Get("Content-Type")
if contentType == contentTypeShort {
reqMeta.codec = CodecProto
} else {
reqMeta.codec = strings.TrimPrefix(contentType, contentTypePrefix)
}
headers.Del("Content-Type")
reqMeta.compression = headers.Get("Grpc-Encoding")
headers.Del("Grpc-Encoding")
reqMeta.acceptCompression = parseMultiHeader(headers.Values("Grpc-Accept-Encoding"))
headers.Del("Grpc-Accept-Encoding")
return reqMeta, nil
}
func grpcExtractResponseMeta(contentTypeShort, contentTypePrefix string, statusCode int, headers http.Header) responseMeta {
var respMeta responseMeta
contentType := headers.Get("Content-Type")
switch {
case contentType == contentTypeShort:
respMeta.codec = CodecProto
case strings.HasPrefix(contentType, contentTypePrefix):
respMeta.codec = strings.TrimPrefix(contentType, contentTypePrefix)
default:
respMeta.codec = contentType + "?"
}
headers.Del("Content-Type")
respMeta.compression = headers.Get("Grpc-Encoding")
headers.Del("Grpc-Encoding")
respMeta.acceptCompression = parseMultiHeader(headers.Values("Grpc-Accept-Encoding"))
headers.Del("Grpc-Accept-Encoding")
// See if RPC is already over (unexpected HTTP error or trailers-only response)
if len(headers.Values("Grpc-Status")) > 0 {
connErr := grpcExtractErrorFromTrailer(headers)
respMeta.end = &responseEnd{
err: connErr,
httpCode: statusCode,
}
headers.Del("Grpc-Status")
headers.Del("Grpc-Message")
headers.Del("Grpc-Status-Details-Bin")
if contentType == "" {
// no need to report "?" codec if no content-type on a trailers-only response
respMeta.codec = ""
}
}
if statusCode != http.StatusOK {
if respMeta.end == nil {
respMeta.end = &responseEnd{}
}
if respMeta.end.err == nil {
code := httpStatusCodeToRPC(statusCode)
respMeta.end.err = connect.NewError(code, fmt.Errorf("unexpected HTTP error: %d %s", statusCode, http.StatusText(statusCode)))
}
}
return respMeta
}
func grpcAddRequestMeta(contentTypePrefix string, meta requestMeta, headers http.Header) {
headers.Set("Content-Type", contentTypePrefix+meta.codec)
if meta.compression != "" {
headers.Set("Grpc-Encoding", meta.compression)
}
if len(meta.acceptCompression) > 0 {
headers.Set("Grpc-Accept-Encoding", strings.Join(meta.acceptCompression, ", "))
}
if meta.hasTimeout {
timeoutStr := grpcEncodeTimeout(meta.timeout)
headers.Set("Grpc-Timeout", timeoutStr)
}
}
func grpcAddResponseMeta(contentTypePrefix string, meta responseMeta, headers http.Header) int {
if meta.end != nil {
headers.Set("Content-Type", contentTypePrefix+meta.codec)
grpcWriteEndToTrailers(meta.end, headers)
return http.StatusOK
}
headers.Set("Content-Type", contentTypePrefix+meta.codec)
if meta.compression != "" {
headers.Set("Grpc-Encoding", meta.compression)
}
if len(meta.acceptCompression) > 0 {
headers.Set("Grpc-Accept-Encoding", strings.Join(meta.acceptCompression, ", "))
}
return http.StatusOK
}
func grpcWriteEndToTrailers(respEnd *responseEnd, trailers http.Header) {
for k, v := range respEnd.trailers {
trailers[k] = v
}
if respEnd.err == nil {
trailers.Set("Grpc-Status", "0")
trailers.Set("Grpc-Message", "")
} else {
trailers.Set("Grpc-Status", strconv.Itoa(int(respEnd.err.Code())))
trailers.Set("Grpc-Message", grpcPercentEncode(respEnd.err.Message()))
if len(respEnd.err.Details()) == 0 {
return
}
stat := grpcStatusFromError(respEnd.err)
bin, err := proto.Marshal(stat)
if err == nil {
trailers.Set("Grpc-Status-Details-Bin", connect.EncodeBinaryHeader(bin))
}
}
}
func grpcStatusFromError(err *connect.Error) *status.Status {
stat := &status.Status{
Code: int32(err.Code()),
Message: err.Message(),
}
if details := err.Details(); len(details) > 0 {
stat.Details = make([]*anypb.Any, len(details))
for i, detail := range details {
stat.Details[i] = &anypb.Any{
TypeUrl: "type.googleapis.com/" + detail.Type(),
Value: detail.Bytes(),
}
}
}
return stat
}
// grpcPercentEncode follows RFC 3986 Section 2.1 and the gRPC HTTP/2 spec.
// It's a variant of URL-encoding with fewer reserved characters. It's intended
// to take UTF-8 encoded text and escape non-ASCII bytes so that they're valid
// HTTP/1 headers, while still maximizing readability of the data on the wire.
//
// The grpc-message trailer (used for human-readable error messages) should be
// percent-encoded.
//
// References:
//
// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#responses
// https://datatracker.ietf.org/doc/html/rfc3986#section-2.1
func grpcPercentEncode(msg string) string {
var hexCount int
for i := 0; i < len(msg); i++ {
if grpcShouldEscape(msg[i]) {
hexCount++
}
}
if hexCount == 0 {
return msg
}
// We need to escape some characters, so we'll need to allocate a new string.
var out strings.Builder
out.Grow(len(msg) + 2*hexCount)
for i := 0; i < len(msg); i++ {
switch char := msg[i]; {
case grpcShouldEscape(char):
out.WriteByte('%')
out.WriteByte(upperhex[char>>4])
out.WriteByte(upperhex[char&15])
default:
out.WriteByte(char)
}
}
return out.String()
}
func grpcPercentDecode(input string) (string, error) {
percentCount := 0
for i := 0; i < len(input); {
switch input[i] {
case '%':
percentCount++
if err := validateHex(input[i:]); err != nil {
return "", err
}
i += 3
default:
i++
}
}
if percentCount == 0 {
return input, nil
}
// We need to unescape some characters, so we'll need to allocate a new string.
var out strings.Builder
out.Grow(len(input) - 2*percentCount)
for i := 0; i < len(input); i++ {
switch input[i] {
case '%':
out.WriteByte(unhex(input[i+1])<<4 | unhex(input[i+2]))
i += 2
default:
out.WriteByte(input[i])
}
}
return out.String(), nil
}
// Characters that need to be escaped are defined in gRPC's HTTP/2 spec.
// They're different from the generic set defined in RFC 3986.
func grpcShouldEscape(char byte) bool {
return char < ' ' || char > '~' || char == '%'
}
// The gRPC wire protocol specifies that errors should be serialized using the
// binary Protobuf format, even if the messages in the request/response stream
// use a different codec. Consequently, this function needs a Protobuf codec to
// unmarshal error information in the headers.
func grpcExtractErrorFromTrailer(trailers http.Header) *connect.Error {
grpcStatus := trailers.Get("Grpc-Status")
grpcMsg := trailers.Get("Grpc-Message")
grpcDetails := trailers.Get("Grpc-Status-Details-Bin")
trailers.Del("Grpc-Status")
trailers.Del("Grpc-Message")
trailers.Del("Grpc-Status-Details-Bin")
codeHeader := grpcStatus
if codeHeader == "" {
return connect.NewError(
connect.CodeInternal,
protocolError("missing trailer header %q", "Grpc-Status"),
)
}
if codeHeader == "0" {
return nil
}
code, err := strconv.ParseUint(codeHeader, 10 /* base */, 32 /* bitsize */)
if err != nil {
return connect.NewError(
connect.CodeInternal,
protocolError("invalid error code %q: %w", codeHeader, err),
)
}
if code == 0 {
return nil
}
if len(grpcDetails) == 0 {
message, err := grpcPercentDecode(grpcMsg)
if err != nil {
return connect.NewError(
connect.CodeInternal,
protocolError("invalid grpc-message trailer: %w", err),
)
}
return connect.NewWireError(connect.Code(code), errors.New(message))
}
// Prefer the Protobuf-encoded data to the headers (grpc-go does this too).
detailsBinary, err := connect.DecodeBinaryHeader(grpcDetails)
if err != nil {
return connect.NewError(
connect.CodeInternal,
protocolError("invalid grpc-status-details-bin trailer: %w", err),
)
}
var stat status.Status
if err := proto.Unmarshal(detailsBinary, &stat); err != nil {
return connect.NewError(
connect.CodeInternal,
protocolError("invalid protobuf for error details: %w", err),
)
}
trailerErr := connect.NewWireError(connect.Code(stat.GetCode()), errors.New(stat.GetMessage()))
for _, msg := range stat.GetDetails() {
errDetail, err := connect.NewErrorDetail(msg)
if err != nil {
// shouldn't happen since msg is an Any and doesn't need to be marshalled
continue
}
trailerErr.AddDetail(errDetail)
}
return trailerErr
}
func grpcExtractTimeoutFromHeaders(headers http.Header, meta *requestMeta) error {
timeoutStr := headers.Get("Grpc-Timeout")
headers.Del("Grpc-Timeout")
if timeoutStr == "" {
return nil
}
timeout, err := grpcDecodeTimeout(timeoutStr)
if err != nil {
return err
}
meta.timeout = timeout
meta.hasTimeout = true
return nil
}
func grpcDecodeTimeout(timeout string) (time.Duration, error) {
if timeout == "" {
return 0, errNoTimeout
}
unit := grpcTimeoutUnitLookup(timeout[len(timeout)-1])
if unit == 0 {
return 0, protocolError("timeout %q has invalid unit", timeout)
}
num, err := strconv.ParseInt(timeout[:len(timeout)-1], 10 /* base */, 64 /* bitsize */)
if err != nil || num < 0 {
return 0, protocolError("invalid timeout %q", timeout)
}
if num > 99999999 { // timeout must be ASCII string of at most 8 digits
return 0, protocolError("timeout %q is too long", timeout)
}
const grpcTimeoutMaxHours = 8
if unit == time.Hour && num > grpcTimeoutMaxHours {
// Timeout is effectively unbounded, so ignore it. The grpc-go
// implementation does the same thing.
return 0, errNoTimeout
}
return time.Duration(num) * unit, nil
}
func grpcEncodeTimeout(timeout time.Duration) string {
if timeout <= 0 {
return "0n"
}
const grpcTimeoutMaxValue = 1e8
var (
size time.Duration
unit byte
)
switch {
case timeout < time.Nanosecond*grpcTimeoutMaxValue:
size, unit = time.Nanosecond, 'n'
case timeout < time.Microsecond*grpcTimeoutMaxValue:
size, unit = time.Microsecond, 'u'
case timeout < time.Millisecond*grpcTimeoutMaxValue:
size, unit = time.Millisecond, 'm'
case timeout < time.Second*grpcTimeoutMaxValue:
size, unit = time.Second, 'S'
case timeout < time.Minute*grpcTimeoutMaxValue:
size, unit = time.Minute, 'M'
default:
size, unit = time.Hour, 'H'
}
value := timeout / size
return strconv.FormatInt(int64(value), 10 /* base */) + string(unit)
}
func grpcTimeoutUnitLookup(unit byte) time.Duration {
switch unit {
case 'n':
return time.Nanosecond
case 'u':
return time.Microsecond
case 'm':
return time.Millisecond
case 'S':
return time.Second
case 'M':
return time.Minute
case 'H':
return time.Hour
default:
return 0
}
}