grand_buffer.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package grand
  7. import (
  8. "crypto/rand"
  9. )
  10. const (
  11. // Buffer size for uint32 random number.
  12. gBUFFER_SIZE = 10000
  13. )
  14. var (
  15. // bufferChan is the buffer for random bytes,
  16. // every item storing 4 bytes.
  17. bufferChan = make(chan []byte, gBUFFER_SIZE)
  18. )
  19. func init() {
  20. go asyncProducingRandomBufferBytesLoop()
  21. }
  22. // asyncProducingRandomBufferBytes is a named goroutine, which uses a asynchronous goroutine
  23. // to produce the random bytes, and a buffer chan to store the random bytes.
  24. // So it has high performance to generate random numbers.
  25. func asyncProducingRandomBufferBytesLoop() {
  26. var step int
  27. for {
  28. buffer := make([]byte, 1024)
  29. if n, err := rand.Read(buffer); err != nil {
  30. panic(err)
  31. } else {
  32. // The random buffer from system is very expensive,
  33. // so fully reuse the random buffer by changing
  34. // the step with a different number can
  35. // improve the performance a lot.
  36. // for _, step = range []int{4, 5, 6, 7} {
  37. for _, step = range []int{4} {
  38. for i := 0; i <= n-4; i += step {
  39. bufferChan <- buffer[i : i+4]
  40. }
  41. }
  42. }
  43. }
  44. }