diff --git a/client.go b/client.go index 2f36548..390b76c 100644 --- a/client.go +++ b/client.go @@ -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 } } diff --git a/cmd/boringproxy/main.go b/cmd/boringproxy/main.go index 6d9148a..9c60515 100644 --- a/cmd/boringproxy/main.go +++ b/cmd/boringproxy/main.go @@ -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()