http urls monitor.

0doc.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. /*
  4. Package codec provides a
  5. High Performance, Feature-Rich Idiomatic Go 1.4+ codec/encoding library
  6. for binc, msgpack, cbor, json.
  7. Supported Serialization formats are:
  8. - msgpack: https://github.com/msgpack/msgpack
  9. - binc: http://github.com/ugorji/binc
  10. - cbor: http://cbor.io http://tools.ietf.org/html/rfc7049
  11. - json: http://json.org http://tools.ietf.org/html/rfc7159
  12. - simple:
  13. To install:
  14. go get github.com/ugorji/go/codec
  15. This package will carefully use 'unsafe' for performance reasons in specific places.
  16. You can build without unsafe use by passing the safe or appengine tag
  17. i.e. 'go install -tags=safe ...'. Note that unsafe is only supported for the last 3
  18. go sdk versions e.g. current go release is go 1.9, so we support unsafe use only from
  19. go 1.7+ . This is because supporting unsafe requires knowledge of implementation details.
  20. For detailed usage information, read the primer at http://ugorji.net/blog/go-codec-primer .
  21. The idiomatic Go support is as seen in other encoding packages in
  22. the standard library (ie json, xml, gob, etc).
  23. Rich Feature Set includes:
  24. - Simple but extremely powerful and feature-rich API
  25. - Support for go1.4 and above, while selectively using newer APIs for later releases
  26. - Excellent code coverage ( > 90% )
  27. - Very High Performance.
  28. Our extensive benchmarks show us outperforming Gob, Json, Bson, etc by 2-4X.
  29. - Careful selected use of 'unsafe' for targeted performance gains.
  30. 100% mode exists where 'unsafe' is not used at all.
  31. - Lock-free (sans mutex) concurrency for scaling to 100's of cores
  32. - Coerce types where appropriate
  33. e.g. decode an int in the stream into a float, decode numbers from formatted strings, etc
  34. - Corner Cases:
  35. Overflows, nil maps/slices, nil values in streams are handled correctly
  36. - Standard field renaming via tags
  37. - Support for omitting empty fields during an encoding
  38. - Encoding from any value and decoding into pointer to any value
  39. (struct, slice, map, primitives, pointers, interface{}, etc)
  40. - Extensions to support efficient encoding/decoding of any named types
  41. - Support encoding.(Binary|Text)(M|Unm)arshaler interfaces
  42. - Support IsZero() bool to determine if a value is a zero value.
  43. Analogous to time.Time.IsZero() bool.
  44. - Decoding without a schema (into a interface{}).
  45. Includes Options to configure what specific map or slice type to use
  46. when decoding an encoded list or map into a nil interface{}
  47. - Mapping a non-interface type to an interface, so we can decode appropriately
  48. into any interface type with a correctly configured non-interface value.
  49. - Encode a struct as an array, and decode struct from an array in the data stream
  50. - Option to encode struct keys as numbers (instead of strings)
  51. (to support structured streams with fields encoded as numeric codes)
  52. - Comprehensive support for anonymous fields
  53. - Fast (no-reflection) encoding/decoding of common maps and slices
  54. - Code-generation for faster performance.
  55. - Support binary (e.g. messagepack, cbor) and text (e.g. json) formats
  56. - Support indefinite-length formats to enable true streaming
  57. (for formats which support it e.g. json, cbor)
  58. - Support canonical encoding, where a value is ALWAYS encoded as same sequence of bytes.
  59. This mostly applies to maps, where iteration order is non-deterministic.
  60. - NIL in data stream decoded as zero value
  61. - Never silently skip data when decoding.
  62. User decides whether to return an error or silently skip data when keys or indexes
  63. in the data stream do not map to fields in the struct.
  64. - Detect and error when encoding a cyclic reference (instead of stack overflow shutdown)
  65. - Encode/Decode from/to chan types (for iterative streaming support)
  66. - Drop-in replacement for encoding/json. `json:` key in struct tag supported.
  67. - Provides a RPC Server and Client Codec for net/rpc communication protocol.
  68. - Handle unique idiosyncrasies of codecs e.g.
  69. - For messagepack, configure how ambiguities in handling raw bytes are resolved
  70. - For messagepack, provide rpc server/client codec to support
  71. msgpack-rpc protocol defined at:
  72. https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md
  73. Extension Support
  74. Users can register a function to handle the encoding or decoding of
  75. their custom types.
  76. There are no restrictions on what the custom type can be. Some examples:
  77. type BisSet []int
  78. type BitSet64 uint64
  79. type UUID string
  80. type MyStructWithUnexportedFields struct { a int; b bool; c []int; }
  81. type GifImage struct { ... }
  82. As an illustration, MyStructWithUnexportedFields would normally be
  83. encoded as an empty map because it has no exported fields, while UUID
  84. would be encoded as a string. However, with extension support, you can
  85. encode any of these however you like.
  86. Custom Encoding and Decoding
  87. This package maintains symmetry in the encoding and decoding halfs.
  88. We determine how to encode or decode by walking this decision tree
  89. - is type a codec.Selfer?
  90. - is there an extension registered for the type?
  91. - is format binary, and is type a encoding.BinaryMarshaler and BinaryUnmarshaler?
  92. - is format specifically json, and is type a encoding/json.Marshaler and Unmarshaler?
  93. - is format text-based, and type an encoding.TextMarshaler?
  94. - else we use a pair of functions based on the "kind" of the type e.g. map, slice, int64, etc
  95. This symmetry is important to reduce chances of issues happening because the
  96. encoding and decoding sides are out of sync e.g. decoded via very specific
  97. encoding.TextUnmarshaler but encoded via kind-specific generalized mode.
  98. Consequently, if a type only defines one-half of the symmetry
  99. (e.g. it implements UnmarshalJSON() but not MarshalJSON() ),
  100. then that type doesn't satisfy the check and we will continue walking down the
  101. decision tree.
  102. RPC
  103. RPC Client and Server Codecs are implemented, so the codecs can be used
  104. with the standard net/rpc package.
  105. Usage
  106. The Handle is SAFE for concurrent READ, but NOT SAFE for concurrent modification.
  107. The Encoder and Decoder are NOT safe for concurrent use.
  108. Consequently, the usage model is basically:
  109. - Create and initialize the Handle before any use.
  110. Once created, DO NOT modify it.
  111. - Multiple Encoders or Decoders can now use the Handle concurrently.
  112. They only read information off the Handle (never write).
  113. - However, each Encoder or Decoder MUST not be used concurrently
  114. - To re-use an Encoder/Decoder, call Reset(...) on it first.
  115. This allows you use state maintained on the Encoder/Decoder.
  116. Sample usage model:
  117. // create and configure Handle
  118. var (
  119. bh codec.BincHandle
  120. mh codec.MsgpackHandle
  121. ch codec.CborHandle
  122. )
  123. mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
  124. // configure extensions
  125. // e.g. for msgpack, define functions and enable Time support for tag 1
  126. // mh.SetExt(reflect.TypeOf(time.Time{}), 1, myExt)
  127. // create and use decoder/encoder
  128. var (
  129. r io.Reader
  130. w io.Writer
  131. b []byte
  132. h = &bh // or mh to use msgpack
  133. )
  134. dec = codec.NewDecoder(r, h)
  135. dec = codec.NewDecoderBytes(b, h)
  136. err = dec.Decode(&v)
  137. enc = codec.NewEncoder(w, h)
  138. enc = codec.NewEncoderBytes(&b, h)
  139. err = enc.Encode(v)
  140. //RPC Server
  141. go func() {
  142. for {
  143. conn, err := listener.Accept()
  144. rpcCodec := codec.GoRpc.ServerCodec(conn, h)
  145. //OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
  146. rpc.ServeCodec(rpcCodec)
  147. }
  148. }()
  149. //RPC Communication (client side)
  150. conn, err = net.Dial("tcp", "localhost:5555")
  151. rpcCodec := codec.GoRpc.ClientCodec(conn, h)
  152. //OR rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, h)
  153. client := rpc.NewClientWithCodec(rpcCodec)
  154. Running Tests
  155. To run tests, use the following:
  156. go test
  157. To run the full suite of tests, use the following:
  158. go test -tags alltests -run Suite
  159. You can run the tag 'safe' to run tests or build in safe mode. e.g.
  160. go test -tags safe -run Json
  161. go test -tags "alltests safe" -run Suite
  162. Running Benchmarks
  163. Please see http://github.com/ugorji/go-codec-bench .
  164. Caveats
  165. Struct fields matching the following are ignored during encoding and decoding
  166. - struct tag value set to -
  167. - func, complex numbers, unsafe pointers
  168. - unexported and not embedded
  169. - unexported and embedded and not struct kind
  170. - unexported and embedded pointers (from go1.10)
  171. Every other field in a struct will be encoded/decoded.
  172. Embedded fields are encoded as if they exist in the top-level struct,
  173. with some caveats. See Encode documentation.
  174. */
  175. package codec
  176. // TODO:
  177. // - For Go 1.11, when mid-stack inlining is enabled,
  178. // we should use committed functions for writeXXX and readXXX calls.
  179. // This involves uncommenting the methods for decReaderSwitch and encWriterSwitch
  180. // and using those (decReaderSwitch and encWriterSwitch) in all handles
  181. // instead of encWriter and decReader.
  182. // The benefit is that, for the (En|De)coder over []byte, the encWriter/decReader
  183. // will be inlined, giving a performance bump for that typical case.
  184. // However, it will only be inlined if mid-stack inlining is enabled,
  185. // as we call panic to raise errors, and panic currently prevents inlining.
  186. //
  187. // PUNTED:
  188. // - To make Handle comparable, make extHandle in BasicHandle a non-embedded pointer,
  189. // and use overlay methods on *BasicHandle to call through to extHandle after initializing
  190. // the "xh *extHandle" to point to a real slice.
  191. //
  192. // BEFORE EACH RELEASE:
  193. // - Look through and fix padding for each type, to eliminate false sharing
  194. // - critical shared objects that are read many times
  195. // TypeInfos
  196. // - pooled objects:
  197. // decNaked, decNakedContainers, codecFner, typeInfoLoadArray,
  198. // - small objects allocated independently, that we read/use much across threads:
  199. // codecFn, typeInfo
  200. // - Objects allocated independently and used a lot
  201. // Decoder, Encoder,
  202. // xxxHandle, xxxEncDriver, xxxDecDriver (xxx = json, msgpack, cbor, binc, simple)
  203. // - In all above, arrange values modified together to be close to each other.
  204. //
  205. // For all of these, either ensure that they occupy full cache lines,
  206. // or ensure that the things just past the cache line boundary are hardly read/written
  207. // e.g. JsonHandle.RawBytesExt - which is copied into json(En|De)cDriver at init
  208. //
  209. // Occupying full cache lines means they occupy 8*N words (where N is an integer).
  210. // Check this out by running: ./run.sh -z
  211. // - look at those tagged ****, meaning they are not occupying full cache lines
  212. // - look at those tagged <<<<, meaning they are larger than 32 words (something to watch)
  213. // - Run "golint -min_confidence 0.81"