Make systray connected/disconnected icon switch faster (#299)

This commit is contained in:
Misha Bragin 2022-04-18 09:43:37 +02:00 committed by GitHub
parent 17fbbbea2a
commit 70ffc9d625
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
"github.com/cenkalti/backoff/v4"
"io/ioutil"
"os"
"os/exec"
@ -32,8 +33,8 @@ import (
)
const (
defaulFailTimeout = time.Duration(3 * time.Second)
fastFailTimeout = time.Second
defaultFailTimeout = 3 * time.Second
failFastTimeout = time.Second
)
func main() {
@ -188,7 +189,7 @@ func (s *serviceClient) getSettingsForm() *widget.Form {
s.preSharedKey = s.iPreSharedKey.Text
s.adminURL = s.iAdminURL.Text
client, err := s.getSrvClient(fastFailTimeout)
client, err := s.getSrvClient(failFastTimeout)
if err != nil {
log.Errorf("get daemon client: %v", err)
return
@ -219,7 +220,7 @@ func (s *serviceClient) getSettingsForm() *widget.Form {
}
func (s *serviceClient) menuUpClick() error {
conn, err := s.getSrvClient(defaulFailTimeout)
conn, err := s.getSrvClient(defaultFailTimeout)
if err != nil {
log.Errorf("get client: %v", err)
return err
@ -245,7 +246,7 @@ func (s *serviceClient) menuUpClick() error {
}
func (s *serviceClient) menuDownClick() error {
conn, err := s.getSrvClient(defaulFailTimeout)
conn, err := s.getSrvClient(defaultFailTimeout)
if err != nil {
log.Errorf("get client: %v", err)
return err
@ -270,30 +271,45 @@ func (s *serviceClient) menuDownClick() error {
return nil
}
func (s *serviceClient) updateStatus() {
conn, err := s.getSrvClient(defaulFailTimeout)
func (s *serviceClient) updateStatus() error {
conn, err := s.getSrvClient(defaultFailTimeout)
if err != nil {
log.Errorf("get client: %v", err)
return
return err
}
err = backoff.Retry(func() error {
status, err := conn.Status(s.ctx, &proto.StatusRequest{})
if err != nil {
log.Errorf("get service status: %v", err)
return err
}
if status.Status == string(internal.StatusConnected) {
systray.SetIcon(s.icConnected)
s.mStatus.SetTitle("Connected")
s.mUp.Disable()
s.mDown.Enable()
} else {
systray.SetIcon(s.icDisconnected)
s.mStatus.SetTitle("Disconnected")
s.mDown.Disable()
s.mUp.Enable()
}
return nil
}, &backoff.ExponentialBackOff{
InitialInterval: time.Second,
RandomizationFactor: backoff.DefaultRandomizationFactor,
Multiplier: backoff.DefaultMultiplier,
MaxInterval: 300 * time.Millisecond,
MaxElapsedTime: 2 * time.Second,
Stop: backoff.Stop,
Clock: backoff.SystemClock,
})
if err != nil {
return err
}
status, err := conn.Status(s.ctx, &proto.StatusRequest{})
if err != nil {
log.Errorf("get service status: %v", err)
return
}
if status.Status == string(internal.StatusConnected) {
systray.SetIcon(s.icConnected)
s.mStatus.SetTitle("Connected")
s.mUp.Disable()
s.mDown.Enable()
} else {
systray.SetIcon(s.icDisconnected)
s.mStatus.SetTitle("Disconnected")
s.mDown.Disable()
s.mUp.Enable()
}
return nil
}
func (s *serviceClient) onTrayReady() {
@ -315,8 +331,11 @@ func (s *serviceClient) onTrayReady() {
go func() {
s.getSrvConfig()
for {
s.updateStatus()
time.Sleep(time.Second * 3)
err := s.updateStatus()
if err != nil {
log.Errorf("error while updating status: %v", err)
}
time.Sleep(2 * time.Second)
}
}()
@ -400,7 +419,7 @@ func (s *serviceClient) getSrvConfig() {
s.managementURL = "https://api.wiretrustee.com:33073"
s.adminURL = "https://app.netbird.io"
conn, err := s.getSrvClient(fastFailTimeout)
conn, err := s.getSrvClient(failFastTimeout)
if err != nil {
log.Errorf("get client: %v", err)
return