Skip to content

Commit

Permalink
Use utc for now timestamps, add pgx config
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim committed Jun 21, 2021
1 parent 4a8d7ad commit 266aa0c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
8 changes: 5 additions & 3 deletions cmd/spicedb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ func main() {
rootCmd.Flags().Duration("revision-fuzzing-duration", 5*time.Second, "amount of time to advertize stale revisions")
rootCmd.Flags().Duration("gc-window", 24*time.Hour, "amount of time before a revision is garbage collected")
rootCmd.Flags().Duration("ns-cache-expiration", 1*time.Minute, "amount of time a namespace entry should remain cached")
rootCmd.Flags().Int("pg-max-conn-open", 20, "number of concurrent connections open in the postgres connection pool")
rootCmd.Flags().Int("pg-max-conn-idle", 20, "number of idle connections open in the postgres connection pool")
rootCmd.Flags().Int("pg-max-conn-open", 20, "number of concurrent connections open in a the postgres connection pool")
rootCmd.Flags().Int("pg-min-conn-open", 10, "number of minimum concurrent connections open in a the postgres connection pool")
rootCmd.Flags().Duration("pg-max-conn-lifetime", 30*time.Minute, "maximum amount of time a connection can live in the postgres connection pool")
rootCmd.Flags().Duration("pg-max-conn-idletime", 30*time.Minute, "maximum amount of time a connection can idle in the postgres connection pool")
rootCmd.Flags().Duration("pg-health-check-period", 30*time.Second, "duration between checks of the health of idle connections")
rootCmd.Flags().Int("crdb-max-conn-open", 20, "number of concurrent connections open in the cockroachdb connection pool")
rootCmd.Flags().Int("crdb-min-conn-open", 20, "number of idle connections to keep open in the cockroachdb connection pool")
rootCmd.Flags().Duration("crdb-max-conn-lifetime", 30*time.Minute, "maximum amount of time a connection can live in the cockroachdb connection pool")
Expand Down Expand Up @@ -186,8 +187,9 @@ func rootRun(cmd *cobra.Command, args []string) {
datastoreUrl,
postgres.ConnMaxIdleTime(cobrautil.MustGetDuration(cmd, "pg-max-conn-idletime")),
postgres.ConnMaxLifetime(cobrautil.MustGetDuration(cmd, "pg-max-conn-lifetime")),
postgres.HealthCheckPeriod(cobrautil.MustGetDuration(cmd, "pg-health-check-period")),
postgres.MaxOpenConns(cobrautil.MustGetInt(cmd, "pg-max-conn-open")),
postgres.MaxIdleConns(cobrautil.MustGetInt(cmd, "pg-max-conn-idle")),
postgres.MinOpenConns(cobrautil.MustGetInt(cmd, "pg-min-conn-open")),
postgres.RevisionFuzzingTimedelta(revisionFuzzingTimedelta),
postgres.GCWindow(gcWindow),
postgres.EnablePrometheusStats(),
Expand Down
19 changes: 13 additions & 6 deletions internal/datastore/postgres/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
)

type postgresOptions struct {
connMaxIdleTime *time.Duration
connMaxLifetime *time.Duration
maxIdleConns *int
maxOpenConns *int
connMaxIdleTime *time.Duration
connMaxLifetime *time.Duration
healthCheckPeriod *time.Duration
maxOpenConns *int
minOpenConns *int

watchBufferLength uint16
revisionFuzzingTimedelta time.Duration
Expand Down Expand Up @@ -63,9 +64,9 @@ func ConnMaxLifetime(lifetime time.Duration) PostgresOption {
}
}

func MaxIdleConns(conns int) PostgresOption {
func HealthCheckPeriod(period time.Duration) PostgresOption {
return func(po *postgresOptions) {
po.maxIdleConns = &conns
po.healthCheckPeriod = &period
}
}

Expand All @@ -75,6 +76,12 @@ func MaxOpenConns(conns int) PostgresOption {
}
}

func MinOpenConns(conns int) PostgresOption {
return func(po *postgresOptions) {
po.minOpenConns = &conns
}
}

func WatchBufferLength(watchBufferLength uint16) PostgresOption {
return func(po *postgresOptions) {
po.watchBufferLength = watchBufferLength
Expand Down
43 changes: 26 additions & 17 deletions internal/datastore/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,42 @@ func NewPostgresDatastore(
return nil, fmt.Errorf(errUnableToInstantiate, err)
}

dbpool, err := pgxpool.Connect(context.Background(), url)
// config must be initialized by ParseConfig
pgxConfig, err := pgxpool.ParseConfig(url)
if err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}

if config.maxOpenConns != nil {
pgxConfig.MaxConns = int32(*config.maxOpenConns)
}
if config.minOpenConns != nil {
pgxConfig.MinConns = int32(*config.minOpenConns)
}
if config.connMaxIdleTime != nil {
pgxConfig.MaxConnIdleTime = *config.connMaxIdleTime
}
if config.connMaxLifetime != nil {
pgxConfig.MaxConnLifetime = *config.connMaxLifetime
}
if config.healthCheckPeriod != nil {
pgxConfig.HealthCheckPeriod = *config.healthCheckPeriod
}

dbpool, err := pgxpool.ConnectConfig(context.Background(), pgxConfig)
if err != nil {
return nil, fmt.Errorf(errUnableToInstantiate, err)
}

// TODO
if config.enablePrometheusStats {
// collector := sqlstats.NewStatsCollector("spicedb", db)
// TODO
// collector := sqlstats.NewStatsCollector("spicedb", dbpool)
// err := prometheus.Register(collector)
// if err != nil {
// return nil, fmt.Errorf(errUnableToInstantiate, err)
// }
}

// TODO update config to match pgx config
// if config.maxOpenConns != nil {
// db.SetMaxOpenConns(*config.maxOpenConns)
// }
// if config.maxIdleConns != nil {
// db.SetMaxIdleConns(*config.maxIdleConns)
// }
// if config.connMaxIdleTime != nil {
// db.SetConnMaxIdleTime(*config.connMaxIdleTime)
// }
// if config.connMaxLifetime != nil {
// db.SetConnMaxLifetime(*config.connMaxLifetime)
// }

return &pgDatastore{
dbpool: dbpool,
watchBufferLength: config.watchBufferLength,
Expand Down Expand Up @@ -245,6 +253,7 @@ func (pgd *pgDatastore) computeRevisionRange(ctx context.Context, windowInverted
if err != nil {
return 0, 0, err
}
now = now.UTC()

span.AddEvent("DB returned value for NOW()")

Expand Down

0 comments on commit 266aa0c

Please sign in to comment.