@@ -249,3 +249,92 @@ func ExampleTransaction_error() {
249249 // -5
250250 // error
251251}
252+
253+ func ExampleNewThrottle () {
254+ throttle , reset := NewThrottle (100 * time .Millisecond , func () {
255+ fmt .Println ("Called once in every 100ms" )
256+ })
257+
258+ for j := 0 ; j < 10 ; j ++ {
259+ throttle ()
260+ time .Sleep (30 * time .Millisecond )
261+ }
262+
263+ reset ()
264+
265+ // Output:
266+ // Called once in every 100ms
267+ // Called once in every 100ms
268+ // Called once in every 100ms
269+ }
270+
271+ func ExampleNewThrottleWithCount () {
272+ throttle , reset := NewThrottleWithCount (100 * time .Millisecond , 2 , func () {
273+ fmt .Println ("Called once in every 100ms" )
274+ })
275+
276+ for j := 0 ; j < 10 ; j ++ {
277+ throttle ()
278+ time .Sleep (30 * time .Millisecond )
279+ }
280+
281+ reset ()
282+
283+ // Output:
284+ // Called once in every 100ms
285+ // Called once in every 100ms
286+ // Called once in every 100ms
287+ // Called once in every 100ms
288+ // Called once in every 100ms
289+ // Called once in every 100ms
290+ }
291+
292+ func ExampleNewThrottleBy () {
293+ throttle , reset := NewThrottleBy (100 * time .Millisecond , func (key string ) {
294+ fmt .Println (key , "Called once in every 100ms" )
295+ })
296+
297+ for j := 0 ; j < 10 ; j ++ {
298+ throttle ("foo" )
299+ throttle ("bar" )
300+ time .Sleep (30 * time .Millisecond )
301+ }
302+
303+ reset ()
304+
305+ // Output:
306+ // foo Called once in every 100ms
307+ // bar Called once in every 100ms
308+ // foo Called once in every 100ms
309+ // bar Called once in every 100ms
310+ // foo Called once in every 100ms
311+ // bar Called once in every 100ms
312+ }
313+
314+ func ExampleNewThrottleByWithCount () {
315+ throttle , reset := NewThrottleByWithCount (100 * time .Millisecond , 2 , func (key string ) {
316+ fmt .Println (key , "Called once in every 100ms" )
317+ })
318+
319+ for j := 0 ; j < 10 ; j ++ {
320+ throttle ("foo" )
321+ throttle ("bar" )
322+ time .Sleep (30 * time .Millisecond )
323+ }
324+
325+ reset ()
326+
327+ // Output:
328+ // foo Called once in every 100ms
329+ // bar Called once in every 100ms
330+ // foo Called once in every 100ms
331+ // bar Called once in every 100ms
332+ // foo Called once in every 100ms
333+ // bar Called once in every 100ms
334+ // foo Called once in every 100ms
335+ // bar Called once in every 100ms
336+ // foo Called once in every 100ms
337+ // bar Called once in every 100ms
338+ // foo Called once in every 100ms
339+ // bar Called once in every 100ms
340+ }
0 commit comments