Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: config fetcher called twice #305

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions router/core/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,10 +550,6 @@ func (r *Router) Start(ctx context.Context) error {
select {
case <-ctx.Done(): // context cancelled
return nil
case cfg := <-initCh: // initial config
if err := r.updateServer(ctx, cfg); err != nil {
return fmt.Errorf("failed to start server with initial config: %w", err)
}
case cfg := <-r.configFetcher.Subscribe(ctx): // new config
if err := r.updateServer(ctx, cfg); err != nil {
r.logger.Error("Failed to start server with new config", zap.Error(err))
Expand All @@ -563,6 +559,15 @@ func (r *Router) Start(ctx context.Context) error {
}
})

eg.Go(func() error {
cfg := <-initCh
if err := r.updateServer(ctx, cfg); err != nil {
return fmt.Errorf("failed to start server with initial config: %w", err)
}

return nil
})

// Poll control-plane to get initial router config
initialCfg, err := r.configFetcher.GetRouterConfig(ctx)
if err != nil {
Expand Down
17 changes: 10 additions & 7 deletions router/internal/controlplane/configfetcher.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package controlplane

import (
"connectrpc.com/connect"
"context"
"fmt"

"connectrpc.com/connect"

"net/http"
"sync"
"time"

"github.com/wundergraph/cosmo/router/gen/proto/wg/cosmo/common"
nodev1 "github.com/wundergraph/cosmo/router/gen/proto/wg/cosmo/node/v1"
"github.com/wundergraph/cosmo/router/gen/proto/wg/cosmo/node/v1/nodev1connect"
"go.uber.org/zap"
"net/http"
"sync"
"time"
)

type Option func(cp *client)
Expand Down Expand Up @@ -77,12 +80,12 @@ func (c *client) Subscribe(ctx context.Context) chan *nodev1.RouterConfig {

cfg, err := c.getRouterConfigFromCP(ctx, &c.latestRouterVersion)
if err != nil {
c.logger.Error("Could not get latest router config, trying again in 10 seconds", zap.Error(err))
c.logger.Error("Could not get latest router config, trying again in "+c.pollInterval.String(), zap.Error(err))
continue
}

if cfg == nil {
c.logger.Debug("No new router config available, received nil router config, trying again in 10 seconds")
c.logger.Debug("No new router config available, received nil router config, trying again in " + c.pollInterval.String())
continue
}

Expand All @@ -93,7 +96,7 @@ func (c *client) Subscribe(ctx context.Context) chan *nodev1.RouterConfig {
c.mu.Unlock()

if newVersion == latestVersion {
c.logger.Debug("No new router config available, trying again in 10 seconds")
c.logger.Debug("No new router config available, trying again in " + c.pollInterval.String())
continue
}

Expand Down
Loading