utils.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. // Permission is hereby granted, free of charge, to any person obtaining a copy
  3. // of this software and associated documentation files (the "Software"), to deal
  4. // in the Software without restriction, including without limitation the rights
  5. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. // copies of the Software, and to permit persons to whom the Software is
  7. // furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in
  10. // all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. // THE SOFTWARE.
  19. package utils
  20. import (
  21. "encoding/binary"
  22. "errors"
  23. "net"
  24. "strconv"
  25. "strings"
  26. "time"
  27. )
  28. var (
  29. // ErrEmptyIP an error for empty ip strings
  30. ErrEmptyIP = errors.New("empty string given for ip")
  31. // ErrNotHostColonPort an error for invalid host port string
  32. ErrNotHostColonPort = errors.New("expecting host:port")
  33. // ErrNotFourOctets an error for the wrong number of octets after splitting a string
  34. ErrNotFourOctets = errors.New("Wrong number of octets")
  35. )
  36. // ParseIPToUint32 converts a string ip (e.g. "x.y.z.w") to an uint32
  37. func ParseIPToUint32(ip string) (uint32, error) {
  38. if ip == "" {
  39. return 0, ErrEmptyIP
  40. }
  41. if ip == "localhost" {
  42. return 127<<24 | 1, nil
  43. }
  44. octets := strings.Split(ip, ".")
  45. if len(octets) != 4 {
  46. return 0, ErrNotFourOctets
  47. }
  48. var intIP uint32
  49. for i := 0; i < 4; i++ {
  50. octet, err := strconv.Atoi(octets[i])
  51. if err != nil {
  52. return 0, err
  53. }
  54. intIP = (intIP << 8) | uint32(octet)
  55. }
  56. return intIP, nil
  57. }
  58. // ParsePort converts port number from string to uin16
  59. func ParsePort(portString string) (uint16, error) {
  60. port, err := strconv.ParseUint(portString, 10, 16)
  61. return uint16(port), err
  62. }
  63. // PackIPAsUint32 packs an IPv4 as uint32
  64. func PackIPAsUint32(ip net.IP) uint32 {
  65. if ipv4 := ip.To4(); ipv4 != nil {
  66. return binary.BigEndian.Uint32(ipv4)
  67. }
  68. return 0
  69. }
  70. // TimeToMicrosecondsSinceEpochInt64 converts Go time.Time to a long
  71. // representing time since epoch in microseconds, which is used expected
  72. // in the Jaeger spans encoded as Thrift.
  73. func TimeToMicrosecondsSinceEpochInt64(t time.Time) int64 {
  74. // ^^^ Passing time.Time by value is faster than passing a pointer!
  75. // BenchmarkTimeByValue-8 2000000000 1.37 ns/op
  76. // BenchmarkTimeByPtr-8 2000000000 1.98 ns/op
  77. return t.UnixNano() / 1000
  78. }