writer.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. err error
  39. xw xWriter
  40. p []byte
  41. n int
  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) WriteString(s string) (int, error) {
  58. w.p = s2b(s)
  59. err := w.do(opWrite)
  60. w.p = nil
  61. return w.n, err
  62. }
  63. func (w *writer) Flush() error {
  64. return w.do(opFlush)
  65. }
  66. func (w *writer) Close() error {
  67. return w.do(opClose)
  68. }
  69. func (w *writer) Reset(dstW io.Writer) {
  70. w.xw.Reset()
  71. w.do(opReset) //nolint:errcheck
  72. w.dstW = dstW
  73. }
  74. func (w *writer) do(op op) error {
  75. w.op = op
  76. if !stacklessWriterFunc(w) {
  77. return errHighLoad
  78. }
  79. err := w.err
  80. if err != nil {
  81. return err
  82. }
  83. if w.xw.bb != nil && len(w.xw.bb.B) > 0 {
  84. _, err = w.dstW.Write(w.xw.bb.B)
  85. }
  86. w.xw.Reset()
  87. return err
  88. }
  89. var errHighLoad = errors.New("cannot compress data due to high load")
  90. var (
  91. stacklessWriterFuncOnce sync.Once
  92. stacklessWriterFuncFunc func(ctx any) bool
  93. )
  94. func stacklessWriterFunc(ctx any) bool {
  95. stacklessWriterFuncOnce.Do(func() {
  96. stacklessWriterFuncFunc = NewFunc(writerFunc)
  97. })
  98. return stacklessWriterFuncFunc(ctx)
  99. }
  100. func writerFunc(ctx any) {
  101. w := ctx.(*writer)
  102. switch w.op {
  103. case opWrite:
  104. w.n, w.err = w.zw.Write(w.p)
  105. case opFlush:
  106. w.err = w.zw.Flush()
  107. case opClose:
  108. w.err = w.zw.Close()
  109. case opReset:
  110. w.zw.Reset(&w.xw)
  111. w.err = nil
  112. default:
  113. panic(fmt.Sprintf("BUG: unexpected op: %d", w.op))
  114. }
  115. }
  116. type xWriter struct {
  117. bb *bytebufferpool.ByteBuffer
  118. }
  119. func (w *xWriter) Write(p []byte) (int, error) {
  120. if w.bb == nil {
  121. w.bb = bufferPool.Get()
  122. }
  123. return w.bb.Write(p)
  124. }
  125. func (w *xWriter) Reset() {
  126. if w.bb != nil {
  127. bufferPool.Put(w.bb)
  128. w.bb = nil
  129. }
  130. }
  131. var bufferPool bytebufferpool.Pool