Skip to content

Commit

Permalink
Package Socket/Delim:
Browse files Browse the repository at this point in the history
- rework to optimize memory & variable use
- remove capabilities of update the instance when running, prefert recreate new one if necessary
  • Loading branch information
nabbar committed Jan 9, 2025
1 parent d8d4467 commit 3e65b05
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 107 deletions.
32 changes: 14 additions & 18 deletions socket/delim/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
package delim

import (
"bufio"
"io"
"sync/atomic"

libsiz "github.com/nabbar/golib/size"
)
Expand All @@ -37,29 +37,25 @@ type BufferDelim interface {
io.ReadCloser
io.WriterTo

SetDelim(d rune)
GetDelim() rune

SetBufferSize(b libsiz.Size)
GetBufferSize() libsiz.Size

SetInput(i io.ReadCloser)
Delim() rune
Reader() io.ReadCloser
Copy(w io.Writer) (n int64, err error)
ReadBytes() ([]byte, error)
UnRead() ([]byte, error)
}

func New(r io.ReadCloser, delim rune, sizeBufferRead libsiz.Size) BufferDelim {
d := &dlm{
i: new(atomic.Value),
d: new(atomic.Int32),
s: new(atomic.Uint64),
r: new(atomic.Value),
}
var b *bufio.Reader

d.SetDelim(delim)
d.SetBufferSize(sizeBufferRead)
d.SetInput(r)
if sizeBufferRead > 0 {
b = bufio.NewReaderSize(r, sizeBufferRead.Int())
} else {
b = bufio.NewReader(r)
}

return d
return &dlm{
i: r,
r: b,
d: delim,
}
}
61 changes: 42 additions & 19 deletions socket/delim/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,67 @@ func (o *dlm) Copy(w io.Writer) (n int64, err error) {
}

func (o *dlm) Read(p []byte) (n int, err error) {
if r := o.getReader(); r == nil {
if o == nil || o.r == nil {
return 0, ErrInstance
} else {
b, e := r.ReadBytes(o.getDelimByte())

if len(b) > 0 {
if cap(p) < len(b) {
p = append(p, make([]byte, len(b)-len(p))...)
}
copy(p, b)
}

b, e := o.r.ReadBytes(byte(o.d))

if len(b) > 0 {
if cap(p) < len(b) {
p = append(p, make([]byte, len(b)-len(p))...)
}
copy(p, b)
}

return len(b), e
}

func (o *dlm) UnRead() ([]byte, error) {
if o == nil || o.r == nil {
return nil, ErrInstance
}

return len(b), e
if s := o.r.Buffered(); s > 0 {
b := make([]byte, s)
_, e := o.r.Read(b)
return b, e
}

return nil, nil
}

func (o *dlm) ReadBytes() ([]byte, error) {
if r := o.getReader(); r == nil {
return make([]byte, 0), ErrInstance
} else {
return r.ReadBytes(o.getDelimByte())
if o.r == nil {
return nil, ErrInstance
}

return o.r.ReadBytes(byte(o.d))
}

func (o *dlm) Close() error {
return o.getInput().Close()
o.r.Reset(nil)
o.r = nil

return o.i.Close()
}

func (o *dlm) WriteTo(w io.Writer) (n int64, err error) {
var (
i int
s = 1
e error
i int
b []byte

s = 1
d = o.getDelimByte()
)

for err == nil && s > 0 {
b, err = o.ReadBytes()
if o.r == nil {
return 0, ErrInstance
}

for err == nil {
b, err = o.r.ReadBytes(d)
s = len(b)

if s > 0 {
Expand Down
76 changes: 6 additions & 70 deletions socket/delim/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,82 +29,18 @@ package delim
import (
"bufio"
"io"
"sync/atomic"

libsiz "github.com/nabbar/golib/size"
)

type dlm struct {
i *atomic.Value // input io.ReadCloser
d *atomic.Int32 // delimiter rune
s *atomic.Uint64 // buffer libsiz.Size
r *atomic.Value // *bufio.Reader
}

func (o *dlm) SetDelim(delim rune) {
o.d.Store(delim)
i io.ReadCloser // input io.ReadCloser
r *bufio.Reader // *bufio.Reader
d rune // delimiter rune
}

func (o *dlm) GetDelim() rune {
return o.d.Load()
func (o *dlm) Delim() rune {
return o.d
}

func (o *dlm) getDelimByte() byte {
return byte(o.GetDelim())
}

func (o *dlm) SetBufferSize(b libsiz.Size) {
o.s.Store(b.Uint64())
}

func (o *dlm) GetBufferSize() libsiz.Size {
return libsiz.Size(o.s.Load())
}

func (o *dlm) SetInput(i io.ReadCloser) {
if i == nil {
i = &DiscardCloser{}
}

o.i.Store(i)
}

func (o *dlm) getInput() io.ReadCloser {
if i := o.i.Load(); i == nil {
return &DiscardCloser{}
} else if v, k := i.(io.ReadCloser); !k {
return &DiscardCloser{}
} else {
return v
}
}

func (o *dlm) newReader() *bufio.Reader {
if siz := o.GetBufferSize(); siz > 0 {
return bufio.NewReaderSize(o.getInput(), siz.Int())
} else {
return bufio.NewReader(o.getInput())
}
}

func (o *dlm) setReader(r *bufio.Reader) {
if r == nil {
r = o.newReader()
}

o.r.Store(r)
}

func (o *dlm) getReader() *bufio.Reader {
if i := o.r.Load(); i == nil {
r := o.newReader()
o.setReader(r)
return r
} else if v, k := i.(*bufio.Reader); !k {
r := o.newReader()
o.setReader(r)
return r
} else {
return v
}
return byte(o.d)
}

0 comments on commit 3e65b05

Please sign in to comment.