| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | package reload | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 	"math/rand" | 
					
						
							|  |  |  | 	"sync" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin" | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | 	clog "github.com/coredns/coredns/plugin/pkg/log" | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 09:04:47 +08:00
										 |  |  | 	"github.com/caddyserver/caddy" | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-22 21:40:33 +01:00
										 |  |  | var log = clog.NewWithPlugin("reload") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | func init() { | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("reload", caddy.Plugin{ | 
					
						
							|  |  |  | 		ServerType: "dns", | 
					
						
							|  |  |  | 		Action:     setup, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | // the info reload is global to all application, whatever number of reloads. | 
					
						
							|  |  |  | // it is used to transmit data between Setup and start of the hook called 'onInstanceStartup' | 
					
						
							|  |  |  | // channel for QUIT is never changed in purpose. | 
					
						
							|  |  |  | // WARNING: this data may be unsync after an invalid attempt of reload Corefile. | 
					
						
							| 
									
										
										
										
											2019-02-17 14:57:36 +00:00
										 |  |  | var ( | 
					
						
							|  |  |  | 	r              = reload{dur: defaultInterval, u: unused, quit: make(chan bool)} | 
					
						
							|  |  |  | 	once, shutOnce sync.Once | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error { | 
					
						
							|  |  |  | 	c.Next() // 'reload' | 
					
						
							|  |  |  | 	args := c.RemainingArgs() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(args) > 2 { | 
					
						
							|  |  |  | 		return plugin.Error("reload", c.ArgErr()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	i := defaultInterval | 
					
						
							|  |  |  | 	if len(args) > 0 { | 
					
						
							|  |  |  | 		d, err := time.ParseDuration(args[0]) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 			return plugin.Error("reload", err) | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		i = d | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 	if i < minInterval { | 
					
						
							|  |  |  | 		return plugin.Error("reload", fmt.Errorf("interval value must be greater or equal to %v", minInterval)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	j := defaultJitter | 
					
						
							|  |  |  | 	if len(args) > 1 { | 
					
						
							|  |  |  | 		d, err := time.ParseDuration(args[1]) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 			return plugin.Error("reload", err) | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		j = d | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 	if j < minJitter { | 
					
						
							|  |  |  | 		return plugin.Error("reload", fmt.Errorf("jitter value must be greater or equal to %v", minJitter)) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if j > i/2 { | 
					
						
							|  |  |  | 		j = i / 2 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	jitter := time.Duration(rand.Int63n(j.Nanoseconds()) - (j.Nanoseconds() / 2)) | 
					
						
							|  |  |  | 	i = i + jitter | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 	// prepare info for next onInstanceStartup event | 
					
						
							| 
									
										
										
										
											2019-02-17 14:57:36 +00:00
										 |  |  | 	r.setInterval(i) | 
					
						
							|  |  |  | 	r.setUsage(used) | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 	once.Do(func() { | 
					
						
							|  |  |  | 		caddy.RegisterEventHook("reload", hook) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 	// re-register on finalShutDown as the instance most-likely will be changed | 
					
						
							| 
									
										
										
										
											2018-02-08 13:57:49 -05:00
										 |  |  | 	shutOnce.Do(func() { | 
					
						
							|  |  |  | 		c.OnFinalShutdown(func() error { | 
					
						
							|  |  |  | 			r.quit <- true | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2018-02-02 13:15:56 -05:00
										 |  |  | 	minJitter       = 1 * time.Second | 
					
						
							|  |  |  | 	minInterval     = 2 * time.Second | 
					
						
							| 
									
										
										
										
											2018-01-27 05:42:57 -05:00
										 |  |  | 	defaultInterval = 30 * time.Second | 
					
						
							|  |  |  | 	defaultJitter   = 15 * time.Second | 
					
						
							|  |  |  | ) |