mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-07 05:34:38 +08:00
fix(go): allow creation of extclient;
This commit is contained in:
parent
3875d12d3a
commit
a115ad0547
4 changed files with 57 additions and 8 deletions
|
@ -4,9 +4,9 @@ import (
|
|||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/gravitl/netmaker/db"
|
||||
_ "github.com/mattn/go-sqlite3" // need to blank import this package
|
||||
"time"
|
||||
)
|
||||
|
||||
// SqliteDB is the db object for sqlite database connections
|
||||
|
@ -34,8 +34,6 @@ func initSqliteDB() error {
|
|||
return dbOpenErr
|
||||
}
|
||||
|
||||
SqliteDB.SetMaxOpenConns(5)
|
||||
SqliteDB.SetConnMaxLifetime(time.Hour)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gravitl/netmaker/config"
|
||||
"gorm.io/driver/postgres"
|
||||
|
@ -29,9 +30,22 @@ func (pg *postgresConnector) connect() (*gorm.DB, error) {
|
|||
pgConf.SSLMode,
|
||||
)
|
||||
|
||||
return gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pgDB, err := db.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pgDB.SetMaxOpenConns(5)
|
||||
pgDB.SetConnMaxLifetime(time.Hour)
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func GetSQLConf() config.SQLConfig {
|
||||
|
|
34
db/sqlite.go
34
db/sqlite.go
|
@ -1,8 +1,10 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
@ -49,7 +51,37 @@ func (s *sqliteConnector) connect() (*gorm.DB, error) {
|
|||
}
|
||||
}
|
||||
|
||||
return gorm.Open(sqlite.Open(dbFilePath), &gorm.Config{
|
||||
// By default, SQLite transactions may keep writes invisible to other readers
|
||||
// until the transaction commits and the connection closes. This is especially
|
||||
// noticeable when using multiple connections from a pool.
|
||||
//
|
||||
// We configure the DSN to improve visibility and concurrency:
|
||||
//
|
||||
// 1. _journal_mode=wal: Use Write-Ahead Logging so committed writes are visible
|
||||
// to other connections immediately, and readers don't block writers. The default
|
||||
// journal mode is DELETE, which is not suitable for concurrent access.
|
||||
//
|
||||
// 2. _txlock=immediate: Acquire a RESERVED write lock as soon as the transaction
|
||||
// begins, avoiding mid-transaction "database is locked" errors. The default is
|
||||
// deferred, which may cause a transaction to rollback if another transaction has
|
||||
// already acquired a RESERVED lock.
|
||||
//
|
||||
// 3. _busy_timeout=5000: Wait up to 5 seconds for a locked database before failing.
|
||||
//
|
||||
// See discussion: https://github.com/mattn/go-sqlite3/issues/1022#issuecomment-1067353980
|
||||
|
||||
dsn := "file:" + dbFilePath + "?_journal_mode=wal&_txlock=immediate&_busy_timeout=5000"
|
||||
|
||||
sqliteDB, err := sql.Open("sqlite3", dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sqliteDB.SetMaxOpenConns(1)
|
||||
sqliteDB.SetConnMaxLifetime(time.Hour)
|
||||
sqliteDB.SetMaxIdleConns(1)
|
||||
|
||||
return gorm.Open(sqlite.Dialector{Conn: sqliteDB}, &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -5,9 +5,6 @@ import (
|
|||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gravitl/netmaker/db"
|
||||
"github.com/gravitl/netmaker/logic/acls"
|
||||
"github.com/gravitl/netmaker/schema"
|
||||
"net"
|
||||
"reflect"
|
||||
"sort"
|
||||
|
@ -15,6 +12,10 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gravitl/netmaker/db"
|
||||
"github.com/gravitl/netmaker/logic/acls"
|
||||
"github.com/gravitl/netmaker/schema"
|
||||
|
||||
"github.com/goombaio/namegenerator"
|
||||
"github.com/gravitl/netmaker/database"
|
||||
"github.com/gravitl/netmaker/logger"
|
||||
|
@ -384,6 +385,10 @@ func SaveExtClient(extclient *models.ExtClient) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if servercfg.CacheEnabled() {
|
||||
storeExtClientInCache(key, *extclient)
|
||||
}
|
||||
|
||||
return SetNetworkNodesLastModified(extclient.Network)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue