优化代码

This commit is contained in:
GoEdgeLab
2023-05-29 20:39:08 +08:00
parent c2f9babe1c
commit 5ee07fcd10

View File

@@ -40,7 +40,7 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) {
var addStatusHeader = this.web.Cache.AddStatusHeader var addStatusHeader = this.web.Cache.AddStatusHeader
if addStatusHeader { if addStatusHeader {
defer func() { defer func() {
cacheStatus := this.varMapping["cache.status"] var cacheStatus = this.varMapping["cache.status"]
if cacheStatus != "HIT" { if cacheStatus != "HIT" {
this.writer.Header().Set("X-Cache", cacheStatus) this.writer.Header().Set("X-Cache", cacheStatus)
} }
@@ -48,7 +48,7 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) {
} }
// 检查服务独立的缓存条件 // 检查服务独立的缓存条件
refType := "" var refType = ""
for _, cacheRef := range this.web.Cache.CacheRefs { for _, cacheRef := range this.web.Cache.CacheRefs {
if !cacheRef.IsOn { if !cacheRef.IsOn {
continue continue
@@ -131,7 +131,7 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) {
this.varMapping["cache.key"] = key this.varMapping["cache.key"] = key
// 读取缓存 // 读取缓存
storage := caches.SharedManager.FindStorageWithPolicy(cachePolicy.Id) var storage = caches.SharedManager.FindStorageWithPolicy(cachePolicy.Id)
if storage == nil { if storage == nil {
this.cacheRef = nil this.cacheRef = nil
return return
@@ -301,13 +301,13 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) {
this.writer.SetSentHeaderBytes(reader.HeaderSize()) this.writer.SetSentHeaderBytes(reader.HeaderSize())
var headerPool = this.bytePool(reader.HeaderSize()) var headerPool = this.bytePool(reader.HeaderSize())
var headerBuf = headerPool.Get() var headerBuf = headerPool.Get()
err = reader.ReadHeader(headerBuf, func(n int) (goNext bool, err error) { err = reader.ReadHeader(headerBuf, func(n int) (goNext bool, readErr error) {
headerData = append(headerData, headerBuf[:n]...) headerData = append(headerData, headerBuf[:n]...)
for { for {
nIndex := bytes.Index(headerData, []byte{'\n'}) var nIndex = bytes.Index(headerData, []byte{'\n'})
if nIndex >= 0 { if nIndex >= 0 {
row := headerData[:nIndex] var row = headerData[:nIndex]
spaceIndex := bytes.Index(row, []byte{':'}) var spaceIndex = bytes.Index(row, []byte{':'})
if spaceIndex <= 0 { if spaceIndex <= 0 {
return false, errors.New("invalid header '" + string(row) + "'") return false, errors.New("invalid header '" + string(row) + "'")
} }
@@ -460,9 +460,9 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) {
var pool = this.bytePool(fileSize) var pool = this.bytePool(fileSize)
var bodyBuf = pool.Get() var bodyBuf = pool.Get()
err = reader.ReadBodyRange(bodyBuf, ranges[0].Start(), ranges[0].End(), func(n int) (goNext bool, err error) { err = reader.ReadBodyRange(bodyBuf, ranges[0].Start(), ranges[0].End(), func(n int) (goNext bool, readErr error) {
_, err = this.writer.Write(bodyBuf[:n]) _, readErr = this.writer.Write(bodyBuf[:n])
if err != nil { if readErr != nil {
return false, errWritingToClient return false, errWritingToClient
} }
return true, nil return true, nil
@@ -485,7 +485,7 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) {
var boundary = httpRequestGenBoundary() var boundary = httpRequestGenBoundary()
respHeader.Set("Content-Type", "multipart/byteranges; boundary="+boundary) respHeader.Set("Content-Type", "multipart/byteranges; boundary="+boundary)
respHeader.Del("Content-Length") respHeader.Del("Content-Length")
contentType := respHeader.Get("Content-Type") var contentType = respHeader.Get("Content-Type")
this.writer.WriteHeader(http.StatusPartialContent) this.writer.WriteHeader(http.StatusPartialContent)
@@ -516,9 +516,9 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) {
var pool = this.bytePool(fileSize) var pool = this.bytePool(fileSize)
var bodyBuf = pool.Get() var bodyBuf = pool.Get()
err := reader.ReadBodyRange(bodyBuf, r.Start(), r.End(), func(n int) (goNext bool, err error) { err = reader.ReadBodyRange(bodyBuf, r.Start(), r.End(), func(n int) (goNext bool, readErr error) {
_, err = this.writer.Write(bodyBuf[:n]) _, readErr = this.writer.Write(bodyBuf[:n])
if err != nil { if readErr != nil {
return false, errWritingToClient return false, errWritingToClient
} }
return true, nil return true, nil