修复内存泄漏问题

addon-dailer
lqqyt2423 4 years ago
parent aa0d8219f8
commit c41bf7c881

@ -39,19 +39,35 @@ var ignoreErr = func(log *_log.Entry, err error) bool {
func transfer(log *_log.Entry, a, b io.ReadWriter) { func transfer(log *_log.Entry, a, b io.ReadWriter) {
done := make(chan struct{}) done := make(chan struct{})
go func() { defer close(done)
_, err := io.Copy(a, b)
if err != nil && !ignoreErr(log, err) { forward := func(dst io.Writer, src io.Reader, ec chan<- error) {
log.Error(err) _, err := io.Copy(dst, src)
if v, ok := dst.(*conn); ok {
// 避免内存泄漏的关键
_ = v.Writer.CloseWithError(nil)
} }
close(done)
}()
_, err := io.Copy(b, a) select {
if err != nil && !ignoreErr(log, err) { case <-done:
return
case ec <- err:
}
}
errChan := make(chan error)
go forward(a, b, errChan)
go forward(b, a, errChan)
for i := 0; i < 2; i++ {
if err := <-errChan; err != nil {
if !ignoreErr(log, err) {
log.Error(err) log.Error(err)
} }
<-done return // 如果有错误,直接返回
}
}
} }
type Options struct { type Options struct {

Loading…
Cancel
Save