Skip to content

Commit

Permalink
MapServer uses same timer to update and prune.
Browse files Browse the repository at this point in the history
  • Loading branch information
juagargi committed Aug 8, 2023
1 parent f4b3895 commit 3684520
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
10 changes: 5 additions & 5 deletions cmd/mapserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
)

type Config struct {
UpdateAt util.TimeOfDayWrap
UpdateTimer util.DurationWrap
CTLogServerURL string
DBConfig *db.Configuration

CTLogServerURL string
DBConfig *db.Configuration
CertificatePemFile string // A X509 pem certificate
PrivateKeyPemFile string // A RSA pem key

UpdateAt util.TimeOfDayWrap
UpdateTimer util.DurationWrap
}

func ReadConfigFromFile(filePath string) (*Config, error) {
Expand Down
13 changes: 7 additions & 6 deletions cmd/mapserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ func writeSampleConfig() error {
mysql.WithLocalSocket("/var/run/mysqld/mysqld.sock"),
)
config := &Config{
UpdateAt: util.NewTimeOfDay(3, 00, 00, 00),
UpdateTimer: util.DurationWrap{
Duration: 24 * time.Hour,
},
DBConfig: dbConfig,
CTLogServerURL: "https://ct.googleapis.com/logs/xenon2023/",
CertificatePemFile: "tests/testdata/servercert.pem",
PrivateKeyPemFile: "tests/testdata/serverkey.pem",

UpdateAt: util.NewTimeOfDay(3, 00, 00, 00),
UpdateTimer: util.DurationWrap{
Duration: 24 * time.Hour,
},
}

return WriteConfigurationToFile(flag.Arg(0), config)
Expand Down Expand Up @@ -102,7 +103,7 @@ func runWithConfig(

// Should update now?
if updateNow {
err := server.Update(ctx)
err := server.PruneAndUpdate(ctx)
if err != nil {
return fmt.Errorf("performing initial update: %w", err)
}
Expand All @@ -111,7 +112,7 @@ func runWithConfig(
// Set update cycle timer.
util.RunWhen(ctx, config.UpdateAt.NextTimeOfDay(), config.UpdateTimer.Duration,
func(ctx context.Context) {
err := server.Update(ctx)
err := server.PruneAndUpdate(ctx)
if err != nil {
fmt.Printf("ERROR: update returned %s\n", err)
}
Expand Down
36 changes: 25 additions & 11 deletions cmd/mapserver/mapserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func NewMapServer(ctx context.Context, config *Config) (*MapServer, error) {
for {
select {
case c := <-s.updateChan:
s.update(c)
s.pruneAndUpdate(c)
case <-ctx.Done():
// Requested to exit.
close(s.updateChan)
Expand All @@ -93,8 +93,14 @@ func NewMapServer(ctx context.Context, config *Config) (*MapServer, error) {
return s, nil
}

// Update triggers an update. If an ongoing update is still in process, it blocks.
func (s *MapServer) Update(ctx context.Context) error {
// Listen is responsible to start the listener for the responder.
func (s *MapServer) Listen(ctx context.Context) error {
<-ctx.Done()
return nil
}

// PruneAndUpdate triggers an update. If an ongoing update is still in process, it blocks.
func (s *MapServer) PruneAndUpdate(ctx context.Context) error {
// Signal we want an update.
s.updateChan <- ctx

Expand All @@ -103,15 +109,28 @@ func (s *MapServer) Update(ctx context.Context) error {
return err
}

func (s *MapServer) update(ctx context.Context) {
func (s *MapServer) pruneAndUpdate(ctx context.Context) {
s.prune(ctx)
s.update(ctx)
}

func (s *MapServer) prune(ctx context.Context) {
getTime := func() string {
return time.Now().UTC().Format(time.RFC3339)
}
fmt.Printf("======== update started at %s\n", getTime())
fmt.Printf("======== prune started at %s\n", getTime())
// deleteme TODO
fmt.Printf("======== prune finished at %s\n\n", getTime())
}

// time.Sleep(3 * time.Second)
func (s *MapServer) update(ctx context.Context) {
getTime := func() string {
return time.Now().UTC().Format(time.RFC3339)
}

fmt.Printf("======== update started at %s\n", getTime())
if err := s.Updater.StartFetchingRemaining(); err != nil {

s.updateErrChan <- fmt.Errorf("retrieving start and end indices: %w", err)
return
}
Expand All @@ -134,8 +153,3 @@ func (s *MapServer) update(ctx context.Context) {
// Queue answer in form of an error:
s.updateErrChan <- error(nil)
}

func (s *MapServer) Listen(ctx context.Context) error {
<-ctx.Done()
return nil
}

0 comments on commit 3684520

Please sign in to comment.