-
Notifications
You must be signed in to change notification settings - Fork 5
/
ping.go
52 lines (47 loc) · 1.02 KB
/
ping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package sitemap
import (
"fmt"
"log"
"net/http"
"sync"
)
//Sends a ping to search engines indicating that the index has been updated.
//Currently supports Google and Bing.
func PingSearchEngines(indexFile string) {
var urls = []string{
fmt.Sprintf("http://www.google.com/ping?sitemap=%s", indexFile),
fmt.Sprintf("http://www.bing.com/ping?sitemap=%s", indexFile),
}
results := asyncHttpGets(urls)
for result := range results {
log.Printf("%s status: %s\n", result.url, result.response.Status)
}
}
type HttpResponse struct {
url string
response *http.Response
err error
}
func asyncHttpGets(urls []string) chan HttpResponse {
ch := make(chan HttpResponse)
go func() {
var wg sync.WaitGroup
for _, url := range urls {
wg.Add(1)
go func(url string) {
resp, err := http.Get(url)
if err != nil {
log.Println("error", resp, err)
wg.Done()
return
}
resp.Body.Close()
ch <- HttpResponse{url, resp, err}
wg.Done()
}(url)
}
wg.Wait()
close(ch)
}()
return ch
}