option.go 925 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package pool
  2. import "github.com/gobwas/pool/internal/pmath"
  3. // Option configures pool.
  4. type Option func(Config)
  5. // Config describes generic pool configuration.
  6. type Config interface {
  7. AddSize(n int)
  8. SetSizeMapping(func(int) int)
  9. }
  10. // WithSizeLogRange returns an Option that will add logarithmic range of
  11. // pooling sizes containing [min, max] values.
  12. func WithLogSizeRange(min, max int) Option {
  13. return func(c Config) {
  14. pmath.LogarithmicRange(min, max, func(n int) {
  15. c.AddSize(n)
  16. })
  17. }
  18. }
  19. // WithSize returns an Option that will add given pooling size to the pool.
  20. func WithSize(n int) Option {
  21. return func(c Config) {
  22. c.AddSize(n)
  23. }
  24. }
  25. func WithSizeMapping(sz func(int) int) Option {
  26. return func(c Config) {
  27. c.SetSizeMapping(sz)
  28. }
  29. }
  30. func WithLogSizeMapping() Option {
  31. return WithSizeMapping(pmath.CeilToPowerOfTwo)
  32. }
  33. func WithIdentitySizeMapping() Option {
  34. return WithSizeMapping(pmath.Identity)
  35. }