mirror of
https://github.com/boringproxy/boringproxy.git
synced 2025-02-25 18:55:29 -06:00
Implement configurable polling interval for client
This commit is contained in:
parent
79c3c60c14
commit
cf4ce028a7
19
client.go
19
client.go
@ -29,6 +29,7 @@ type Client struct {
|
|||||||
cancelFuncsMutex *sync.Mutex
|
cancelFuncsMutex *sync.Mutex
|
||||||
certConfig *certmagic.Config
|
certConfig *certmagic.Config
|
||||||
behindProxy bool
|
behindProxy bool
|
||||||
|
pollInterval int
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientConfig struct {
|
type ClientConfig struct {
|
||||||
@ -42,6 +43,7 @@ type ClientConfig struct {
|
|||||||
AcmeCa string `json:"acmeCa,omitempty"`
|
AcmeCa string `json:"acmeCa,omitempty"`
|
||||||
DnsServer string `json:"dnsServer,omitempty"`
|
DnsServer string `json:"dnsServer,omitempty"`
|
||||||
BehindProxy bool `json:"behindProxy,omitempty"`
|
BehindProxy bool `json:"behindProxy,omitempty"`
|
||||||
|
PollInterval int `json:"pollInterval,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(config *ClientConfig) (*Client, error) {
|
func NewClient(config *ClientConfig) (*Client, error) {
|
||||||
@ -113,6 +115,7 @@ func NewClient(config *ClientConfig) (*Client, error) {
|
|||||||
cancelFuncsMutex: cancelFuncsMutex,
|
cancelFuncsMutex: cancelFuncsMutex,
|
||||||
certConfig: certConfig,
|
certConfig: certConfig,
|
||||||
behindProxy: config.BehindProxy,
|
behindProxy: config.BehindProxy,
|
||||||
|
pollInterval: config.PollInterval,
|
||||||
}, nil
|
}, 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)
|
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 {
|
for {
|
||||||
err := c.PollTunnels(ctx)
|
err := c.PollTunnels(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -155,7 +171,8 @@ func (c *Client) Run(ctx context.Context) error {
|
|||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return nil
|
return nil
|
||||||
case <-time.After(2 * time.Second):
|
case <-pollChan:
|
||||||
|
fmt.Println("poll")
|
||||||
// continue
|
// continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -96,6 +96,7 @@ func main() {
|
|||||||
acmeCa := flagSet.String("acme-certificate-authority", "", "URI for ACME Certificate Authority")
|
acmeCa := flagSet.String("acme-certificate-authority", "", "URI for ACME Certificate Authority")
|
||||||
dnsServer := flagSet.String("dns-server", "", "Custom DNS server")
|
dnsServer := flagSet.String("dns-server", "", "Custom DNS server")
|
||||||
behindProxy := flagSet.Bool("behind-proxy", false, "Whether we're running behind another reverse proxy")
|
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:])
|
err := flagSet.Parse(os.Args[2:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -110,6 +111,11 @@ func main() {
|
|||||||
fail("-token is required")
|
fail("-token is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
minPollInterval := 100
|
||||||
|
if *pollInterval < minPollInterval {
|
||||||
|
fail(fmt.Sprintf("-poll-interval-ms must be at least %d", minPollInterval))
|
||||||
|
}
|
||||||
|
|
||||||
config := &boringproxy.ClientConfig{
|
config := &boringproxy.ClientConfig{
|
||||||
ServerAddr: *server,
|
ServerAddr: *server,
|
||||||
Token: *token,
|
Token: *token,
|
||||||
@ -121,6 +127,7 @@ func main() {
|
|||||||
AcmeCa: *acmeCa,
|
AcmeCa: *acmeCa,
|
||||||
DnsServer: *dnsServer,
|
DnsServer: *dnsServer,
|
||||||
BehindProxy: *behindProxy,
|
BehindProxy: *behindProxy,
|
||||||
|
PollInterval: *pollInterval,
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
Loading…
Reference in New Issue
Block a user