Skip to content

Commit

Permalink
[chore] move caches to a separate State{} structure (#1078)
Browse files Browse the repository at this point in the history
* move caches to a separate State{} structure

Signed-off-by: kim <grufwub@gmail.com>

* fix call to log.Panic not using formatted call

Signed-off-by: kim <grufwub@gmail.com>

* move caches to use interfaces, to make switchouts easier in future

Signed-off-by: kim <grufwub@gmail.com>

* fix rebase issue

Signed-off-by: kim <grufwub@gmail.com>

* improve code comment

Signed-off-by: kim <grufwub@gmail.com>

* fix further issues after rebase

Signed-off-by: kim <grufwub@gmail.com>

* heh

Signed-off-by: kim <grufwub@gmail.com>

* add missing license text

Signed-off-by: kim <grufwub@gmail.com>

Signed-off-by: kim <grufwub@gmail.com>
  • Loading branch information
NyaaaWhatsUpDoc authored Dec 8, 2022
1 parent dd1a4cd commit e58d2d8
Show file tree
Hide file tree
Showing 27 changed files with 725 additions and 332 deletions.
49 changes: 43 additions & 6 deletions cmd/gotosocial/action/admin/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,24 @@ import (
"github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
"github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/superseriousbusiness/gotosocial/internal/validate"
"golang.org/x/crypto/bcrypt"
)

// Create creates a new account in the database using the provided flags.
var Create action.GTSAction = func(ctx context.Context) error {
dbConn, err := bundb.NewBunDBService(ctx)
var state state.State
state.Caches.Init()

dbConn, err := bundb.NewBunDBService(ctx, &state)
if err != nil {
return fmt.Errorf("error creating dbservice: %s", err)
}

// Set the state DB connection
state.DB = dbConn

username := config.GetAdminAccountUsername()
if username == "" {
return errors.New("no username set")
Expand Down Expand Up @@ -88,11 +95,17 @@ var Create action.GTSAction = func(ctx context.Context) error {

// Confirm sets a user to Approved, sets Email to the current UnconfirmedEmail value, and sets ConfirmedAt to now.
var Confirm action.GTSAction = func(ctx context.Context) error {
dbConn, err := bundb.NewBunDBService(ctx)
var state state.State
state.Caches.Init()

dbConn, err := bundb.NewBunDBService(ctx, &state)
if err != nil {
return fmt.Errorf("error creating dbservice: %s", err)
}

// Set the state DB connection
state.DB = dbConn

username := config.GetAdminAccountUsername()
if username == "" {
return errors.New("no username set")
Expand Down Expand Up @@ -125,11 +138,17 @@ var Confirm action.GTSAction = func(ctx context.Context) error {

// Promote sets a user to admin.
var Promote action.GTSAction = func(ctx context.Context) error {
dbConn, err := bundb.NewBunDBService(ctx)
var state state.State
state.Caches.Init()

dbConn, err := bundb.NewBunDBService(ctx, &state)
if err != nil {
return fmt.Errorf("error creating dbservice: %s", err)
}

// Set the state DB connection
state.DB = dbConn

username := config.GetAdminAccountUsername()
if username == "" {
return errors.New("no username set")
Expand Down Expand Up @@ -159,11 +178,17 @@ var Promote action.GTSAction = func(ctx context.Context) error {

// Demote sets admin on a user to false.
var Demote action.GTSAction = func(ctx context.Context) error {
dbConn, err := bundb.NewBunDBService(ctx)
var state state.State
state.Caches.Init()

dbConn, err := bundb.NewBunDBService(ctx, &state)
if err != nil {
return fmt.Errorf("error creating dbservice: %s", err)
}

// Set the state DB connection
state.DB = dbConn

username := config.GetAdminAccountUsername()
if username == "" {
return errors.New("no username set")
Expand Down Expand Up @@ -193,11 +218,17 @@ var Demote action.GTSAction = func(ctx context.Context) error {

// Disable sets Disabled to true on a user.
var Disable action.GTSAction = func(ctx context.Context) error {
dbConn, err := bundb.NewBunDBService(ctx)
var state state.State
state.Caches.Init()

dbConn, err := bundb.NewBunDBService(ctx, &state)
if err != nil {
return fmt.Errorf("error creating dbservice: %s", err)
}

// Set the state DB connection
state.DB = dbConn

username := config.GetAdminAccountUsername()
if username == "" {
return errors.New("no username set")
Expand Down Expand Up @@ -227,11 +258,17 @@ var Disable action.GTSAction = func(ctx context.Context) error {

// Password sets the password of target account.
var Password action.GTSAction = func(ctx context.Context) error {
dbConn, err := bundb.NewBunDBService(ctx)
var state state.State
state.Caches.Init()

dbConn, err := bundb.NewBunDBService(ctx, &state)
if err != nil {
return fmt.Errorf("error creating dbservice: %s", err)
}

// Set the state DB connection
state.DB = dbConn

username := config.GetAdminAccountUsername()
if username == "" {
return errors.New("no username set")
Expand Down
8 changes: 6 additions & 2 deletions cmd/gotosocial/action/admin/media/prune/orphaned.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/media"
"github.com/superseriousbusiness/gotosocial/internal/state"
gtsstorage "github.com/superseriousbusiness/gotosocial/internal/storage"
)

// Orphaned prunes orphaned media from storage.
var Orphaned action.GTSAction = func(ctx context.Context) error {
dbService, err := bundb.NewBunDBService(ctx)
var state state.State
state.Caches.Init()

dbService, err := bundb.NewBunDBService(ctx, &state)
if err != nil {
return fmt.Errorf("error creating dbservice: %s", err)
}
Expand All @@ -54,7 +58,7 @@ var Orphaned action.GTSAction = func(ctx context.Context) error {
return fmt.Errorf("error pruning: %s", err)
}

if dry {
if dry /* dick heyyoooooo */ {
log.Infof("DRY RUN: %d stored items are orphaned and eligible to be pruned", pruned)
} else {
log.Infof("%d stored items were orphaned and pruned", pruned)
Expand Down
8 changes: 7 additions & 1 deletion cmd/gotosocial/action/admin/trans/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ import (
"github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
"github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/superseriousbusiness/gotosocial/internal/trans"
)

// Export exports info from the database into a file
var Export action.GTSAction = func(ctx context.Context) error {
dbConn, err := bundb.NewBunDBService(ctx)
var state state.State

dbConn, err := bundb.NewBunDBService(ctx, &state)
if err != nil {
return fmt.Errorf("error creating dbservice: %s", err)
}

// Set the state DB connection
state.DB = dbConn

exporter := trans.NewExporter(dbConn)

path := config.GetAdminTransPath()
Expand Down
8 changes: 7 additions & 1 deletion cmd/gotosocial/action/admin/trans/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ import (
"github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
"github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/superseriousbusiness/gotosocial/internal/trans"
)

// Import imports info from a file into the database
var Import action.GTSAction = func(ctx context.Context) error {
dbConn, err := bundb.NewBunDBService(ctx)
var state state.State

dbConn, err := bundb.NewBunDBService(ctx, &state)
if err != nil {
return fmt.Errorf("error creating dbservice: %s", err)
}

// Set the state DB connection
state.DB = dbConn

importer := trans.NewImporter(dbConn)

path := config.GetAdminTransPath()
Expand Down
12 changes: 11 additions & 1 deletion cmd/gotosocial/action/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/oidc"
"github.com/superseriousbusiness/gotosocial/internal/processing"
"github.com/superseriousbusiness/gotosocial/internal/router"
"github.com/superseriousbusiness/gotosocial/internal/state"
gtsstorage "github.com/superseriousbusiness/gotosocial/internal/storage"
"github.com/superseriousbusiness/gotosocial/internal/transport"
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
Expand All @@ -73,11 +74,20 @@ import (

// Start creates and starts a gotosocial server
var Start action.GTSAction = func(ctx context.Context) error {
dbService, err := bundb.NewBunDBService(ctx)
var state state.State

// Initialize caches
state.Caches.Init()

// Open connection to the database
dbService, err := bundb.NewBunDBService(ctx, &state)
if err != nil {
return fmt.Errorf("error creating dbservice: %s", err)
}

// Set the state DB connection
state.DB = dbService

if err := dbService.CreateInstanceAccount(ctx); err != nil {
return fmt.Errorf("error creating instance account: %s", err)
}
Expand Down
44 changes: 44 additions & 0 deletions internal/cache/ap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
GoToSocial
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package cache

type APCaches interface {
// Init will initialize all the ActivityPub caches in this collection.
// NOTE: the cache MUST NOT be in use anywhere, this is not thread-safe.
Init()

// Start will attempt to start all of the ActivityPub caches, or panic.
Start()

// Stop will attempt to stop all of the ActivityPub caches, or panic.
Stop()
}

// NewAP returns a new default implementation of APCaches.
func NewAP() APCaches {
return &apCaches{}
}

type apCaches struct{}

func (c *apCaches) Init() {}

func (c *apCaches) Start() {}

func (c *apCaches) Stop() {}
60 changes: 60 additions & 0 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
GoToSocial
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package cache

type Caches struct {
// GTS provides access to the collection of gtsmodel object caches.
GTS GTSCaches

// AP provides access to the collection of ActivityPub object caches.
AP APCaches

// prevent pass-by-value.
_ nocopy
}

// Init will (re)initialize both the GTS and AP cache collections.
// NOTE: the cache MUST NOT be in use anywhere, this is not thread-safe.
func (c *Caches) Init() {
if c.GTS == nil {
// use default impl
c.GTS = NewGTS()
}

if c.AP == nil {
// use default impl
c.AP = NewAP()
}

// initialize caches
c.GTS.Init()
c.AP.Init()
}

// Start will start both the GTS and AP cache collections.
func (c *Caches) Start() {
c.GTS.Start()
c.AP.Start()
}

// Stop will stop both the GTS and AP cache collections.
func (c *Caches) Stop() {
c.GTS.Stop()
c.AP.Stop()
}
Loading

0 comments on commit e58d2d8

Please sign in to comment.