1
0
Преглед на файлове

Merge pull request #3 from skx/2-timeout-config

Allow user to specify a timeout.
Steve Kemp преди 5 години
родител
ревизия
5500c4872a
променени са 1 файла, в които са добавени 9 реда и са изтрити 1 реда
  1. 9 1
      rss2hook.go

+ 9 - 1
rss2hook.go

@@ -41,6 +41,10 @@ type RSSEntry struct {
 // configuration file
 var Loaded []RSSEntry
 
+// Timeout is the (global) timeout we use when loading remote RSS
+// feeds.
+var Timeout time.Duration
+
 // loadConfig loads the named configuration file and populates our
 // `Loaded` list of RSS-feeds & Webhook addresses
 func loadConfig(filename string) {
@@ -94,7 +98,7 @@ func loadConfig(filename string) {
 func fetchFeed(url string) (string, error) {
 
 	// Ensure we setup a timeout for our fetch
-	client := &http.Client{Timeout: time.Duration(5 * time.Second)}
+	client := &http.Client{Timeout: Timeout}
 
 	// We'll only make a GET request
 	req, err := http.NewRequest("GET", url, nil)
@@ -254,8 +258,12 @@ func main() {
 
 	// Parse the command-line flags
 	config := flag.String("config", "", "The path to the configuration-file to read")
+	timeout := flag.Duration("timeout", 5*time.Second, "The timeout used for fetching the remote feeds")
 	flag.Parse()
 
+	// Setup the default timeout.
+	Timeout = *timeout
+
 	if *config == "" {
 		fmt.Printf("Please specify a configuration-file to read\n")
 		return