-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reduce the number of stream memory copies (#5402)
* fix: generalize the openai data prefix * feat: reduce the number of stream memory copies * fix: lint * feat: reduce the number of stream memory copies * fix: lint * fix: reset buf when usage * chore: mv event to render * fix: ignore samgrep error
- Loading branch information
1 parent
a855c20
commit 40ec466
Showing
10 changed files
with
179 additions
and
139 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package render | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/labring/sealos/service/aiproxy/common/conv" | ||
) | ||
|
||
var ( | ||
contentType = []string{"text/event-stream"} | ||
noCache = []string{"no-cache"} | ||
) | ||
|
||
type OpenAISSE struct { | ||
Data string | ||
} | ||
|
||
const ( | ||
nn = "\n\n" | ||
data = "data: " | ||
) | ||
|
||
var ( | ||
nnBytes = conv.StringToBytes(nn) | ||
dataBytes = conv.StringToBytes(data) | ||
) | ||
|
||
func (r OpenAISSE) Render(w http.ResponseWriter) error { | ||
r.WriteContentType(w) | ||
|
||
for _, bytes := range [][]byte{ | ||
dataBytes, | ||
conv.StringToBytes(r.Data), | ||
nnBytes, | ||
} { | ||
// nosemgrep: go.lang.security.audit.xss.no-direct-write-to-responsewriter.no-direct-write-to-responsewriter | ||
if _, err := w.Write(bytes); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (r OpenAISSE) WriteContentType(w http.ResponseWriter) { | ||
header := w.Header() | ||
header["Content-Type"] = contentType | ||
|
||
if _, exist := header["Cache-Control"]; !exist { | ||
header["Cache-Control"] = noCache | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.