mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-11 22:00:25 +08:00
实现基础的206 partial content缓存
This commit is contained in:
52
internal/utils/readers/reader_tee.go
Normal file
52
internal/utils/readers/reader_tee.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package readers
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
type TeeReader struct {
|
||||
r io.Reader
|
||||
w io.Writer
|
||||
|
||||
onFail func(err error)
|
||||
onEOF func()
|
||||
}
|
||||
|
||||
func NewTeeReader(reader io.Reader, writer io.Writer) *TeeReader {
|
||||
return &TeeReader{
|
||||
r: reader,
|
||||
w: writer,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *TeeReader) Read(p []byte) (n int, err error) {
|
||||
n, err = this.r.Read(p)
|
||||
if n > 0 {
|
||||
_, wErr := this.w.Write(p[:n])
|
||||
if err == nil && wErr != nil {
|
||||
err = wErr
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
if this.onEOF != nil {
|
||||
this.onEOF()
|
||||
}
|
||||
} else {
|
||||
if this.onFail != nil {
|
||||
this.onFail(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (this *TeeReader) OnFail(onFail func(err error)) {
|
||||
this.onFail = onFail
|
||||
}
|
||||
|
||||
func (this *TeeReader) OnEOF(onEOF func()) {
|
||||
this.onEOF = onEOF
|
||||
}
|
||||
Reference in New Issue
Block a user