Skip to content

Commit 6780894

Browse files
authored
feat: update check internet connectivity and run checks in parallel
1 parent f5804ec commit 6780894

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ require (
5252
go.bug.st/f v0.4.0
5353
go.bug.st/serial v1.6.4
5454
golang.org/x/crypto v0.41.0
55+
golang.org/x/sync v0.17.0
5556
golang.org/x/term v0.34.0
5657
golang.org/x/text v0.29.0
5758
)
@@ -272,7 +273,6 @@ require (
272273
golang.org/x/mod v0.27.0 // indirect
273274
golang.org/x/net v0.43.0 // indirect
274275
golang.org/x/oauth2 v0.30.0 // indirect
275-
golang.org/x/sync v0.17.0 // indirect
276276
golang.org/x/sys v0.36.0 // indirect
277277
golang.org/x/time v0.11.0 // indirect
278278
golang.org/x/tools v0.36.0 // indirect

internal/update/update.go

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import (
55
"errors"
66
"fmt"
77
"log/slog"
8+
"net/http"
89
"strings"
910
"sync"
11+
"time"
12+
13+
"golang.org/x/sync/errgroup"
1014
)
1115

1216
var ErrOperationAlreadyInProgress = errors.New("an operation is already in progress")
@@ -56,15 +60,42 @@ func (m *Manager) ListUpgradablePackages(ctx context.Context, matcher func(Upgra
5660
}
5761
defer m.lock.Unlock()
5862

59-
arduinoPkgs, err := m.arduinoPlatformUpdateService.ListUpgradablePackages(ctx, matcher)
60-
if err != nil {
61-
return nil, err
63+
// Make sure to be connected to the internet, before checking for updates.
64+
// This is needed because the checks below work also when offline (using cached data).
65+
if !isConnected() {
66+
return nil, errors.New("no internet connectivity")
6267
}
6368

64-
debPkgs, err := m.debUpdateService.ListUpgradablePackages(ctx, matcher)
65-
if err != nil {
69+
// Get the list of upgradable packages from two sources (deb and platform) in parallel.
70+
g, ctx := errgroup.WithContext(ctx)
71+
var (
72+
debPkgs []UpgradablePackage
73+
arduinoPkgs []UpgradablePackage
74+
)
75+
76+
g.Go(func() error {
77+
pkgs, err := m.debUpdateService.ListUpgradablePackages(ctx, matcher)
78+
if err != nil {
79+
return err
80+
}
81+
debPkgs = pkgs
82+
return nil
83+
})
84+
85+
g.Go(func() error {
86+
pkgs, err := m.arduinoPlatformUpdateService.ListUpgradablePackages(ctx, matcher)
87+
if err != nil {
88+
return err
89+
}
90+
arduinoPkgs = pkgs
91+
return nil
92+
})
93+
94+
// Wait for all the checks to complete (or any to fail).
95+
if err := g.Wait(); err != nil {
6696
return nil, err
6797
}
98+
6899
return append(arduinoPkgs, debPkgs...), nil
69100
}
70101

@@ -161,3 +192,19 @@ func (b *Manager) broadcast(event Event) {
161192
}
162193
}
163194
}
195+
196+
func isConnected() bool {
197+
client := http.Client{
198+
Timeout: 3 * time.Second,
199+
}
200+
201+
// Just check that the connection can be estabilished.
202+
// The HEAD method will not get the results and we are ignoring the HTTP status code.
203+
resp, err := client.Head("https://downloads.arduino.cc/")
204+
if err != nil {
205+
return false
206+
}
207+
defer resp.Body.Close()
208+
209+
return true
210+
}

0 commit comments

Comments
 (0)