client: add fuzzy timer for update check and ping interval

Uses the same timing parameters as update_engine.
This commit is contained in:
Michael Marineau 2017-06-02 18:38:56 -07:00
parent c88c5916bb
commit 1b026dfef5

View file

@ -19,6 +19,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/url" "net/url"
"time"
"github.com/satori/go.uuid" "github.com/satori/go.uuid"
@ -27,6 +28,11 @@ import (
const ( const (
defaultClientVersion = "go-omaha" defaultClientVersion = "go-omaha"
// periodic update check and ping intervals
pingFuzz = 10 * time.Minute
pingDelay = 7 * time.Minute // first check after 2-12 minutes
pingInterval = 45 * time.Minute // check in every 40-50 minutes
) )
// Client supports managing multiple apps using a single server. // Client supports managing multiple apps using a single server.
@ -37,6 +43,7 @@ type Client struct {
userID string userID string
sessionID string sessionID string
isMachine bool isMachine bool
sentPing bool
apps map[string]*AppClient apps map[string]*AppClient
} }
@ -97,6 +104,16 @@ func (c *Client) SetClientVersion(clientVersion string) {
c.clientVersion = clientVersion c.clientVersion = clientVersion
} }
// NextPing returns a timer channel that will fire when the next update
// check or ping should be sent.
func (c *Client) NextPing() <-chan time.Time {
d := pingDelay
if c.sentPing {
d = pingInterval
}
return FuzzyAfter(d, pingFuzz)
}
// AppClient gets the application client for the given application ID. // AppClient gets the application client for the given application ID.
func (c *Client) AppClient(appID string) (*AppClient, error) { func (c *Client) AppClient(appID string) (*AppClient, error) {
if app, ok := c.apps[appID]; ok { if app, ok := c.apps[appID]; ok {
@ -167,6 +184,8 @@ func (ac *AppClient) UpdateCheck() (*omaha.UpdateResponse, error) {
app.AddPing() app.AddPing()
app.AddUpdateCheck() app.AddUpdateCheck()
ac.sentPing = true
appResp, err := ac.doReq(ac.apiEndpoint, req) appResp, err := ac.doReq(ac.apiEndpoint, req)
if err != nil { if err != nil {
return nil, err return nil, err
@ -196,6 +215,8 @@ func (ac *AppClient) Ping() error {
app := req.Apps[0] app := req.Apps[0]
app.AddPing() app.AddPing()
ac.sentPing = true
appResp, err := ac.doReq(ac.apiEndpoint, req) appResp, err := ac.doReq(ac.apiEndpoint, req)
if err != nil { if err != nil {
return err return err