OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package prpc | 5 package prpc |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "strconv" | 9 "strconv" |
10 "time" | 10 "time" |
11 ) | 11 ) |
12 | 12 |
13 // headerTimeout is HTTP header used to set pRPC request timeout. | |
14 // The single value should match regexp `\d+[HMSmun]`. | |
15 const headerTimeout = "X-Prpc-Timeout" | |
16 | |
17 // The rest of this file is adapted from | 13 // The rest of this file is adapted from |
18 // https://github.com/grpc/grpc-go/blob/6a026b9f108b49838491178e5d9bf7a4dcf32cf2
/transport/http_util.go#L295 | 14 // https://github.com/grpc/grpc-go/blob/6a026b9f108b49838491178e5d9bf7a4dcf32cf2
/transport/http_util.go#L295 |
19 | 15 |
20 type timeoutUnit uint8 | 16 type timeoutUnit uint8 |
21 | 17 |
22 const ( | 18 const ( |
23 hour timeoutUnit = 'H' | 19 hour timeoutUnit = 'H' |
24 minute timeoutUnit = 'M' | 20 minute timeoutUnit = 'M' |
25 second timeoutUnit = 'S' | 21 second timeoutUnit = 'S' |
26 millisecond timeoutUnit = 'm' | 22 millisecond timeoutUnit = 'm' |
(...skipping 13 matching lines...) Expand all Loading... |
40 return time.Millisecond, true | 36 return time.Millisecond, true |
41 case microsecond: | 37 case microsecond: |
42 return time.Microsecond, true | 38 return time.Microsecond, true |
43 case nanosecond: | 39 case nanosecond: |
44 return time.Nanosecond, true | 40 return time.Nanosecond, true |
45 default: | 41 default: |
46 } | 42 } |
47 return | 43 return |
48 } | 44 } |
49 | 45 |
50 func decodeTimeout(s string) (time.Duration, error) { | 46 // DecodeTimeout decodes a gRPC/pRPC timeout header string into a time.Duration. |
| 47 func DecodeTimeout(s string) (time.Duration, error) { |
51 size := len(s) | 48 size := len(s) |
52 if size < 2 { | 49 if size < 2 { |
53 return 0, fmt.Errorf("too short: %q", s) | 50 return 0, fmt.Errorf("too short: %q", s) |
54 } | 51 } |
55 unit := timeoutUnit(s[size-1]) | 52 unit := timeoutUnit(s[size-1]) |
56 d, ok := timeoutUnitToDuration(unit) | 53 d, ok := timeoutUnitToDuration(unit) |
57 if !ok { | 54 if !ok { |
58 return 0, fmt.Errorf("unit is not recognized: %q", s) | 55 return 0, fmt.Errorf("unit is not recognized: %q", s) |
59 } | 56 } |
60 t, err := strconv.ParseInt(s[:size-1], 10, 64) | 57 t, err := strconv.ParseInt(s[:size-1], 10, 64) |
61 if err != nil { | 58 if err != nil { |
62 return 0, err | 59 return 0, err |
63 } | 60 } |
64 return d * time.Duration(t), nil | 61 return d * time.Duration(t), nil |
65 } | 62 } |
| 63 |
| 64 const maxTimeoutValue int64 = 100000000 - 1 |
| 65 |
| 66 // div does integer division and round-up the result. Note that this is |
| 67 // equivalent to (d+r-1)/r but has less chance to overflow. |
| 68 func div(d, r time.Duration) int64 { |
| 69 if m := d % r; m > 0 { |
| 70 return int64(d/r + 1) |
| 71 } |
| 72 return int64(d / r) |
| 73 } |
| 74 |
| 75 // EncodeTimeout encodes a gRPC/pRPC timeout into a timeout header |
| 76 // time.Duration. |
| 77 // |
| 78 // This rounds the time.Duration to the smallest time unit that can represent |
| 79 // it. |
| 80 func EncodeTimeout(t time.Duration) string { |
| 81 if d := div(t, time.Nanosecond); d <= maxTimeoutValue { |
| 82 return strconv.FormatInt(d, 10) + "n" |
| 83 } |
| 84 if d := div(t, time.Microsecond); d <= maxTimeoutValue { |
| 85 return strconv.FormatInt(d, 10) + "u" |
| 86 } |
| 87 if d := div(t, time.Millisecond); d <= maxTimeoutValue { |
| 88 return strconv.FormatInt(d, 10) + "m" |
| 89 } |
| 90 if d := div(t, time.Second); d <= maxTimeoutValue { |
| 91 return strconv.FormatInt(d, 10) + "S" |
| 92 } |
| 93 if d := div(t, time.Minute); d <= maxTimeoutValue { |
| 94 return strconv.FormatInt(d, 10) + "M" |
| 95 } |
| 96 // Note that maxTimeoutValue * time.Hour > MaxInt64. |
| 97 return strconv.FormatInt(div(t, time.Hour), 10) + "H" |
| 98 } |
OLD | NEW |