Skip to content

Commit

Permalink
Actually honour max conns globally, not per storage struct
Browse files Browse the repository at this point in the history
  • Loading branch information
kegsay committed Jul 12, 2023
1 parent 37dcd91 commit c47665f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
4 changes: 4 additions & 0 deletions state/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func NewStorage(postgresURI string) *Storage {
// 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")
}
return NewStorageWithDB(db)
}

func NewStorageWithDB(db *sqlx.DB) *Storage {
acc := &Accumulator{
db: db,
roomsTable: NewRoomsTable(db),
Expand Down
4 changes: 4 additions & 0 deletions sync2/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func NewStore(postgresURI, secret string) *Storage {
// 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")
}
return NewStoreWithDB(db, secret)
}

func NewStoreWithDB(db *sqlx.DB, secret string) *Storage {
return &Storage{
DevicesTable: NewDevicesTable(db),
TokensTable: NewTokensTable(db, secret),
Expand Down
30 changes: 17 additions & 13 deletions v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,24 @@ func Setup(destHomeserver, postgresURI, secret string, opts Opts) (*handler2.Han
},
DestinationServer: destHomeserver,
}
store := state.NewStorage(postgresURI)
storev2 := sync2.NewStore(postgresURI, secret)
for _, db := range []*sqlx.DB{store.DB, storev2.DB} {
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)
}
db, err := sqlx.Open("postgres", postgresURI)
if err != nil {
sentry.CaptureException(err)
// 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")
}
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
deviceDataUpdateFrequency := time.Second
if opts.TestingSynchronousPubsub {
Expand Down

0 comments on commit c47665f

Please sign in to comment.