gfpool.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright GoFrame Author(https://goframe.org). 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 gfpool provides io-reusable pool for file pointer.
  7. package gfpool
  8. import (
  9. "github.com/gogf/gf/container/gmap"
  10. "github.com/gogf/gf/container/gpool"
  11. "github.com/gogf/gf/container/gtype"
  12. "os"
  13. "time"
  14. )
  15. // File pointer pool.
  16. type Pool struct {
  17. id *gtype.Int // Pool id, which is used to mark this pool whether recreated.
  18. pool *gpool.Pool // Underlying pool.
  19. init *gtype.Bool // Whether initialized, used for marking this file added to fsnotify, and it can only be added just once.
  20. ttl time.Duration // Time to live for file pointer items.
  21. }
  22. // File is an item in the pool.
  23. type File struct {
  24. *os.File // Underlying file pointer.
  25. stat os.FileInfo // State of current file pointer.
  26. pid int // Belonging pool id, which is set when file pointer created. It's used to check whether the pool is recreated.
  27. pool *Pool // Belonging ool.
  28. flag int // Flash for opening file.
  29. perm os.FileMode // Permission for opening file.
  30. path string // Absolute path of the file.
  31. }
  32. var (
  33. // Global file pointer pool.
  34. pools = gmap.NewStrAnyMap(true)
  35. )