feat: add option sessionConnection for boltdb

This commit is contained in:
fiftin 2024-07-14 15:28:16 +05:00
parent d0e95bbcef
commit 3605bf25cd
No known key found for this signature in database
GPG Key ID: 044381366A5D4731
2 changed files with 52 additions and 20 deletions

View File

@ -230,6 +230,9 @@ jobs:
needs:
- migrate-boltdb
env:
SEMAPHORE_DB_OPTIONS: '{"sessionConnection": "true"}'
steps:
- name: Checkout source
uses: actions/checkout@v4

View File

@ -73,24 +73,7 @@ func (d *BoltDb) Migrate() error {
return nil
}
func (d *BoltDb) Connect(token string) {
d.mu.Lock()
defer d.mu.Unlock()
if d.connections == nil {
d.connections = make(map[string]bool)
}
if _, exists := d.connections[token]; exists {
// Use for debugging
panic(fmt.Errorf("Connection " + token + " already exists"))
}
if len(d.connections) > 0 {
d.connections[token] = true
return
}
func (d *BoltDb) openDbFile() {
var filename string
if d.Filename == "" {
config, err := util.Config.GetDBConfig()
@ -110,11 +93,40 @@ func (d *BoltDb) Connect(token string) {
if err != nil {
panic(err)
}
}
func (d *BoltDb) openSession(token string) {
d.mu.Lock()
defer d.mu.Unlock()
if d.connections == nil {
d.connections = make(map[string]bool)
}
if _, exists := d.connections[token]; exists {
// Use for debugging
panic(fmt.Errorf("Connection " + token + " already exists"))
}
if len(d.connections) > 0 {
d.connections[token] = true
return
}
d.openDbFile()
d.connections[token] = true
}
func (d *BoltDb) Close(token string) {
func (d *BoltDb) Connect(token string) {
if d.PermanentConnection() {
d.openDbFile()
} else {
d.openSession(token)
}
}
func (d *BoltDb) closeSession(token string) {
d.mu.Lock()
defer d.mu.Unlock()
@ -139,8 +151,25 @@ func (d *BoltDb) Close(token string) {
delete(d.connections, token)
}
func (d *BoltDb) Close(token string) {
if d.PermanentConnection() {
if err := d.db.Close(); err != nil {
panic(err)
}
} else {
d.closeSession(token)
}
}
func (d *BoltDb) PermanentConnection() bool {
return false
config, err := util.Config.GetDBConfig()
if err != nil {
panic(err)
}
isSessionConnection, ok := config.Options["sessionConnection"]
return ok && (isSessionConnection == "true" || isSessionConnection == "yes")
}
func (d *BoltDb) IsInitialized() (initialized bool, err error) {