Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
453f3be8
authored
Mar 10, 2023
by
Archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: mongo内存泄漏
parent
65da4653
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
69 additions
and
55 deletions
+69
-55
src/components/Markdown/index.module.scss
+1
-1
src/pages/api/chat/chatGpt.ts
+33
-38
src/pages/chat/index.tsx
+3
-3
src/service/mongo.ts
+22
-12
src/types/index.d.ts
+9
-0
tsconfig.json
+1
-1
No files found.
src/components/Markdown/index.module.scss
View file @
453f3be8
...
...
@@ -8,7 +8,7 @@
animation
:
blink
0
.6s
infinite
;
}
.animation
{
:last-child::after
{
>
:last-child::after
{
display
:
inline-block
;
content
:
''
;
width
:
4px
;
...
...
src/pages/api/chat/chatGpt.ts
View file @
453f3be8
...
...
@@ -69,56 +69,51 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
stream
:
true
},
{
timeout
:
20000
,
responseType
:
'stream'
,
httpsAgent
:
openaiProxy
?.
httpsAgent
}
);
console
.
log
(
'response success'
);
let
AIResponse
=
''
;
// 解析数据
const
decoder
=
new
TextDecoder
();
new
ReadableStream
({
async
start
(
controller
)
{
// callback
async
function
onParse
(
event
:
ParsedEvent
|
ReconnectInterval
)
{
if
(
event
.
type
===
'event'
)
{
const
data
=
event
.
data
;
if
(
data
===
'[DONE]'
)
{
controller
.
close
();
res
.
write
(
'event: done\ndata: \n\n'
);
res
.
end
();
// 存入库
await
ChatWindow
.
findByIdAndUpdate
(
windowId
,
{
$push
:
{
content
:
{
obj
:
'AI'
,
value
:
AIResponse
}
},
updateTime
:
Date
.
now
()
});
return
;
}
try
{
const
json
=
JSON
.
parse
(
data
);
const
content
:
string
=
json
.
choices
[
0
].
delta
.
content
||
''
;
res
.
write
(
`event: responseData\ndata:
${
content
.
replace
(
/
\n
/g
,
'<br/>'
)}
\n\n`
);
AIResponse
+=
content
;
}
catch
(
e
)
{
// maybe parse error
controller
.
error
(
e
);
res
.
end
();
}
}
const
onParse
=
async
(
event
:
ParsedEvent
|
ReconnectInterval
)
=>
{
if
(
event
.
type
===
'event'
)
{
const
data
=
event
.
data
;
if
(
data
===
'[DONE]'
)
{
// 存入库
await
ChatWindow
.
findByIdAndUpdate
(
windowId
,
{
$push
:
{
content
:
{
obj
:
'AI'
,
value
:
AIResponse
}
},
updateTime
:
Date
.
now
()
});
res
.
write
(
'event: done\ndata: \n\n'
);
res
.
end
();
return
;
}
const
parser
=
createParser
(
onParse
);
for
await
(
const
chunk
of
chatResponse
.
data
as
any
)
{
parser
.
feed
(
decoder
.
decode
(
chunk
));
try
{
const
json
=
JSON
.
parse
(
data
);
const
content
:
string
=
json
.
choices
[
0
].
delta
.
content
||
''
;
// console.log('content:', content)
res
.
write
(
`event: responseData\ndata:
${
content
.
replace
(
/
\n
/g
,
'<br/>'
)}
\n\n`
);
AIResponse
+=
content
;
}
catch
(
e
)
{
res
.
end
();
}
}
});
};
const
parser
=
createParser
(
onParse
);
for
await
(
const
chunk
of
chatResponse
.
data
as
any
)
{
parser
.
feed
(
decoder
.
decode
(
chunk
));
}
}
catch
(
err
:
any
)
{
let
errorText
=
err
;
if
(
err
.
code
===
'ECONNRESET'
)
{
...
...
src/pages/chat/index.tsx
View file @
453f3be8
...
...
@@ -127,14 +127,14 @@ const Chat = () => {
let
timer
=
setTimeout
(()
=>
{
event
.
close
();
reject
(
'服务器超时'
);
},
30000
0
);
},
30000
);
event
.
addEventListener
(
'responseData'
,
({
data
})
=>
{
/* 重置定时器 */
clearTimeout
(
timer
);
timer
=
setTimeout
(()
=>
{
event
.
close
();
reject
(
'服务器超时'
);
},
30000
0
);
},
30000
);
const
msg
=
data
.
replace
(
/<br
\/
>/g
,
'\n'
);
setChatList
((
state
)
=>
...
...
@@ -264,7 +264,7 @@ const Chat = () => {
const
reEdit
=
useCallback
(
async
()
=>
{
if
(
chatList
[
chatList
.
length
-
1
]?.
obj
!==
'Human'
)
return
;
// 删除数据库最后一句
delLastMessage
(
windowId
);
await
delLastMessage
(
windowId
);
const
val
=
chatList
[
chatList
.
length
-
1
].
value
;
setInputVal
(
val
);
...
...
src/service/mongo.ts
View file @
453f3be8
import
mongoose
from
'mongoose'
;
import
type
{
Mongoose
}
from
'mongoose'
;
let
cachedClient
:
Mongoose
;
export
async
function
connectToDatabase
()
{
if
(
cachedClient
&&
cachedClient
.
connection
.
readyState
===
1
)
{
return
cachedClient
;
/**
* 连接 MongoDB 数据库
*/
export
async
function
connectToDatabase
():
Promise
<
void
>
{
// @ts-ignore
if
(
global
.
mongodb
)
{
return
;
}
// @ts-ignore
global
.
mongodb
=
'connecting'
;
console
.
log
(
'connect mongo'
);
try
{
// @ts-ignore
global
.
mongodb
=
await
mongoose
.
connect
(
process
.
env
.
MONGODB_URI
as
string
,
{
dbName
:
'doc_gpt'
,
maxPoolSize
:
10
,
minPoolSize
:
1
});
}
catch
(
error
)
{
console
.
error
(
'mongo connect error'
);
// @ts-ignore
global
.
mongodb
=
null
;
}
cachedClient
=
await
mongoose
.
connect
(
process
.
env
.
MONGODB_URI
as
string
,
{
dbName
:
'doc_gpt'
});
return
cachedClient
;
}
export
*
from
'./models/authCode'
;
...
...
src/types/index.d.ts
0 → 100644
View file @
453f3be8
import
type
{
Mongoose
}
from
'mongoose'
;
declare
global
{
interface
Global
{
mongodb
:
Mongoose
;
}
}
export
type
a
=
string
;
tsconfig.json
View file @
453f3be8
...
...
@@ -19,6 +19,6 @@
"@/*"
:
[
"./src/*"
]
}
},
"include"
:
[
"next-env.d.ts"
,
"**/*.ts"
,
"**/*.tsx"
],
"include"
:
[
"next-env.d.ts"
,
"**/*.ts"
,
"**/*.tsx"
,
"**/*.d.ts"
],
"exclude"
:
[
"node_modules"
]
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment