Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
Chatgpt Forwarder
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
Chatgpt Forwarder
Commits
54cbd172
Commit
54cbd172
authored
Feb 18, 2023
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add init account queue per proxy
parent
8af6f27f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
63 additions
and
16 deletions
+63
-16
src/account-pool/account-pool.service.ts
src/account-pool/account-pool.service.ts
+63
-16
No files found.
src/account-pool/account-pool.service.ts
View file @
54cbd172
...
...
@@ -12,6 +12,13 @@ import { AccountProviderService } from '../account-provider/account-provider.ser
import
{
AccountPoolStatusDto
}
from
'
./account-pool-status.dto
'
;
import
{
Interval
}
from
'
@nestjs/schedule
'
;
import
BetterLock
from
'
better-lock
'
;
import
{
resolve
}
from
'
path
'
;
interface
AddAccountPayload
extends
OpenAIAccount
{
resolve
:
(
success
:
boolean
)
=>
void
;
reject
:
(
error
:
any
)
=>
void
;
retry
:
boolean
;
}
@
Injectable
()
export
class
AccountPoolService
...
...
@@ -31,11 +38,14 @@ export class AccountPoolService
private
async
initAccount
(
account
:
OpenAIAccount
,
retry
=
true
,
proxy
?:
string
,
):
Promise
<
boolean
>
{
if
(
this
.
accounts
.
has
(
account
.
email
))
{
return
false
;
}
const
proxy
=
await
this
.
proxyPoolService
.
getProxy
();
if
(
!
proxy
)
{
proxy
=
await
this
.
proxyPoolService
.
getProxy
();
}
const
state
=
new
AccountState
(
account
,
new
this
.
ChatGPTAPIBrowserConstructor
({
...
...
@@ -44,7 +54,7 @@ export class AccountPoolService
isProAccount
:
!!
account
.
pro
,
isGoogleLogin
:
account
.
loginType
===
'
google
'
,
isMicrosoftLogin
:
account
.
loginType
===
'
microsoft
'
,
proxyServer
:
proxy
,
proxyServer
:
proxy
||
undefined
,
}),
);
this
.
log
(
`Adding account
${
account
.
email
}
`
);
...
...
@@ -54,17 +64,52 @@ export class AccountPoolService
this
.
log
(
`Added account
${
account
.
email
}
`
);
return
true
;
}
else
if
(
retry
)
{
await
Promise
.
all
([
new
Promise
((
r
)
=>
setTimeout
(
r
,
5000
)),
state
.
close
(),
]);
return
await
this
.
initAccount
(
account
,
retry
);
await
state
.
close
();
return
this
.
initAccountQueued
(
account
,
retry
);
}
else
{
await
state
.
close
();
return
false
;
}
}
private
initAccountQueue
=
new
Map
<
string
,
AddAccountPayload
[]
>
();
private
async
initAccountQueued
(
account
:
OpenAIAccount
,
retry
=
true
)
{
if
(
this
.
accounts
.
has
(
account
.
email
))
{
return
false
;
}
const
proxy
=
(
await
this
.
proxyPoolService
.
getProxy
())
||
''
;
if
(
!
this
.
initAccountQueue
.
has
(
proxy
))
{
this
.
initAccountQueue
.
set
(
proxy
,
[]);
}
const
queue
=
this
.
initAccountQueue
.
get
(
proxy
);
return
new
Promise
<
boolean
>
((
resolve
,
reject
)
=>
queue
.
push
({
...
account
,
retry
,
resolve
,
reject
,
}),
);
}
@
Interval
(
1000
*
10
)
private
async
initAccountQueueResolve
()
{
const
entries
=
[...
this
.
initAccountQueue
.
entries
()];
await
Promise
.
all
(
entries
.
map
(
async
([
proxy
,
queue
])
=>
{
const
item
=
queue
.
shift
();
if
(
!
item
)
{
return
;
}
try
{
item
.
resolve
(
await
this
.
initAccount
(
item
,
item
.
retry
,
proxy
));
}
catch
(
e
)
{
item
.
reject
(
e
);
}
}),
);
}
private
accountInfos
:
OpenAIAccount
[];
@
Interval
(
1000
*
60
*
5
)
...
...
@@ -91,7 +136,17 @@ export class AccountPoolService
await
eval
(
"
import('chatgpt3')
"
)
).
ChatGPTAPIBrowser
;
this
.
accountInfos
=
await
this
.
accountProvider
.
get
();
this
.
accountInfos
.
forEach
((
a
)
=>
this
.
addAccount
(
a
,
true
));
this
.
accountInfos
.
forEach
((
a
,
i
)
=>
this
.
addAccount
(
a
,
true
,
i
===
0
));
}
private
addAccountLock
=
new
BetterLock
();
async
addAccount
(
account
:
OpenAIAccount
,
retry
=
false
,
noQueue
=
false
)
{
return
this
.
addAccountLock
.
acquire
(
account
.
email
,
()
=>
noQueue
?
this
.
initAccount
(
account
,
retry
)
:
this
.
initAccountQueued
(
account
,
retry
),
);
}
async
onModuleDestroy
()
{
...
...
@@ -134,14 +189,6 @@ export class AccountPoolService
return
this
.
accounts
.
get
(
email
)
||
this
.
randomAccount
(
exclude
);
}
private
addAccountLock
=
new
BetterLock
();
async
addAccount
(
account
:
OpenAIAccount
,
retry
=
false
)
{
return
this
.
addAccountLock
.
acquire
(
account
.
email
,
()
=>
this
.
initAccount
(
account
,
retry
),
);
}
async
removeAccount
(
email
:
string
)
{
const
state
=
this
.
accounts
.
get
(
email
);
if
(
!
state
)
{
...
...
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