Skip to content

Commit

Permalink
fix: fix buf reader
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean committed Nov 29, 2024
1 parent 649294d commit 9bb033a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
24 changes: 18 additions & 6 deletions protocol/thrift/apache/adaptor/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func AdaptRead(p, iprot interface{}) error {
return fmt.Errorf("no codec implementation available")
}

var rd io.Reader
var br bufiox.Reader
// if iprot is from kitex v0.12.0+, use interface assert to get bufiox reader
if bp, ok := iprot.(bufioxReaderWriter); ok {
Expand All @@ -58,29 +59,40 @@ func AdaptRead(p, iprot interface{}) error {
case byteBuffer:
// if reader is from byteBuffer, Read() function is not always available
// so use an adaptor to implement Read() by Next() and ReadableLen()
br = bufiox.NewDefaultReader(byteBuffer2ReadWriter(r))
rd = byteBuffer2ReadWriter(r)
case io.ReadWriter:
// if reader is not byteBuffer but is io.ReadWriter, it suppose to be apache thrift binary protocol
br = bufiox.NewDefaultReader(r)
// if reader is not byteBuffer but is io.ReadWriter, it supposes to be apache thrift binary protocol
rd = r
default:
return fmt.Errorf("reader not ok")
}
break
}
}
}
if br == nil {
if rd == nil && br == nil {
return fmt.Errorf("no available field for reader")
}

// read data from iprot
buf, err := thrift.NewSkipDecoder(br).Next(thrift.STRUCT)
var sd *thrift.SkipDecoder
if br != nil {
sd = thrift.NewSkipDecoder(br)
} else {
// if there's no bufiox.Reader, do not wrap a new bufiox.Reader, or some data will remain in the buffer
// directly read from io.Reader
sd = thrift.NewSkipDecoderWithIOReader(rd)

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / unit-benchmark-test (1.21, X64)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / unit-benchmark-test (1.22, X64)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / unit-benchmark-test (1.19, X64)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / unit-benchmark-test (1.23, X64)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / unit-benchmark-test (1.20, X64)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / unittest (1.22)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / unittest (1.19)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / unittest (1.20)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / unittest (1.18)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / benchmark (1.21)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / benchmark (1.18)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / lint

undefined: thrift.NewSkipDecoderWithIOReader (typecheck)

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / unit-benchmark-test (1.18, X64)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / unittest (1.23)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / unittest (1.21)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / benchmark (1.19)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / benchmark (1.23)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / benchmark (1.20)

undefined: thrift.NewSkipDecoderWithIOReader

Check failure on line 83 in protocol/thrift/apache/adaptor/adaptor.go

View workflow job for this annotation

GitHub Actions / benchmark (1.22)

undefined: thrift.NewSkipDecoderWithIOReader
}

buf, err := sd.Next(thrift.STRUCT)
if err != nil {
return err
}

sd.Release()

// unmarshal the data into struct
_, err = fastStruct.FastRead(buf)

return err
}

Expand Down
1 change: 1 addition & 0 deletions protocol/thrift/skipdecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func (p *SkipDecoder) next(n int) (buf []byte, err error) {
if buf, err = p.r.Next(n); err != nil {
return
}

if cap(p.nextBuf)-len(p.nextBuf) < n {
var ncap int
for ncap = cap(p.nextBuf) * 2; ncap-len(p.nextBuf) < n; ncap *= 2 {
Expand Down

0 comments on commit 9bb033a

Please sign in to comment.