mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-10-20 05:56:56 +00:00
perf(sse): skip line splitting for single-line data events
This commit is contained in:
@@ -315,13 +315,21 @@ func splitSSELines(b []byte) [][]byte {
|
|||||||
// "\r" of any kind ever reaches the wire from here, regardless of what
|
// "\r" of any kind ever reaches the wire from here, regardless of what
|
||||||
// readSSE upstream of this call did or did not already split it on.
|
// readSSE upstream of this call did or did not already split it on.
|
||||||
func (s *sseWriter) writeData(b []byte) error {
|
func (s *sseWriter) writeData(b []byte) error {
|
||||||
|
var buf []byte
|
||||||
|
if !bytes.ContainsAny(b, "\r\n") {
|
||||||
|
buf = make([]byte, 0, len(sseDataPrefix)+len(b)+2)
|
||||||
|
buf = append(buf, sseDataPrefix...)
|
||||||
|
buf = append(buf, b...)
|
||||||
|
buf = append(buf, '\n')
|
||||||
|
} else {
|
||||||
lines := splitSSELines(b)
|
lines := splitSSELines(b)
|
||||||
buf := make([]byte, 0, len(b)+len(lines)*len(sseDataPrefix)+1)
|
buf = make([]byte, 0, len(b)+len(lines)*len(sseDataPrefix)+1)
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
buf = append(buf, sseDataPrefix...)
|
buf = append(buf, sseDataPrefix...)
|
||||||
buf = append(buf, line...)
|
buf = append(buf, line...)
|
||||||
buf = append(buf, '\n')
|
buf = append(buf, '\n')
|
||||||
}
|
}
|
||||||
|
}
|
||||||
buf = append(buf, '\n')
|
buf = append(buf, '\n')
|
||||||
|
|
||||||
if _, err := s.w.Write(buf); err != nil {
|
if _, err := s.w.Write(buf); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user