优化stream
This commit is contained in:
@@ -413,7 +413,7 @@ func HandleProy(c *gin.Context) {
|
|||||||
if resp.StatusCode == 200 && localuser {
|
if resp.StatusCode == 200 && localuser {
|
||||||
|
|
||||||
if isStream {
|
if isStream {
|
||||||
contentCh := fetchResponseContent(writer, reader)
|
contentCh := fetchResponseContent(c, reader)
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
for content := range contentCh {
|
for content := range contentCh {
|
||||||
buffer.WriteString(content)
|
buffer.WriteString(content)
|
||||||
@@ -536,15 +536,18 @@ func HandleUsage(c *gin.Context) {
|
|||||||
c.JSON(200, usage)
|
c.JSON(200, usage)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchResponseContent(w *bufio.Writer, responseBody *bufio.Reader) <-chan string {
|
func fetchResponseContent(ctx *gin.Context, responseBody *bufio.Reader) <-chan string {
|
||||||
contentCh := make(chan string)
|
contentCh := make(chan string)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(contentCh)
|
defer close(contentCh)
|
||||||
for {
|
for {
|
||||||
line, err := responseBody.ReadString('\n')
|
line, err := responseBody.ReadString('\n')
|
||||||
if err == nil {
|
if err == nil {
|
||||||
w.WriteString(line)
|
lines := strings.Split(line, "")
|
||||||
w.Flush()
|
for _, word := range lines {
|
||||||
|
ctx.Writer.WriteString(word)
|
||||||
|
ctx.Writer.Flush()
|
||||||
|
}
|
||||||
if line == "\n" {
|
if line == "\n" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user