| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package prpc | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 "strconv" | |
| 10 "time" | |
| 11 ) | |
| 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 | |
| 18 // https://github.com/grpc/grpc-go/blob/6a026b9f108b49838491178e5d9bf7a4dcf32cf2
/transport/http_util.go#L295 | |
| 19 | |
| 20 type timeoutUnit uint8 | |
| 21 | |
| 22 const ( | |
| 23 hour timeoutUnit = 'H' | |
| 24 minute timeoutUnit = 'M' | |
| 25 second timeoutUnit = 'S' | |
| 26 millisecond timeoutUnit = 'm' | |
| 27 microsecond timeoutUnit = 'u' | |
| 28 nanosecond timeoutUnit = 'n' | |
| 29 ) | |
| 30 | |
| 31 func timeoutUnitToDuration(u timeoutUnit) (d time.Duration, ok bool) { | |
| 32 switch u { | |
| 33 case hour: | |
| 34 return time.Hour, true | |
| 35 case minute: | |
| 36 return time.Minute, true | |
| 37 case second: | |
| 38 return time.Second, true | |
| 39 case millisecond: | |
| 40 return time.Millisecond, true | |
| 41 case microsecond: | |
| 42 return time.Microsecond, true | |
| 43 case nanosecond: | |
| 44 return time.Nanosecond, true | |
| 45 default: | |
| 46 } | |
| 47 return | |
| 48 } | |
| 49 | |
| 50 func decodeTimeout(s string) (time.Duration, error) { | |
| 51 size := len(s) | |
| 52 if size < 2 { | |
| 53 return 0, fmt.Errorf("too short: %q", s) | |
| 54 } | |
| 55 unit := timeoutUnit(s[size-1]) | |
| 56 d, ok := timeoutUnitToDuration(unit) | |
| 57 if !ok { | |
| 58 return 0, fmt.Errorf("unit is not recognized: %q", s) | |
| 59 } | |
| 60 t, err := strconv.ParseInt(s[:size-1], 10, 64) | |
| 61 if err != nil { | |
| 62 return 0, err | |
| 63 } | |
| 64 return d * time.Duration(t), nil | |
| 65 } | |
| OLD | NEW |