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

Try to establish pusher connection ASAP #1121

Merged
merged 1 commit into from
Dec 23, 2024
Merged
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
14 changes: 7 additions & 7 deletions .github/workflows/dance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ jobs:
tags: tag:ci
version: latest

- name: Start pinging Tailscale
if: github.event_name != 'pull_request'
run: |
mkdir -p /tmp/logs
tailscale ping -c 0 --until-direct --timeout 2s --verbose ${{ secrets.DANCE_PUSH_HOST }} 2>&1 > /tmp/logs/tailscale.txt &

- name: Checkout code
uses: actions/checkout@v4
with:
Expand All @@ -97,20 +103,14 @@ jobs:
- name: Run init
run: bin/task init build

- name: Wait for Tailscale to be up
if: github.event_name != 'pull_request'
run: tailscale ping -c 0 --until-direct --timeout 1s --verbose ${{ secrets.DANCE_PUSH_HOST }}

- name: Dance!
run: bin/task dance CONFIG=${{ matrix.project }}.yml
env:
DANCE_PUSH: ${{ secrets.DANCE_PUSH }}

- name: Collect logs
if: failure()
run: |
mkdir -p /tmp/logs
bin/task env-logs-collect > /tmp/logs/compose.txt
run: bin/task env-logs-collect > /tmp/logs/compose.txt

- name: Compress logs before upload
if: failure()
Expand Down
2 changes: 0 additions & 2 deletions .golangci-new.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ linters:
- err113
- errchkjson
- errname
- execinquery
- exhaustruct
- exportloopref
- fatcontext
Expand All @@ -112,7 +111,6 @@ linters:
- gocyclo
- gofmt
- gofumpt
- gomnd
- gomoddirectives
- goprintffuncname
- gosec
Expand Down
22 changes: 11 additions & 11 deletions cmd/dance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ func main() {
stop()
}()

var pusherClient *pusher.Client

if cli.Push != "" {
var err error
if pusherClient, err = pusher.New(cli.Push, l.With(slog.String("name", "pusher"))); err != nil {
log.Fatal(err)
}

defer pusherClient.Close()
}

if len(cli.Database) == 0 {
cli.Database = maps.Keys(configload.DBs)
slices.Sort(cli.Database)
Expand All @@ -152,17 +163,6 @@ func main() {
}
}

var pusherClient *pusher.Client

if cli.Push != "" {
var err error
if pusherClient, err = pusher.New(cli.Push, l.With(slog.String("name", "pusher"))); err != nil {
log.Fatal(err)
}

defer pusherClient.Close()
}

if len(cli.Config) == 0 {
var err error
if cli.Config, err = filepath.Glob("*.yml"); err != nil {
Expand Down
85 changes: 61 additions & 24 deletions internal/pusher/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ import (

// Client represents a MongoDB client.
type Client struct {
l *slog.Logger
c *mongo.Client
database string
hostname string
runner string
repository string
l *slog.Logger
c *mongo.Client
pingerCancel context.CancelFunc
pingerDone chan struct{}
database string
hostname string
runner string
repository string
}

// New creates a new MongoDB client with given URI.
Expand All @@ -53,34 +55,64 @@ func New(uri string, l *slog.Logger) (*Client, error) {
return nil, fmt.Errorf("database name is empty in the URL")
}

ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
hostname, err := os.Hostname()
if err != nil {
return nil, err
}

// We ignore many URI connection parameters and start pinger as soon as possible
// because it takes a long time on CI to establish the Tailscale connection.

ctx := context.Background()

opts := options.Client().ApplyURI(uri)
opts.SetDirect(true)
opts.SetConnectTimeout(3 * time.Second)
opts.SetHeartbeatInterval(3 * time.Second)
opts.SetMaxConnIdleTime(0)
opts.SetMinPoolSize(1)
opts.SetMaxPoolSize(1)
opts.SetMaxConnecting(1)

l.InfoContext(ctx, "Connecting to MongoDB URI to push results...", slog.String("uri", u.Redacted()))

c, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
c, err := mongo.Connect(ctx, opts)
if err != nil {
return nil, err
}

if err = c.Ping(ctx, nil); err != nil {
c.Disconnect(ctx)
return nil, err
pingerCtx, pingerCancel := context.WithCancel(ctx)

res := &Client{
l: l,
c: c,
pingerCancel: pingerCancel,
pingerDone: make(chan struct{}),
database: database,
hostname: hostname,
runner: os.Getenv("RUNNER_NAME"),
repository: os.Getenv("GITHUB_REPOSITORY"),
}

hostname, err := os.Hostname()
if err != nil {
return nil, err
go res.runPinger(pingerCtx)

return res, nil
}

func (c *Client) runPinger(ctx context.Context) {
for ctx.Err() == nil {
pingCtx, pingCancel := context.WithTimeout(ctx, 5*time.Second)

if err := c.c.Ping(pingCtx, nil); err != nil {
c.l.WarnContext(pingCtx, "Ping failed", slog.String("error", err.Error()))
}

// always wait, even if ping returns immediately
<-pingCtx.Done()
pingCancel()
}

return &Client{
l: l,
c: c,
database: database,
hostname: hostname,
runner: os.Getenv("RUNNER_NAME"),
repository: os.Getenv("GITHUB_REPOSITORY"),
}, nil
close(c.pingerDone)
}

// Push pushes test results to MongoDB-compatible database.
Expand Down Expand Up @@ -113,8 +145,13 @@ func (c *Client) Push(ctx context.Context, config, database string, res map[stri

// Close closes all connections.
func (c *Client) Close() {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
c.pingerCancel()
<-c.pingerDone

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

c.c.Disconnect(ctx)

c.c = nil
}
Loading