Skip to content

Commit

Permalink
feat: make ResetableWriter public
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Evdokimov committed Jan 16, 2025
1 parent 08702fc commit a5374f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 5 additions & 5 deletions internal/topic/topicwriterinternal/encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
codecUnknown = rawtopiccommon.CodecUNSPECIFIED
)

type resetableWriter interface {
type PublicResetableWriter interface {
io.WriteCloser
Reset(wr io.Writer)
}
Expand All @@ -26,13 +26,13 @@ type encoderPool struct {
pool sync.Pool
}

func (p *encoderPool) Get() resetableWriter {
enc, _ := p.pool.Get().(resetableWriter)
func (p *encoderPool) Get() PublicResetableWriter {
enc, _ := p.pool.Get().(PublicResetableWriter)

return enc
}

func (p *encoderPool) Put(wr resetableWriter) {
func (p *encoderPool) Put(wr PublicResetableWriter) {
p.pool.Put(wr)
}

Expand Down Expand Up @@ -79,7 +79,7 @@ func (e *EncoderMap) Encode(codec rawtopiccommon.Codec, target io.Writer, data [
return 0, err
}

resetableEnc, ok := enc.(resetableWriter)
resetableEnc, ok := enc.(PublicResetableWriter)
if ok {
if _, ok := e.p[codec]; !ok {
e.p[codec] = newEncoderPool()
Expand Down
6 changes: 6 additions & 0 deletions topic/topicoptions/topicoptions_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ type WriteSessionMetadata map[string]string
// CreateEncoderFunc for create message decoders
type CreateEncoderFunc = topicwriterinternal.PublicCreateEncoderFunc

// ResettableWriter is able to reset a nested writer between uses.
type ResetableWriter = topicwriterinternal.PublicResetableWriter

// WithWriterAddEncoder add custom codec implementation to writer.
// It allows to set custom codecs implementations for custom and internal codecs.
//
// If CreateEncoderFunc returns a writer implementing ResetableWriter, then the compression objects
// will be reused for this codec. This will reduce the load on the GC.
func WithWriterAddEncoder(codec topictypes.Codec, f CreateEncoderFunc) WriterOption {
return topicwriterinternal.WithAddEncoder(rawtopiccommon.Codec(codec), f)
}
Expand Down

0 comments on commit a5374f0

Please sign in to comment.