|
27 | 27 | <h1>uuid</h1>
|
28 | 28 |
|
29 | 29 |
|
30 |
| -<h2>Contents</h2> |
31 |
| -<ul> |
32 |
| -<li><a href="#Functions">Functions</a></li> |
33 |
| -</ul> |
| 30 | + |
34 | 31 |
|
35 | 32 |
|
36 | 33 | <h2>Modules</h2>
|
37 | 34 | <ul class="nowrap">
|
38 |
| - <li><strong>uuid</strong></li> |
| 35 | + <li><a href="modules/uuid.html">uuid</a></li> |
| 36 | + <li><a href="modules/uuid.rng.html">uuid.rng</a></li> |
39 | 37 | </ul>
|
40 | 38 | <h2>Topics</h2>
|
41 | 39 | <ul class="">
|
42 |
| - <li><a href="topics/readme.md.html">readme</a></li> |
| 40 | + <li><a href="topics/01-introduction.md.html">1. Introduction</a></li> |
| 41 | + <li><a href="topics/CHANGELOG.md.html">CHANGELOG</a></li> |
| 42 | + <li><a href="topics/LICENSE.md.html">Apache 2.0 License</a></li> |
43 | 43 | </ul>
|
44 | 44 |
|
45 | 45 | </div>
|
46 | 46 |
|
47 | 47 | <div id="content">
|
48 | 48 |
|
49 |
| -<h1>Module <code>uuid</code></h1> |
50 |
| -<p>Copyright 2012 Rackspace (original), 2013-2021 Thijs Schreijer (modifications)</p> |
51 |
| - |
52 |
| -<p> Licensed under the Apache License, Version 2.0 (the "License"); |
53 |
| - you may not use this file except in compliance with the License.</p> |
54 |
| -<p> You may obtain a copy of the License at</p> |
55 |
| - |
56 |
| - |
57 |
| -<pre> |
58 |
| -http://www.apache.org/licenses/LICENSE-<span class="number">2.0</span> |
59 |
| -</pre> |
60 |
| - |
61 |
| -<p> Unless required by applicable law or agreed to in writing, software |
62 |
| - distributed under the License is distributed on an "AS-IS" BASIS, |
63 |
| - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
64 |
| - See the License for the specific language governing permissions and |
65 |
| - limitations under the License.</p> |
66 |
| - |
67 |
| -<p> see http://www.ietf.org/rfc/rfc4122.txt</p> |
68 | 49 |
|
69 |
| -<p> Note that this is not a true version 4 (random) UUID. Since <code>os.time()</code> precision is only 1 second, it would be hard |
70 |
| - to guarantee spacial uniqueness when two hosts generate a uuid after being seeded during the same second. This |
71 |
| - is solved by using the node field from a version 1 UUID. It represents the mac address.</p> |
| 50 | + <h2>Module to generate uuids in pure Lua</h2> |
72 | 51 |
|
73 |
| -<p> 28-apr-2013 modified by Thijs Schreijer from the original <a href="https://github.com/kans/zirgo/blob/807250b1af6725bad4776c931c89a784c1e34db2/util/uuid.lua">Rackspace code</a> as a generic Lua module. |
74 |
| - Regarding the above mention on <code>os.time()</code>; the modifications use the <code>socket.gettime()</code> function from LuaSocket |
75 |
| - if available and hence reduce that problem (provided LuaSocket has been loaded before uuid).</p> |
76 |
| - |
77 |
| -<p> <strong>Important:</strong> the random seed is a global piece of data. Hence setting it is |
78 |
| - an application level responsibility, libraries should never set it!</p> |
79 |
| - |
80 |
| -<p> See this issue; <a href="https://github.com/Kong/kong/issues/478">https://github.com/Kong/kong/issues/478</a> |
81 |
| - It demonstrates the problem of using time as a random seed. Specifically when used from multiple processes. |
82 |
| - So make sure to seed only once, application wide. And to not have multiple processes do that |
83 |
| - simultaneously.</p> |
84 |
| - |
85 |
| - |
86 |
| -<h2><a href="#Functions">Functions</a></h2> |
87 |
| -<table class="function_list"> |
| 52 | +<h2>Modules</h2> |
| 53 | +<table class="module_list"> |
88 | 54 | <tr>
|
89 |
| - <td class="name" nowrap><a href="#new">new (hwaddr)</a></td> |
90 |
| - <td class="summary">Creates a new uuid.</td> |
| 55 | + <td class="name" nowrap><a href="modules/uuid.html">uuid</a></td> |
| 56 | + <td class="summary">Copyright 2012 Rackspace (original), 2013-2024 Thijs Schreijer (modifications)</td> |
91 | 57 | </tr>
|
92 | 58 | <tr>
|
93 |
| - <td class="name" nowrap><a href="#randomseed">randomseed (seed)</a></td> |
94 |
| - <td class="summary">Improved randomseed function.</td> |
| 59 | + <td class="name" nowrap><a href="modules/uuid.rng.html">uuid.rng</a></td> |
| 60 | + <td class="summary">Random number generator utilities with different properties and uses.</td> |
95 | 61 | </tr>
|
| 62 | +</table> |
| 63 | +<h2>Topics</h2> |
| 64 | +<table class="module_list"> |
96 | 65 | <tr>
|
97 |
| - <td class="name" nowrap><a href="#seed">seed ()</a></td> |
98 |
| - <td class="summary">Seeds the random generator.</td> |
| 66 | + <td class="name" nowrap><a href="topics/01-introduction.md.html">01-introduction.md</a></td> |
| 67 | + <td class="summary"></td> |
| 68 | + </tr> |
| 69 | + <tr> |
| 70 | + <td class="name" nowrap><a href="topics/CHANGELOG.md.html">CHANGELOG.md</a></td> |
| 71 | + <td class="summary"></td> |
| 72 | + </tr> |
| 73 | + <tr> |
| 74 | + <td class="name" nowrap><a href="topics/LICENSE.md.html">LICENSE.md</a></td> |
| 75 | + <td class="summary"></td> |
99 | 76 | </tr>
|
100 | 77 | </table>
|
101 | 78 |
|
102 |
| -<br/> |
103 |
| -<br/> |
104 |
| - |
105 |
| - |
106 |
| - <h2 class="section-header "><a name="Functions"></a>Functions</h2> |
107 |
| - |
108 |
| - <dl class="function"> |
109 |
| - <dt> |
110 |
| - <a name = "new"></a> |
111 |
| - <strong>new (hwaddr)</strong> |
112 |
| - </dt> |
113 |
| - <dd> |
114 |
| - |
115 |
| -<p>Creates a new uuid. Either provide a unique hex string, or make sure the |
116 |
| - random seed is properly set. The module table itself is a shortcut to this |
117 |
| - function, so <code>my_uuid = uuid.new()</code> equals <code>my_uuid = uuid()</code>.</p> |
118 |
| - |
119 |
| -<p> For proper use there are 3 options;</p> |
120 |
| - |
121 |
| -<ol> |
122 |
| - <li>first require <code>luasocket</code>, then call <code>uuid.seed()</code>, and request a uuid using no |
123 |
| - parameter, eg. <code>my_uuid = uuid()</code></li> |
124 |
| - <li>use <a href="index.html#">uuid</a> without <code>luasocket</code>, set a random seed using <code>uuid.randomseed(some_good_seed)</code>, |
125 |
| - and request a uuid using no parameter, eg. <code>my_uuid = uuid()</code></li> |
126 |
| - <li>use <a href="index.html#">uuid</a> without <code>luasocket</code>, and request a uuid using an unique hex string, |
127 |
| - eg. <code>my_uuid = uuid(my_networkcard_macaddress)</code></li> |
128 |
| -</ol> |
129 |
| - |
130 |
| - |
131 |
| - |
132 |
| - |
133 |
| - <h3>Parameters:</h3> |
134 |
| - <ul> |
135 |
| - <li><span class="parameter">hwaddr</span> |
136 |
| - (optional) string containing a unique hex value (e.g.: <code>00:0c:29:69:41:c6</code>), to be used to compensate for the lesser <code>math_random()</code> function. Use a mac address for solid results. If omitted, a fully randomized uuid will be generated, but then you must ensure that the random seed is set properly! |
137 |
| - </li> |
138 |
| - </ul> |
139 |
| - |
140 |
| - <h3>Returns:</h3> |
141 |
| - <ol> |
142 |
| - |
143 |
| - a properly formatted uuid string |
144 |
| - </ol> |
145 |
| - |
146 |
| - |
147 |
| - |
148 |
| - <h3>Usage:</h3> |
149 |
| - <ul> |
150 |
| - <pre class="example"><span class="keyword">local</span> uuid = <span class="global">require</span>(<span class="string">"uuid"</span>) |
151 |
| -<span class="global">print</span>(<span class="string">"here's a new uuid: "</span>,uuid())</pre> |
152 |
| - </ul> |
153 |
| - |
154 |
| -</dd> |
155 |
| - <dt> |
156 |
| - <a name = "randomseed"></a> |
157 |
| - <strong>randomseed (seed)</strong> |
158 |
| - </dt> |
159 |
| - <dd> |
160 |
| - Improved randomseed function. |
161 |
| - Lua 5.1 and 5.2 both truncate the seed given if it exceeds the integer |
162 |
| - range. If this happens, the seed will be 0 or 1 and all randomness will |
163 |
| - be gone (each application run will generate the same sequence of random |
164 |
| - numbers in that case). This improved version drops the most significant |
165 |
| - bits in those cases to get the seed within the proper range again. |
166 |
| - |
167 |
| - |
168 |
| - <h3>Parameters:</h3> |
169 |
| - <ul> |
170 |
| - <li><span class="parameter">seed</span> |
171 |
| - the random seed to set (integer from 0 - 2^32, negative values will be made positive) |
172 |
| - </li> |
173 |
| - </ul> |
174 |
| - |
175 |
| - <h3>Returns:</h3> |
176 |
| - <ol> |
177 |
| - |
178 |
| - the (potentially modified) seed used |
179 |
| - </ol> |
180 |
| - |
181 |
| - |
182 |
| - |
183 |
| - <h3>Usage:</h3> |
184 |
| - <ul> |
185 |
| - <pre class="example"><span class="keyword">local</span> socket = <span class="global">require</span>(<span class="string">"socket"</span>) <span class="comment">-- gettime() has higher precision than os.time() |
186 |
| -</span><span class="keyword">local</span> uuid = <span class="global">require</span>(<span class="string">"uuid"</span>) |
187 |
| -<span class="comment">-- see also example at uuid.seed() |
188 |
| -</span>uuid.randomseed(socket.gettime()*<span class="number">10000</span>) |
189 |
| -<span class="global">print</span>(<span class="string">"here's a new uuid: "</span>,uuid())</pre> |
190 |
| - </ul> |
191 |
| - |
192 |
| -</dd> |
193 |
| - <dt> |
194 |
| - <a name = "seed"></a> |
195 |
| - <strong>seed ()</strong> |
196 |
| - </dt> |
197 |
| - <dd> |
198 |
| - Seeds the random generator. |
199 |
| - It does so in 3 possible ways;</p> |
200 |
| - |
201 |
| -<ol> |
202 |
| - <li>if in ngx_lua, use <code>ngx.time() + ngx.worker.pid()</code> to ensure a unique seed |
203 |
| - for each worker. It should ideally be called from the <code>init_worker</code> context.</li> |
204 |
| - <li>use luasocket <code>gettime()</code> function, but it only does so when LuaSocket |
205 |
| - has been required already.</li> |
206 |
| - <li>use <code>os.time()</code>: this only offers resolution to one second (used when |
207 |
| - LuaSocket hasn't been loaded)</li> |
208 |
| -</ol> |
209 |
| - |
210 |
| -<p> <strong>Important:</strong> the random seed is a global piece of data. Hence setting it is |
211 |
| - an application level responsibility, libraries should never set it! |
212 |
| - |
213 |
| - |
214 |
| - |
215 |
| - |
216 |
| - |
217 |
| - |
218 |
| - <h3>Usage:</h3> |
219 |
| - <ul> |
220 |
| - <pre class="example"><span class="keyword">local</span> socket = <span class="global">require</span>(<span class="string">"socket"</span>) <span class="comment">-- gettime() has higher precision than os.time() |
221 |
| -</span><span class="comment">-- LuaSocket loaded, so below line does the same as the example from randomseed() |
222 |
| -</span>uuid.seed() |
223 |
| -<span class="global">print</span>(<span class="string">"here's a new uuid: "</span>,uuid())</pre> |
224 |
| - </ul> |
225 |
| - |
226 |
| -</dd> |
227 |
| -</dl> |
228 |
| - |
229 |
| - |
230 | 79 | </div> <!-- id="content" -->
|
231 | 80 | </div> <!-- id="main" -->
|
232 | 81 | <div id="about">
|
233 |
| -<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i> |
234 |
| -<i style="float:right;">Last updated 2021-07-11 08:59:24 </i> |
| 82 | +<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i> |
| 83 | +<i style="float:right;">Last updated 2024-10-13 00:59:03 </i> |
235 | 84 | </div> <!-- id="about" -->
|
236 | 85 | </div> <!-- id="container" -->
|
237 | 86 | </body>
|
|
0 commit comments