Actually honour max conns globally, not per storage struct

This commit is contained in:
Kegan Dougal 2023-07-12 17:36:59 +01:00
parent 37dcd91e09
commit c47665f1e8
3 changed files with 25 additions and 13 deletions

View File

@ -57,6 +57,10 @@ func NewStorage(postgresURI string) *Storage {
// TODO: if we panic(), will sentry have a chance to flush the event? // TODO: if we panic(), will sentry have a chance to flush the event?
logger.Panic().Err(err).Str("uri", postgresURI).Msg("failed to open SQL DB") logger.Panic().Err(err).Str("uri", postgresURI).Msg("failed to open SQL DB")
} }
return NewStorageWithDB(db)
}
func NewStorageWithDB(db *sqlx.DB) *Storage {
acc := &Accumulator{ acc := &Accumulator{
db: db, db: db,
roomsTable: NewRoomsTable(db), roomsTable: NewRoomsTable(db),

View File

@ -26,6 +26,10 @@ func NewStore(postgresURI, secret string) *Storage {
// TODO: if we panic(), will sentry have a chance to flush the event? // TODO: if we panic(), will sentry have a chance to flush the event?
logger.Panic().Err(err).Str("uri", postgresURI).Msg("failed to open SQL DB") logger.Panic().Err(err).Str("uri", postgresURI).Msg("failed to open SQL DB")
} }
return NewStoreWithDB(db, secret)
}
func NewStoreWithDB(db *sqlx.DB, secret string) *Storage {
return &Storage{ return &Storage{
DevicesTable: NewDevicesTable(db), DevicesTable: NewDevicesTable(db),
TokensTable: NewTokensTable(db, secret), TokensTable: NewTokensTable(db, secret),

30
v3.go
View File

@ -77,20 +77,24 @@ func Setup(destHomeserver, postgresURI, secret string, opts Opts) (*handler2.Han
}, },
DestinationServer: destHomeserver, DestinationServer: destHomeserver,
} }
store := state.NewStorage(postgresURI) db, err := sqlx.Open("postgres", postgresURI)
storev2 := sync2.NewStore(postgresURI, secret) if err != nil {
for _, db := range []*sqlx.DB{store.DB, storev2.DB} { sentry.CaptureException(err)
if opts.DBMaxConns > 0 { // TODO: if we panic(), will sentry have a chance to flush the event?
// https://github.com/go-sql-driver/mysql#important-settings logger.Panic().Err(err).Str("uri", postgresURI).Msg("failed to open SQL DB")
// "db.SetMaxIdleConns() is recommended to be set same to db.SetMaxOpenConns(). When it is smaller
// than SetMaxOpenConns(), connections can be opened and closed much more frequently than you expect."
db.SetMaxOpenConns(opts.DBMaxConns)
db.SetMaxIdleConns(opts.DBMaxConns)
}
if opts.DBConnMaxIdleTime > 0 {
db.SetConnMaxIdleTime(opts.DBConnMaxIdleTime)
}
} }
if opts.DBMaxConns > 0 {
// https://github.com/go-sql-driver/mysql#important-settings
// "db.SetMaxIdleConns() is recommended to be set same to db.SetMaxOpenConns(). When it is smaller
// than SetMaxOpenConns(), connections can be opened and closed much more frequently than you expect."
db.SetMaxOpenConns(opts.DBMaxConns)
db.SetMaxIdleConns(opts.DBMaxConns)
}
if opts.DBConnMaxIdleTime > 0 {
db.SetConnMaxIdleTime(opts.DBConnMaxIdleTime)
}
store := state.NewStorageWithDB(db)
storev2 := sync2.NewStoreWithDB(db, secret)
bufferSize := 50 bufferSize := 50
deviceDataUpdateFrequency := time.Second deviceDataUpdateFrequency := time.Second
if opts.TestingSynchronousPubsub { if opts.TestingSynchronousPubsub {