mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-10-31 13:42:25 -05:00 
			
		
		
		
	Replace federating DB locks map, add a cleanup goroutine (#166)
Signed-off-by: kim (grufwub) <grufwub@gmail.com>
This commit is contained in:
		
					parent
					
						
							
								add6eb6e2b
							
						
					
				
			
			
				commit
				
					
						79afcdba3f
					
				
			
		
					 2 changed files with 74 additions and 11 deletions
				
			
		|  | @ -21,6 +21,7 @@ package federatingdb | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"sync" | 	"sync" | ||||||
|  | 	"time" | ||||||
| 
 | 
 | ||||||
| 	"github.com/go-fed/activity/pub" | 	"github.com/go-fed/activity/pub" | ||||||
| 	"github.com/go-fed/activity/streams/vocab" | 	"github.com/go-fed/activity/streams/vocab" | ||||||
|  | @ -41,7 +42,9 @@ type DB interface { | ||||||
| // FederatingDB uses the underlying DB interface to implement the go-fed pub.Database interface. | // FederatingDB uses the underlying DB interface to implement the go-fed pub.Database interface. | ||||||
| // It doesn't care what the underlying implementation of the DB interface is, as long as it works. | // It doesn't care what the underlying implementation of the DB interface is, as long as it works. | ||||||
| type federatingDB struct { | type federatingDB struct { | ||||||
| 	locks         *sync.Map | 	mutex         sync.Mutex | ||||||
|  | 	locks         map[string]*mutex | ||||||
|  | 	pool          sync.Pool | ||||||
| 	db            db.DB | 	db            db.DB | ||||||
| 	config        *config.Config | 	config        *config.Config | ||||||
| 	log           *logrus.Logger | 	log           *logrus.Logger | ||||||
|  | @ -50,11 +53,32 @@ type federatingDB struct { | ||||||
| 
 | 
 | ||||||
| // New returns a DB interface using the given database, config, and logger. | // New returns a DB interface using the given database, config, and logger. | ||||||
| func New(db db.DB, config *config.Config, log *logrus.Logger) DB { | func New(db db.DB, config *config.Config, log *logrus.Logger) DB { | ||||||
| 	return &federatingDB{ | 	fdb := federatingDB{ | ||||||
| 		locks:         new(sync.Map), | 		mutex:         sync.Mutex{}, | ||||||
|  | 		locks:         make(map[string]*mutex, 100), | ||||||
|  | 		pool:          sync.Pool{New: func() interface{} { return &mutex{} }}, | ||||||
| 		db:            db, | 		db:            db, | ||||||
| 		config:        config, | 		config:        config, | ||||||
| 		log:           log, | 		log:           log, | ||||||
| 		typeConverter: typeutils.NewConverter(config, db), | 		typeConverter: typeutils.NewConverter(config, db), | ||||||
| 	} | 	} | ||||||
|  | 	go fdb.cleanupLocks() | ||||||
|  | 	return &fdb | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (db *federatingDB) cleanupLocks() { | ||||||
|  | 	for { | ||||||
|  | 		// Sleep for a minute... | ||||||
|  | 		time.Sleep(time.Minute) | ||||||
|  | 
 | ||||||
|  | 		// Delete unused locks from map | ||||||
|  | 		db.mutex.Lock() | ||||||
|  | 		for id, mu := range db.locks { | ||||||
|  | 			if !mu.inUse() { | ||||||
|  | 				delete(db.locks, id) | ||||||
|  | 				db.pool.Put(mu) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		db.mutex.Unlock() | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -23,6 +23,7 @@ import ( | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"net/url" | 	"net/url" | ||||||
| 	"sync" | 	"sync" | ||||||
|  | 	"sync/atomic" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // Lock takes a lock for the object at the specified id. If an error | // Lock takes a lock for the object at the specified id. If an error | ||||||
|  | @ -45,14 +46,21 @@ func (f *federatingDB) Lock(c context.Context, id *url.URL) error { | ||||||
| 	if id == nil { | 	if id == nil { | ||||||
| 		return errors.New("Lock: id was nil") | 		return errors.New("Lock: id was nil") | ||||||
| 	} | 	} | ||||||
|  | 	idStr := id.String() | ||||||
| 
 | 
 | ||||||
| 	mu := &sync.Mutex{} | 	// Acquire map lock | ||||||
| 	mu.Lock() // Optimistically lock if we do store it. | 	f.mutex.Lock() | ||||||
| 	i, loaded := f.locks.LoadOrStore(id.String(), mu) | 
 | ||||||
| 	if loaded { | 	// Get mutex, or create new | ||||||
| 		mu = i.(*sync.Mutex) | 	mu, ok := f.locks[idStr] | ||||||
| 		mu.Lock() | 	if !ok { | ||||||
|  | 		mu = f.pool.Get().(*mutex) | ||||||
|  | 		f.locks[idStr] = mu | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	// Unlock map, acquire mutex lock | ||||||
|  | 	f.mutex.Unlock() | ||||||
|  | 	mu.Lock() | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -66,12 +74,43 @@ func (f *federatingDB) Unlock(c context.Context, id *url.URL) error { | ||||||
| 	if id == nil { | 	if id == nil { | ||||||
| 		return errors.New("Unlock: id was nil") | 		return errors.New("Unlock: id was nil") | ||||||
| 	} | 	} | ||||||
|  | 	idStr := id.String() | ||||||
|  | 
 | ||||||
|  | 	// Check map for mutex | ||||||
|  | 	f.mutex.Lock() | ||||||
|  | 	mu, ok := f.locks[idStr] | ||||||
|  | 	f.mutex.Unlock() | ||||||
| 
 | 
 | ||||||
| 	i, ok := f.locks.Load(id.String()) |  | ||||||
| 	if !ok { | 	if !ok { | ||||||
| 		return errors.New("missing an id in unlock") | 		return errors.New("missing an id in unlock") | ||||||
| 	} | 	} | ||||||
| 	mu := i.(*sync.Mutex) | 
 | ||||||
|  | 	// Unlock the mutex | ||||||
| 	mu.Unlock() | 	mu.Unlock() | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | // mutex defines a mutex we can check the lock status of. | ||||||
|  | // this is not perfect, but it's good enough for a semi | ||||||
|  | // regular mutex cleanup routine | ||||||
|  | type mutex struct { | ||||||
|  | 	mu sync.Mutex | ||||||
|  | 	st uint32 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // inUse returns if the mutex is in use | ||||||
|  | func (mu *mutex) inUse() bool { | ||||||
|  | 	return atomic.LoadUint32(&mu.st) == 1 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Lock acquire mutex lock | ||||||
|  | func (mu *mutex) Lock() { | ||||||
|  | 	mu.mu.Lock() | ||||||
|  | 	atomic.StoreUint32(&mu.st, 1) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Unlock releases mutex lock | ||||||
|  | func (mu *mutex) Unlock() { | ||||||
|  | 	mu.mu.Unlock() | ||||||
|  | 	atomic.StoreUint32(&mu.st, 0) | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue