writer.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package stackless
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "sync"
  7. "github.com/valyala/bytebufferpool"
  8. )
  9. // Writer is an interface stackless writer must conform to.
  10. //
  11. // The interface contains common subset for Writers from compress/* packages.
  12. type Writer interface {
  13. Write(p []byte) (int, error)
  14. Flush() error
  15. Close() error
  16. Reset(w io.Writer)
  17. }
  18. // NewWriterFunc must return new writer that will be wrapped into
  19. // stackless writer.
  20. type NewWriterFunc func(w io.Writer) Writer
  21. // NewWriter creates a stackless writer around a writer returned
  22. // from newWriter.
  23. //
  24. // The returned writer writes data to dstW.
  25. //
  26. // Writers that use a lot of stack space may be wrapped into stackless writer,
  27. // thus saving stack space for high number of concurrently running goroutines.
  28. func NewWriter(dstW io.Writer, newWriter NewWriterFunc) Writer {
  29. w := &writer{
  30. dstW: dstW,
  31. }
  32. w.zw = newWriter(&w.xw)
  33. return w
  34. }
  35. type writer struct {
  36. dstW io.Writer
  37. zw Writer
  38. xw xWriter
  39. err error
  40. n int
  41. p []byte
  42. op op
  43. }
  44. type op int
  45. const (
  46. opWrite op = iota
  47. opFlush
  48. opClose
  49. opReset
  50. )
  51. func (w *writer) Write(p []byte) (int, error) {
  52. w.p = p
  53. err := w.do(opWrite)
  54. w.p = nil
  55. return w.n, err
  56. }
  57. func (w *writer) Flush() error {
  58. return w.do(opFlush)
  59. }
  60. func (w *writer) Close() error {
  61. return w.do(opClose)
  62. }
  63. func (w *writer) Reset(dstW io.Writer) {
  64. w.xw.Reset()
  65. w.do(opReset) //nolint:errcheck
  66. w.dstW = dstW
  67. }
  68. func (w *writer) do(op op) error {
  69. w.op = op
  70. if !stacklessWriterFunc(w) {
  71. return errHighLoad
  72. }
  73. err := w.err
  74. if err != nil {
  75. return err
  76. }
  77. if w.xw.bb != nil && len(w.xw.bb.B) > 0 {
  78. _, err = w.dstW.Write(w.xw.bb.B)
  79. }
  80. w.xw.Reset()
  81. return err
  82. }
  83. var errHighLoad = errors.New("cannot compress data due to high load")
  84. var (
  85. stacklessWriterFuncOnce sync.Once
  86. stacklessWriterFuncFunc func(ctx any) bool
  87. )
  88. func stacklessWriterFunc(ctx any) bool {
  89. stacklessWriterFuncOnce.Do(func() {
  90. stacklessWriterFuncFunc = NewFunc(writerFunc)
  91. })
  92. return stacklessWriterFuncFunc(ctx)
  93. }
  94. func writerFunc(ctx any) {
  95. w := ctx.(*writer)
  96. switch w.op {
  97. case opWrite:
  98. w.n, w.err = w.zw.Write(w.p)
  99. case opFlush:
  100. w.err = w.zw.Flush()
  101. case opClose:
  102. w.err = w.zw.Close()
  103. case opReset:
  104. w.zw.Reset(&w.xw)
  105. w.err = nil
  106. default:
  107. panic(fmt.Sprintf("BUG: unexpected op: %d", w.op))
  108. }
  109. }
  110. type xWriter struct {
  111. bb *bytebufferpool.ByteBuffer
  112. }
  113. func (w *xWriter) Write(p []byte) (int, error) {
  114. if w.bb == nil {
  115. w.bb = bufferPool.Get()
  116. }
  117. return w.bb.Write(p)
  118. }
  119. func (w *xWriter) Reset() {
  120. if w.bb != nil {
  121. bufferPool.Put(w.bb)
  122. w.bb = nil
  123. }
  124. }
  125. var bufferPool bytebufferpool.Pool