Implement configurable polling interval for client

This commit is contained in:
Anders Pitman 2022-11-11 12:00:25 -07:00
parent 79c3c60c14
commit cf4ce028a7
2 changed files with 25 additions and 1 deletions

View File

@ -29,6 +29,7 @@ type Client struct {
cancelFuncsMutex *sync.Mutex
certConfig *certmagic.Config
behindProxy bool
pollInterval int
}
type ClientConfig struct {
@ -42,6 +43,7 @@ type ClientConfig struct {
AcmeCa string `json:"acmeCa,omitempty"`
DnsServer string `json:"dnsServer,omitempty"`
BehindProxy bool `json:"behindProxy,omitempty"`
PollInterval int `json:"pollInterval,omitempty"`
}
func NewClient(config *ClientConfig) (*Client, error) {
@ -113,6 +115,7 @@ func NewClient(config *ClientConfig) (*Client, error) {
cancelFuncsMutex: cancelFuncsMutex,
certConfig: certConfig,
behindProxy: config.BehindProxy,
pollInterval: config.PollInterval,
}, nil
}
@ -146,6 +149,19 @@ func (c *Client) Run(ctx context.Context) error {
return fmt.Errorf("Failed to create client. Are the user ('%s') and token correct? HTTP Status code: %d. Message: %s", c.user, resp.StatusCode, msg)
}
pollChan := make(chan struct{})
// A polling interval of 0 disables polling. Basically pollChan will
// remain blocked and never trigger in the select below.
if c.pollInterval > 0 {
go func() {
for {
<-time.After(time.Duration(c.pollInterval) * time.Millisecond)
pollChan <- struct{}{}
}
}()
}
for {
err := c.PollTunnels(ctx)
if err != nil {
@ -155,7 +171,8 @@ func (c *Client) Run(ctx context.Context) error {
select {
case <-ctx.Done():
return nil
case <-time.After(2 * time.Second):
case <-pollChan:
fmt.Println("poll")
// continue
}
}

View File

@ -96,6 +96,7 @@ func main() {
acmeCa := flagSet.String("acme-certificate-authority", "", "URI for ACME Certificate Authority")
dnsServer := flagSet.String("dns-server", "", "Custom DNS server")
behindProxy := flagSet.Bool("behind-proxy", false, "Whether we're running behind another reverse proxy")
pollInterval := flagSet.Int("poll-interval-ms", 2000, "Interval in milliseconds to poll for tunnel changes")
err := flagSet.Parse(os.Args[2:])
if err != nil {
@ -110,6 +111,11 @@ func main() {
fail("-token is required")
}
minPollInterval := 100
if *pollInterval < minPollInterval {
fail(fmt.Sprintf("-poll-interval-ms must be at least %d", minPollInterval))
}
config := &boringproxy.ClientConfig{
ServerAddr: *server,
Token: *token,
@ -121,6 +127,7 @@ func main() {
AcmeCa: *acmeCa,
DnsServer: *dnsServer,
BehindProxy: *behindProxy,
PollInterval: *pollInterval,
}
ctx := context.Background()