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" "context"
"flag" "flag"
"fmt" "fmt"
"github.com/cenkalti/backoff/v4"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
@ -32,8 +33,8 @@ import (
) )
const ( const (
defaulFailTimeout = time.Duration(3 * time.Second) defaultFailTimeout = 3 * time.Second
fastFailTimeout = time.Second failFastTimeout = time.Second
) )
func main() { func main() {
@ -188,7 +189,7 @@ func (s *serviceClient) getSettingsForm() *widget.Form {
s.preSharedKey = s.iPreSharedKey.Text s.preSharedKey = s.iPreSharedKey.Text
s.adminURL = s.iAdminURL.Text s.adminURL = s.iAdminURL.Text
client, err := s.getSrvClient(fastFailTimeout) client, err := s.getSrvClient(failFastTimeout)
if err != nil { if err != nil {
log.Errorf("get daemon client: %v", err) log.Errorf("get daemon client: %v", err)
return return
@ -219,7 +220,7 @@ func (s *serviceClient) getSettingsForm() *widget.Form {
} }
func (s *serviceClient) menuUpClick() error { func (s *serviceClient) menuUpClick() error {
conn, err := s.getSrvClient(defaulFailTimeout) conn, err := s.getSrvClient(defaultFailTimeout)
if err != nil { if err != nil {
log.Errorf("get client: %v", err) log.Errorf("get client: %v", err)
return err return err
@ -245,7 +246,7 @@ func (s *serviceClient) menuUpClick() error {
} }
func (s *serviceClient) menuDownClick() error { func (s *serviceClient) menuDownClick() error {
conn, err := s.getSrvClient(defaulFailTimeout) conn, err := s.getSrvClient(defaultFailTimeout)
if err != nil { if err != nil {
log.Errorf("get client: %v", err) log.Errorf("get client: %v", err)
return err return err
@ -270,30 +271,45 @@ func (s *serviceClient) menuDownClick() error {
return nil return nil
} }
func (s *serviceClient) updateStatus() { func (s *serviceClient) updateStatus() error {
conn, err := s.getSrvClient(defaulFailTimeout) conn, err := s.getSrvClient(defaultFailTimeout)
if err != nil { if err != nil {
log.Errorf("get client: %v", err) return err
return }
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{}) return nil
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()
}
} }
func (s *serviceClient) onTrayReady() { func (s *serviceClient) onTrayReady() {
@ -315,8 +331,11 @@ func (s *serviceClient) onTrayReady() {
go func() { go func() {
s.getSrvConfig() s.getSrvConfig()
for { for {
s.updateStatus() err := s.updateStatus()
time.Sleep(time.Second * 3) 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.managementURL = "https://api.wiretrustee.com:33073"
s.adminURL = "https://app.netbird.io" s.adminURL = "https://app.netbird.io"
conn, err := s.getSrvClient(fastFailTimeout) conn, err := s.getSrvClient(failFastTimeout)
if err != nil { if err != nil {
log.Errorf("get client: %v", err) log.Errorf("get client: %v", err)
return return