diff options
author | 2022-04-28 13:23:11 +0100 | |
---|---|---|
committer | 2022-04-28 13:23:11 +0100 | |
commit | 420e2fb22bc7aa4967ddadb11e444079efdf5117 (patch) | |
tree | 413842c5df646c30a8079671ade5e677e3825fb8 /internal/processing/account | |
parent | [bugfix] Fix possible race condition in federatingdb (#490) (diff) | |
download | gotosocial-420e2fb22bc7aa4967ddadb11e444079efdf5117.tar.xz |
replace async client API / federator msg processing with worker pools (#497)
* replace async client API / federator msg processing with worker pools
* appease our lord-and-saviour, the linter
Diffstat (limited to 'internal/processing/account')
-rw-r--r-- | internal/processing/account/account.go | 39 | ||||
-rw-r--r-- | internal/processing/account/account_test.go | 19 | ||||
-rw-r--r-- | internal/processing/account/create.go | 4 | ||||
-rw-r--r-- | internal/processing/account/createblock.go | 12 | ||||
-rw-r--r-- | internal/processing/account/createfollow.go | 4 | ||||
-rw-r--r-- | internal/processing/account/delete.go | 10 | ||||
-rw-r--r-- | internal/processing/account/removeblock.go | 4 | ||||
-rw-r--r-- | internal/processing/account/removefollow.go | 8 | ||||
-rw-r--r-- | internal/processing/account/update.go | 4 |
9 files changed, 59 insertions, 45 deletions
diff --git a/internal/processing/account/account.go b/internal/processing/account/account.go index 2a9e5f898..c49df1a1a 100644 --- a/internal/processing/account/account.go +++ b/internal/processing/account/account.go @@ -33,6 +33,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/text" "github.com/superseriousbusiness/gotosocial/internal/typeutils" "github.com/superseriousbusiness/gotosocial/internal/visibility" + "github.com/superseriousbusiness/gotosocial/internal/worker" "github.com/superseriousbusiness/oauth2/v4" ) @@ -81,28 +82,28 @@ type Processor interface { } type processor struct { - tc typeutils.TypeConverter - mediaManager media.Manager - fromClientAPI chan messages.FromClientAPI - oauthServer oauth.Server - filter visibility.Filter - formatter text.Formatter - db db.DB - federator federation.Federator - parseMention gtsmodel.ParseMentionFunc + tc typeutils.TypeConverter + mediaManager media.Manager + clientWorker *worker.Worker[messages.FromClientAPI] + oauthServer oauth.Server + filter visibility.Filter + formatter text.Formatter + db db.DB + federator federation.Federator + parseMention gtsmodel.ParseMentionFunc } // New returns a new account processor. -func New(db db.DB, tc typeutils.TypeConverter, mediaManager media.Manager, oauthServer oauth.Server, fromClientAPI chan messages.FromClientAPI, federator federation.Federator, parseMention gtsmodel.ParseMentionFunc) Processor { +func New(db db.DB, tc typeutils.TypeConverter, mediaManager media.Manager, oauthServer oauth.Server, clientWorker *worker.Worker[messages.FromClientAPI], federator federation.Federator, parseMention gtsmodel.ParseMentionFunc) Processor { return &processor{ - tc: tc, - mediaManager: mediaManager, - fromClientAPI: fromClientAPI, - oauthServer: oauthServer, - filter: visibility.NewFilter(db), - formatter: text.NewFormatter(db), - db: db, - federator: federator, - parseMention: parseMention, + tc: tc, + mediaManager: mediaManager, + clientWorker: clientWorker, + oauthServer: oauthServer, + filter: visibility.NewFilter(db), + formatter: text.NewFormatter(db), + db: db, + federator: federator, + parseMention: parseMention, } } diff --git a/internal/processing/account/account_test.go b/internal/processing/account/account_test.go index aff4f02aa..33b744250 100644 --- a/internal/processing/account/account_test.go +++ b/internal/processing/account/account_test.go @@ -19,6 +19,8 @@ package account_test import ( + "context" + "codeberg.org/gruf/go-store/kv" "github.com/stretchr/testify/suite" "github.com/superseriousbusiness/activity/pub" @@ -33,6 +35,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/processing/account" "github.com/superseriousbusiness/gotosocial/internal/transport" "github.com/superseriousbusiness/gotosocial/internal/typeutils" + "github.com/superseriousbusiness/gotosocial/internal/worker" "github.com/superseriousbusiness/gotosocial/testrig" ) @@ -78,6 +81,16 @@ func (suite *AccountStandardTestSuite) SetupTest() { testrig.InitTestLog() testrig.InitTestConfig() + fedWorker := worker.New[messages.FromFederator](-1, -1) + clientWorker := worker.New[messages.FromClientAPI](-1, -1) + clientWorker.SetProcessor(func(_ context.Context, msg messages.FromClientAPI) error { + suite.fromClientAPIChan <- msg + return nil + }) + + _ = fedWorker.Start() + _ = clientWorker.Start() + suite.db = testrig.NewTestDB() suite.tc = testrig.NewTestTypeConverter(suite.db) suite.storage = testrig.NewTestStorage() @@ -85,11 +98,11 @@ func (suite *AccountStandardTestSuite) SetupTest() { suite.oauthServer = testrig.NewTestOauthServer(suite.db) suite.fromClientAPIChan = make(chan messages.FromClientAPI, 100) suite.httpClient = testrig.NewMockHTTPClient(nil) - suite.transportController = testrig.NewTestTransportController(suite.httpClient, suite.db) - suite.federator = testrig.NewTestFederator(suite.db, suite.transportController, suite.storage, suite.mediaManager) + suite.transportController = testrig.NewTestTransportController(suite.httpClient, suite.db, fedWorker) + suite.federator = testrig.NewTestFederator(suite.db, suite.transportController, suite.storage, suite.mediaManager, fedWorker) suite.sentEmails = make(map[string]string) suite.emailSender = testrig.NewEmailSender("../../../web/template/", suite.sentEmails) - suite.accountProcessor = account.New(suite.db, suite.tc, suite.mediaManager, suite.oauthServer, suite.fromClientAPIChan, suite.federator, processing.GetParseMentionFunc(suite.db, suite.federator)) + suite.accountProcessor = account.New(suite.db, suite.tc, suite.mediaManager, suite.oauthServer, clientWorker, suite.federator, processing.GetParseMentionFunc(suite.db, suite.federator)) testrig.StandardDBSetup(suite.db, nil) testrig.StandardStorageSetup(suite.storage, "../../../testrig/media") } diff --git a/internal/processing/account/create.go b/internal/processing/account/create.go index 992dcf6b1..bbca11fae 100644 --- a/internal/processing/account/create.go +++ b/internal/processing/account/create.go @@ -85,12 +85,12 @@ func (p *processor) Create(ctx context.Context, applicationToken oauth2.TokenInf // there are side effects for creating a new account (sending confirmation emails etc) // so pass a message to the processor so that it can do it asynchronously - p.fromClientAPI <- messages.FromClientAPI{ + p.clientWorker.Queue(messages.FromClientAPI{ APObjectType: ap.ObjectProfile, APActivityType: ap.ActivityCreate, GTSModel: user.Account, OriginAccount: user.Account, - } + }) return &apimodel.Token{ AccessToken: accessToken.GetAccess(), diff --git a/internal/processing/account/createblock.go b/internal/processing/account/createblock.go index 6e05866d7..e1bad0d38 100644 --- a/internal/processing/account/createblock.go +++ b/internal/processing/account/createblock.go @@ -113,7 +113,7 @@ func (p *processor) BlockCreate(ctx context.Context, requestingAccount *gtsmodel // follow request status changed so send the UNDO activity to the channel for async processing if frChanged { - p.fromClientAPI <- messages.FromClientAPI{ + p.clientWorker.Queue(messages.FromClientAPI{ APObjectType: ap.ActivityFollow, APActivityType: ap.ActivityUndo, GTSModel: >smodel.Follow{ @@ -123,12 +123,12 @@ func (p *processor) BlockCreate(ctx context.Context, requestingAccount *gtsmodel }, OriginAccount: requestingAccount, TargetAccount: targetAccount, - } + }) } // follow status changed so send the UNDO activity to the channel for async processing if fChanged { - p.fromClientAPI <- messages.FromClientAPI{ + p.clientWorker.Queue(messages.FromClientAPI{ APObjectType: ap.ActivityFollow, APActivityType: ap.ActivityUndo, GTSModel: >smodel.Follow{ @@ -138,17 +138,17 @@ func (p *processor) BlockCreate(ctx context.Context, requestingAccount *gtsmodel }, OriginAccount: requestingAccount, TargetAccount: targetAccount, - } + }) } // handle the rest of the block process asynchronously - p.fromClientAPI <- messages.FromClientAPI{ + p.clientWorker.Queue(messages.FromClientAPI{ APObjectType: ap.ActivityBlock, APActivityType: ap.ActivityCreate, GTSModel: block, OriginAccount: requestingAccount, TargetAccount: targetAccount, - } + }) return p.RelationshipGet(ctx, requestingAccount, targetAccountID) } diff --git a/internal/processing/account/createfollow.go b/internal/processing/account/createfollow.go index 5f4eecba7..cd2dcbd8c 100644 --- a/internal/processing/account/createfollow.go +++ b/internal/processing/account/createfollow.go @@ -101,13 +101,13 @@ func (p *processor) FollowCreate(ctx context.Context, requestingAccount *gtsmode } // otherwise we leave the follow request as it is and we handle the rest of the process asynchronously - p.fromClientAPI <- messages.FromClientAPI{ + p.clientWorker.Queue(messages.FromClientAPI{ APObjectType: ap.ActivityFollow, APActivityType: ap.ActivityCreate, GTSModel: fr, OriginAccount: requestingAccount, TargetAccount: targetAcct, - } + }) // return whatever relationship results from this return p.RelationshipGet(ctx, requestingAccount, form.ID) diff --git a/internal/processing/account/delete.go b/internal/processing/account/delete.go index a114777cf..5b40804d8 100644 --- a/internal/processing/account/delete.go +++ b/internal/processing/account/delete.go @@ -159,13 +159,13 @@ selectStatusesLoop: // pass the status delete through the client api channel for processing s.Account = account l.Debug("putting status in the client api channel") - p.fromClientAPI <- messages.FromClientAPI{ + p.clientWorker.Queue(messages.FromClientAPI{ APObjectType: ap.ObjectNote, APActivityType: ap.ActivityDelete, GTSModel: s, OriginAccount: account, TargetAccount: account, - } + }) if err := p.db.DeleteByID(ctx, s.ID, s); err != nil { if err != db.ErrNoEntries { @@ -195,13 +195,13 @@ selectStatusesLoop: } l.Debug("putting boost undo in the client api channel") - p.fromClientAPI <- messages.FromClientAPI{ + p.clientWorker.Queue(messages.FromClientAPI{ APObjectType: ap.ActivityAnnounce, APActivityType: ap.ActivityUndo, GTSModel: s, OriginAccount: b.Account, TargetAccount: account, - } + }) if err := p.db.DeleteByID(ctx, b.ID, b); err != nil { if err != db.ErrNoEntries { @@ -331,7 +331,7 @@ func (p *processor) DeleteLocal(ctx context.Context, account *gtsmodel.Account, } // put the delete in the processor queue to handle the rest of it asynchronously - p.fromClientAPI <- fromClientAPIMessage + p.clientWorker.Queue(fromClientAPIMessage) return nil } diff --git a/internal/processing/account/removeblock.go b/internal/processing/account/removeblock.go index a28497a2a..f15350a12 100644 --- a/internal/processing/account/removeblock.go +++ b/internal/processing/account/removeblock.go @@ -54,13 +54,13 @@ func (p *processor) BlockRemove(ctx context.Context, requestingAccount *gtsmodel // block status changed so send the UNDO activity to the channel for async processing if blockChanged { - p.fromClientAPI <- messages.FromClientAPI{ + p.clientWorker.Queue(messages.FromClientAPI{ APObjectType: ap.ActivityBlock, APActivityType: ap.ActivityUndo, GTSModel: block, OriginAccount: requestingAccount, TargetAccount: targetAccount, - } + }) } // return whatever relationship results from all this diff --git a/internal/processing/account/removefollow.go b/internal/processing/account/removefollow.go index 4cfea7790..82020b604 100644 --- a/internal/processing/account/removefollow.go +++ b/internal/processing/account/removefollow.go @@ -80,7 +80,7 @@ func (p *processor) FollowRemove(ctx context.Context, requestingAccount *gtsmode // follow request status changed so send the UNDO activity to the channel for async processing if frChanged { - p.fromClientAPI <- messages.FromClientAPI{ + p.clientWorker.Queue(messages.FromClientAPI{ APObjectType: ap.ActivityFollow, APActivityType: ap.ActivityUndo, GTSModel: >smodel.Follow{ @@ -90,12 +90,12 @@ func (p *processor) FollowRemove(ctx context.Context, requestingAccount *gtsmode }, OriginAccount: requestingAccount, TargetAccount: targetAcct, - } + }) } // follow status changed so send the UNDO activity to the channel for async processing if fChanged { - p.fromClientAPI <- messages.FromClientAPI{ + p.clientWorker.Queue(messages.FromClientAPI{ APObjectType: ap.ActivityFollow, APActivityType: ap.ActivityUndo, GTSModel: >smodel.Follow{ @@ -105,7 +105,7 @@ func (p *processor) FollowRemove(ctx context.Context, requestingAccount *gtsmode }, OriginAccount: requestingAccount, TargetAccount: targetAcct, - } + }) } // return whatever relationship results from all this diff --git a/internal/processing/account/update.go b/internal/processing/account/update.go index a0056563b..738aa8c88 100644 --- a/internal/processing/account/update.go +++ b/internal/processing/account/update.go @@ -117,12 +117,12 @@ func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form return nil, fmt.Errorf("could not update account %s: %s", account.ID, err) } - p.fromClientAPI <- messages.FromClientAPI{ + p.clientWorker.Queue(messages.FromClientAPI{ APObjectType: ap.ObjectProfile, APActivityType: ap.ActivityUpdate, GTSModel: updatedAccount, OriginAccount: updatedAccount, - } + }) acctSensitive, err := p.tc.AccountToAPIAccountSensitive(ctx, updatedAccount) if err != nil { |