[performance] cached oauth database types (#2838)

* update token + client code to use struct caches

* add code comments

* slight tweak to default mem ratios

* fix envparsing

* add appropriate invalidate hooks

* update the tokenstore sweeping function to rely on caches

* update to use PutClient()

* add ClientID to list of token struct indices
This commit is contained in:
kim
2024-04-15 14:22:21 +01:00
committed by GitHub
parent 8b30709791
commit f79d50b9b2
18 changed files with 428 additions and 67 deletions

View File

@@ -27,11 +27,11 @@ import (
)
type clientStore struct {
db db.Basic
db db.DB
}
// NewClientStore returns an implementation of the oauth2 ClientStore interface, using the given db as a storage backend.
func NewClientStore(db db.Basic) oauth2.ClientStore {
func NewClientStore(db db.DB) oauth2.ClientStore {
pts := &clientStore{
db: db,
}
@@ -39,26 +39,27 @@ func NewClientStore(db db.Basic) oauth2.ClientStore {
}
func (cs *clientStore) GetByID(ctx context.Context, clientID string) (oauth2.ClientInfo, error) {
poc := &gtsmodel.Client{}
if err := cs.db.GetByID(ctx, clientID, poc); err != nil {
client, err := cs.db.GetClientByID(ctx, clientID)
if err != nil {
return nil, err
}
return models.New(poc.ID, poc.Secret, poc.Domain, poc.UserID), nil
return models.New(
client.ID,
client.Secret,
client.Domain,
client.UserID,
), nil
}
func (cs *clientStore) Set(ctx context.Context, id string, cli oauth2.ClientInfo) error {
poc := &gtsmodel.Client{
return cs.db.PutClient(ctx, &gtsmodel.Client{
ID: cli.GetID(),
Secret: cli.GetSecret(),
Domain: cli.GetDomain(),
UserID: cli.GetUserID(),
}
return cs.db.Put(ctx, poc)
})
}
func (cs *clientStore) Delete(ctx context.Context, id string) error {
poc := &gtsmodel.Client{
ID: id,
}
return cs.db.DeleteByID(ctx, id, poc)
return cs.db.DeleteClientByID(ctx, id)
}

View File

@@ -19,7 +19,5 @@ package oauth
import "github.com/superseriousbusiness/oauth2/v4/errors"
// InvalidRequest returns an oauth spec compliant 'invalid_request' error.
func InvalidRequest() error {
return errors.New("invalid_request")
}
// ErrInvalidRequest is an oauth spec compliant 'invalid_request' error.
var ErrInvalidRequest = errors.New("invalid_request")

View File

@@ -75,7 +75,7 @@ type s struct {
}
// New returns a new oauth server that implements the Server interface
func New(ctx context.Context, database db.Basic) Server {
func New(ctx context.Context, database db.DB) Server {
ts := newTokenStore(ctx, database)
cs := NewClientStore(database)

View File

@@ -20,7 +20,6 @@ package oauth
import (
"context"
"errors"
"fmt"
"time"
"github.com/superseriousbusiness/gotosocial/internal/db"
@@ -34,14 +33,14 @@ import (
// tokenStore is an implementation of oauth2.TokenStore, which uses our db interface as a storage backend.
type tokenStore struct {
oauth2.TokenStore
db db.Basic
db db.DB
}
// newTokenStore returns a token store that satisfies the oauth2.TokenStore interface.
//
// In order to allow tokens to 'expire', it will also set off a goroutine that iterates through
// the tokens in the DB once per minute and deletes any that have expired.
func newTokenStore(ctx context.Context, db db.Basic) oauth2.TokenStore {
func newTokenStore(ctx context.Context, db db.DB) oauth2.TokenStore {
ts := &tokenStore{
db: db,
}
@@ -69,19 +68,19 @@ func newTokenStore(ctx context.Context, db db.Basic) oauth2.TokenStore {
func (ts *tokenStore) sweep(ctx context.Context) error {
// select *all* tokens from the db
// todo: if this becomes expensive (ie., there are fucking LOADS of tokens) then figure out a better way.
tokens := new([]*gtsmodel.Token)
if err := ts.db.GetAll(ctx, tokens); err != nil {
tokens, err := ts.db.GetAllTokens(ctx)
if err != nil {
return err
}
// iterate through and remove expired tokens
now := time.Now()
for _, dbt := range *tokens {
for _, dbt := range tokens {
// The zero value of a time.Time is 00:00 january 1 1970, which will always be before now. So:
// we only want to check if a token expired before now if the expiry time is *not zero*;
// ie., if it's been explicity set.
if !dbt.CodeExpiresAt.IsZero() && dbt.CodeExpiresAt.Before(now) || !dbt.RefreshExpiresAt.IsZero() && dbt.RefreshExpiresAt.Before(now) || !dbt.AccessExpiresAt.IsZero() && dbt.AccessExpiresAt.Before(now) {
if err := ts.db.DeleteByID(ctx, dbt.ID, dbt); err != nil {
if err := ts.db.DeleteTokenByID(ctx, dbt.ID); err != nil {
return err
}
}
@@ -107,67 +106,49 @@ func (ts *tokenStore) Create(ctx context.Context, info oauth2.TokenInfo) error {
dbt.ID = dbtID
}
if err := ts.db.Put(ctx, dbt); err != nil {
return fmt.Errorf("error in tokenstore create: %s", err)
}
return nil
return ts.db.PutToken(ctx, dbt)
}
// RemoveByCode deletes a token from the DB based on the Code field
func (ts *tokenStore) RemoveByCode(ctx context.Context, code string) error {
return ts.db.DeleteWhere(ctx, []db.Where{{Key: "code", Value: code}}, &gtsmodel.Token{})
return ts.db.DeleteTokenByCode(ctx, code)
}
// RemoveByAccess deletes a token from the DB based on the Access field
func (ts *tokenStore) RemoveByAccess(ctx context.Context, access string) error {
return ts.db.DeleteWhere(ctx, []db.Where{{Key: "access", Value: access}}, &gtsmodel.Token{})
return ts.db.DeleteTokenByAccess(ctx, access)
}
// RemoveByRefresh deletes a token from the DB based on the Refresh field
func (ts *tokenStore) RemoveByRefresh(ctx context.Context, refresh string) error {
return ts.db.DeleteWhere(ctx, []db.Where{{Key: "refresh", Value: refresh}}, &gtsmodel.Token{})
return ts.db.DeleteTokenByRefresh(ctx, refresh)
}
// GetByCode selects a token from the DB based on the Code field
func (ts *tokenStore) GetByCode(ctx context.Context, code string) (oauth2.TokenInfo, error) {
if code == "" {
return nil, nil
}
dbt := &gtsmodel.Token{
Code: code,
}
if err := ts.db.GetWhere(ctx, []db.Where{{Key: "code", Value: code}}, dbt); err != nil {
token, err := ts.db.GetTokenByCode(ctx, code)
if err != nil {
return nil, err
}
return DBTokenToToken(dbt), nil
return DBTokenToToken(token), nil
}
// GetByAccess selects a token from the DB based on the Access field
func (ts *tokenStore) GetByAccess(ctx context.Context, access string) (oauth2.TokenInfo, error) {
if access == "" {
return nil, nil
}
dbt := &gtsmodel.Token{
Access: access,
}
if err := ts.db.GetWhere(ctx, []db.Where{{Key: "access", Value: access}}, dbt); err != nil {
token, err := ts.db.GetTokenByAccess(ctx, access)
if err != nil {
return nil, err
}
return DBTokenToToken(dbt), nil
return DBTokenToToken(token), nil
}
// GetByRefresh selects a token from the DB based on the Refresh field
func (ts *tokenStore) GetByRefresh(ctx context.Context, refresh string) (oauth2.TokenInfo, error) {
if refresh == "" {
return nil, nil
}
dbt := &gtsmodel.Token{
Refresh: refresh,
}
if err := ts.db.GetWhere(ctx, []db.Where{{Key: "refresh", Value: refresh}}, dbt); err != nil {
token, err := ts.db.GetTokenByRefresh(ctx, refresh)
if err != nil {
return nil, err
}
return DBTokenToToken(dbt), nil
return DBTokenToToken(token), nil
}
/*