From d767ca177a4d0abb5265464114fe1d44735ab87e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sun, 30 Jul 2023 09:00:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E5=86=99=E5=85=A5=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=97=B6=E8=87=AA=E5=8A=A8=E5=88=86=E5=A4=9A?= =?UTF-8?q?=E6=AC=A1=E5=86=99=E5=85=A5=E2=80=9C=E5=A4=A7=E2=80=9D=E7=9A=84?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/writer_file.go | 55 +++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/internal/caches/writer_file.go b/internal/caches/writer_file.go index 514deee..4949eff 100644 --- a/internal/caches/writer_file.go +++ b/internal/caches/writer_file.go @@ -75,23 +75,30 @@ func (this *FileWriter) WriteHeaderLength(headerLength int) error { // Write 写入数据 func (this *FileWriter) Write(data []byte) (n int, err error) { - fsutils.WriteBegin() - n, err = this.rawWriter.Write(data) - fsutils.WriteEnd() - this.bodySize += int64(n) - - if this.maxSize > 0 && this.bodySize > this.maxSize { - err = ErrEntityTooLarge - - if this.storage != nil { - this.storage.IgnoreKey(this.key, this.maxSize) + // split LARGE data + var l = len(data) + if l > (2 << 20) { + var offset = 0 + const bufferSize = 256 << 10 + for { + var end = offset + bufferSize + if end > l { + end = l + } + n1, err1 := this.write(data[offset:end]) + n += n1 + if err1 != nil { + return n, err1 + } + if end >= l { + return n, nil + } + offset = end } } - if err != nil { - _ = this.Discard() - } - return + // write NORMAL size data + return this.write(data) } // WriteAt 在指定位置写入数据 @@ -187,3 +194,23 @@ func (this *FileWriter) Key() string { func (this *FileWriter) ItemType() ItemType { return ItemTypeFile } + +func (this *FileWriter) write(data []byte) (n int, err error) { + fsutils.WriteBegin() + n, err = this.rawWriter.Write(data) + fsutils.WriteEnd() + this.bodySize += int64(n) + + if this.maxSize > 0 && this.bodySize > this.maxSize { + err = ErrEntityTooLarge + + if this.storage != nil { + this.storage.IgnoreKey(this.key, this.maxSize) + } + } + + if err != nil { + _ = this.Discard() + } + return +}