mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-11 13:53:34 +08:00
缓存文件实现Sendfile
This commit is contained in:
@@ -819,6 +819,14 @@ func (this *FileStorage) IgnoreKey(key string) {
|
|||||||
this.ignoreKeys.Push(key)
|
this.ignoreKeys.Push(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CanSendfile 是否支持Sendfile
|
||||||
|
func (this *FileStorage) CanSendfile() bool {
|
||||||
|
if this.options == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return this.options.EnableSendfile
|
||||||
|
}
|
||||||
|
|
||||||
// 绝对路径
|
// 绝对路径
|
||||||
func (this *FileStorage) dir() string {
|
func (this *FileStorage) dir() string {
|
||||||
return this.options.Dir + "/p" + strconv.FormatInt(this.policy.Id, 10) + "/"
|
return this.options.Dir + "/p" + strconv.FormatInt(this.policy.Id, 10) + "/"
|
||||||
|
|||||||
@@ -51,4 +51,7 @@ type StorageInterface interface {
|
|||||||
|
|
||||||
// IgnoreKey 忽略某个Key,即不缓存某个Key
|
// IgnoreKey 忽略某个Key,即不缓存某个Key
|
||||||
IgnoreKey(key string)
|
IgnoreKey(key string)
|
||||||
|
|
||||||
|
// CanSendfile 是否支持Sendfile
|
||||||
|
CanSendfile() bool
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -347,6 +347,11 @@ func (this *MemoryStorage) IgnoreKey(key string) {
|
|||||||
this.ignoreKeys.Push(key)
|
this.ignoreKeys.Push(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CanSendfile 是否支持Sendfile
|
||||||
|
func (this *MemoryStorage) CanSendfile() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// 计算Key Hash
|
// 计算Key Hash
|
||||||
func (this *MemoryStorage) hash(key string) uint64 {
|
func (this *MemoryStorage) hash(key string) uint64 {
|
||||||
return xxhash.Sum64String(key)
|
return xxhash.Sum64String(key)
|
||||||
|
|||||||
@@ -540,7 +540,15 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) {
|
|||||||
this.writer.Prepare(resp, fileSize, reader.Status(), false)
|
this.writer.Prepare(resp, fileSize, reader.Status(), false)
|
||||||
this.writer.WriteHeader(reader.Status())
|
this.writer.WriteHeader(reader.Status())
|
||||||
|
|
||||||
|
if storage.CanSendfile() {
|
||||||
|
if fp, canSendFile := this.writer.canSendfile(); canSendFile {
|
||||||
|
this.writer.sentBodyBytes, err = io.CopyBuffer(this.writer.rawWriter, fp, buf)
|
||||||
|
} else {
|
||||||
_, err = io.CopyBuffer(this.writer, resp.Body, buf)
|
_, err = io.CopyBuffer(this.writer, resp.Body, buf)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
_, err = io.CopyBuffer(this.writer, resp.Body, buf)
|
||||||
|
}
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1058,8 +1058,10 @@ func (this *HTTPWriter) Close() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if this.sentBodyBytes == 0 {
|
||||||
this.sentBodyBytes = this.counterWriter.TotalBytes()
|
this.sentBodyBytes = this.counterWriter.TotalBytes()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Hijack Hijack
|
// Hijack Hijack
|
||||||
func (this *HTTPWriter) Hijack() (conn net.Conn, buf *bufio.ReadWriter, err error) {
|
func (this *HTTPWriter) Hijack() (conn net.Conn, buf *bufio.ReadWriter, err error) {
|
||||||
|
|||||||
9
internal/nodes/http_writer_ext.go
Normal file
9
internal/nodes/http_writer_ext.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||||
|
//go:build !plus
|
||||||
|
// +build !plus
|
||||||
|
|
||||||
|
package nodes
|
||||||
|
|
||||||
|
func (this *HTTPWriter) canSendfile() (*os.File, bool) {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
@@ -13,6 +13,10 @@ func NewBytesCounterWriter(rawWriter io.Writer) *BytesCounterWriter {
|
|||||||
return &BytesCounterWriter{writer: rawWriter}
|
return &BytesCounterWriter{writer: rawWriter}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *BytesCounterWriter) RawWriter() io.Writer {
|
||||||
|
return this.writer
|
||||||
|
}
|
||||||
|
|
||||||
func (this *BytesCounterWriter) Write(p []byte) (n int, err error) {
|
func (this *BytesCounterWriter) Write(p []byte) (n int, err error) {
|
||||||
n, err = this.writer.Write(p)
|
n, err = this.writer.Write(p)
|
||||||
this.count += int64(n)
|
this.count += int64(n)
|
||||||
|
|||||||
Reference in New Issue
Block a user