Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager handles the reload mechanism. The will be listening to the trigger of any of the notifiers, when this process is triggered it will call to all the reloaders based on the priority groups.
func (*Manager) Add ¶
Add a reloader to the manager.
The reloader will be called when any of the notifiers end the execution.
When adding a reloader, the reloader will have a priority. All the reloaders with the same priority will be batched and executed in parallel. When the reloaders batch executing ends, if there is not error, it will execute the next priority batch. This pricess will continue until all priority batches have been executed.
The priority order is ascendant (e.g 0, 42, 100, 250, 999...).
func (*Manager) On ¶
On registers a notifier that will execute all reloaders when any of the notifiers returns.
The notifier should stay waiting until the reload needs to take place. The notifier should be able to be called multiple times.
When a notifier ends its execution triggering the reload process the notifier will start again and keep waiting along with the ones already waiting.
This process will be repeated forever until the manager stops.
func (*Manager) Run ¶
Run will start the manager. This starts all the notifiers and wait until any of them returns a result, then it will call the notifiers in priority batches. All the triggered notifiers will start again.
If any of the notifiers returns an error, the execution will end with an error.
If the context is cancelled, the manager Run will end without error. If any of the reloaders reload process ends with an error, run will end its execution and return an error.
type NotifierChan ¶
type NotifierChan <-chan string
NotifierChan is a helper to create notifiers from channels.
Note: Closing the channel is not safe, as the channel will be reused and read from it multiple times for each notification.
type NotifierFunc ¶
NotifierFunc is a helper to create notifiers from functions.
type Reloader ¶
Reloader knows how to reload a resource.
Example (Basic) ¶
package main import ( "context" "fmt" "time" "github.com/slok/reload" ) func main() { reloadSvc := reload.NewManager() // Add reloader. reloadSvc.Add(0, reload.ReloaderFunc(func(ctx context.Context, id string) error { fmt.Printf("Reloader 1: %s\n", id) return nil })) // Run ticker and add as a reload notifier. { t := time.NewTicker(100 * time.Millisecond) defer t.Stop() reloadSvc.On(reload.NotifierFunc(func(ctx context.Context) (string, error) { <-t.C return "ticker", nil })) } ctx, cancel := context.WithTimeout(context.Background(), 550*time.Millisecond) defer cancel() _ = reloadSvc.Run(ctx) }
Output: Reloader 1: ticker Reloader 1: ticker Reloader 1: ticker Reloader 1: ticker Reloader 1: ticker
Example (BasicHTTP) ¶
package main import ( "context" "fmt" "net/http" "net/http/httptest" "time" "github.com/slok/reload" ) func main() { reloadSvc := reload.NewManager() // Add reloader. reloadSvc.Add(0, reload.ReloaderFunc(func(ctx context.Context, id string) error { fmt.Printf("Reloader 1: %s\n", id) return nil })) // Run http server and add as a reload notifier. serverURL := "" { reloadC := make(chan string) h := http.NewServeMux() h.Handle("/-/reload", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { fmt.Println("Triggering HTTP reload...") reloadC <- "http" })) server := httptest.NewServer(h) defer server.Close() serverURL = server.URL reloadSvc.On(reload.NotifierChan(reloadC)) } ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) defer cancel() // Make a reload HTTP request. go func() { time.Sleep(100 * time.Millisecond) _, _ = http.Get(serverURL + "/-/reload") }() _ = reloadSvc.Run(ctx) }
Output: Triggering HTTP reload... Reloader 1: http
Example (Multinotifier) ¶
package main import ( "context" "fmt" "time" "github.com/slok/reload" ) func main() { reloadSvc := reload.NewManager() // Add reloaders. reloadSvc.Add(0, reload.ReloaderFunc(func(ctx context.Context, id string) error { fmt.Printf("Reloader 1: %s\n", id) return nil })) // Run ticker 1 and add as a reload notifier. { t := time.NewTicker(150 * time.Millisecond) defer t.Stop() reloadSvc.On(reload.NotifierFunc(func(ctx context.Context) (string, error) { <-t.C return "ticker1", nil })) } // Run ticker 2 and add as a reload notifier. { t := time.NewTicker(80 * time.Millisecond) defer t.Stop() reloadSvc.On(reload.NotifierFunc(func(ctx context.Context) (string, error) { <-t.C return "ticker2", nil })) } ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) defer cancel() _ = reloadSvc.Run(ctx) }
Output: Reloader 1: ticker2 Reloader 1: ticker1 Reloader 1: ticker2 Reloader 1: ticker2 Reloader 1: ticker1 Reloader 1: ticker2 Reloader 1: ticker2 Reloader 1: ticker1 Reloader 1: ticker2
Example (Priority) ¶
package main import ( "context" "fmt" "time" "github.com/slok/reload" ) func main() { reloadSvc := reload.NewManager() // Add reloaders with different reload priorities. reloadSvc.Add(10, reload.ReloaderFunc(func(ctx context.Context, id string) error { fmt.Printf("Reloader 2: %s\n", id) return nil })) reloadSvc.Add(0, reload.ReloaderFunc(func(ctx context.Context, id string) error { fmt.Printf("Reloader 1: %s\n", id) return nil })) reloadSvc.Add(20, reload.ReloaderFunc(func(ctx context.Context, id string) error { fmt.Printf("Reloader 3: %s\n", id) return nil })) // Run ticker and add as a reload notifier. { t := time.NewTicker(100 * time.Millisecond) defer t.Stop() reloadSvc.On(reload.NotifierFunc(func(ctx context.Context) (string, error) { <-t.C return "ticker", nil })) } ctx, cancel := context.WithTimeout(context.Background(), 550*time.Millisecond) defer cancel() _ = reloadSvc.Run(ctx) }
Output: Reloader 1: ticker Reloader 2: ticker Reloader 3: ticker Reloader 1: ticker Reloader 2: ticker Reloader 3: ticker Reloader 1: ticker Reloader 2: ticker Reloader 3: ticker Reloader 1: ticker Reloader 2: ticker Reloader 3: ticker Reloader 1: ticker Reloader 2: ticker Reloader 3: ticker