Home > other >  net/http and ants gives me different result every run
net/http and ants gives me different result every run

Time:11-05

I wrote a code to separate domains that give 200 responses and those that don't. Although I have 3447 different domains, I get different results after each run of the program.

Source where i found domains: https://github.com/opendns/public-domain-lists/blob/master/opendns-random-domains.txt

wc -l example.txt 3447

package main

import (
    "bufio"
    "log"
    "net/http"
    "os"
    "sync"
    "time"

    "github.com/panjf2000/ants"
)

var sitelist []string

func check(pool interface{}) {
    address := pool.(string)
    client := &http.Client{}
    client.Timeout = time.Second * 5
    req, _ := http.NewRequest("GET", "http://" address, nil)

    if response, err := client.Do(req); err == nil {
        status := response.StatusCode
        if status == 200 {
            // log.Println("Works:", address)
            sitelist = append(sitelist, "works:" address)
            return
        }

        // log.Println("Not working:", address)
        sitelist = append(sitelist, "not-works:" address)

    } else {
        // log.Println("Not working:", address)
        sitelist = append(sitelist, "not-works:" address)

    }

}

func main() {

    defer ants.Release()

    var wg sync.WaitGroup
    p, _ := ants.NewPoolWithFunc(1000, func(i interface{}) {
        check(i)
        wg.Done()
    })
    defer p.Release()

    file, err := os.Open("example.txt")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        wg.Add(1)
        a := scanner.Text()
        _ = p.Invoke(string(a))
    }

    wg.Wait()

    log.Println(len(sitelist))
}

go run ./main.go

3385

go run ./main.go

3338

go run ./main.go

3300

And here is example.txt

I can't understand what I'm doing wrong. thank you


I noticed that when I want to use map, I get different errors.

Code:

package main

import (
    "bufio"
    "fmt"
    "log"
    "net/http"
    "os"
    "sync"
    "time"

    "github.com/panjf2000/ants"
)

var (
    sitelist = make(map[string]int8)
)

func check(pool interface{}) {

    address := pool.(string)
    client := &http.Client{}
    client.Timeout = time.Second * 15

    req, _ := http.NewRequest("GET", "http://" address, nil)
    if response, err := client.Do(req); err == nil {
        status := response.StatusCode
        if status == 200 {

            sitelist[string(address)] = 1

        } else {

            sitelist[string(address)] = 0
        }
    } else {

        sitelist[string(address)] = 0

    }

}

func main() {

    defer ants.Release()
    var wg sync.WaitGroup
    p, _ := ants.NewPoolWithFunc(1000, func(i interface{}) {
        check(i)
        wg.Done()
    })
    defer p.Release()
    file, err := os.Open("example.txt")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        wg.Add(1)
        a := scanner.Text()
        _ = p.Invoke(string(a))
    }

    wg.Wait()

    fmt.Println(len(sitelist))

}

Output:

goroutine 4027 [select]:
net.cgoLookupIP({0x6b8778, 0xc002dff1c0}, {0x65d1b4, 0xf}, {0xc00073aa38, 0xc00003de10})
        /usr/lib/go/src/net/cgo_unix.go:231  0x1b7
net.(*Resolver).lookupIP(0x842360, {0x6b8778, 0xc002dff1c0}, {0x65d1b4, 0x3}, {0xc00073aa38, 0xf})
        /usr/lib/go/src/net/lookup_unix.go:97  0x128
net.glob..func1({0x6b8778, 0xc002dff1c0}, 0x0, {0x65d1b4, 0x0}, {0xc00073aa38, 0x5ea7d0})
        /usr/lib/go/src/net/hook.go:23  0x3d
net.(*Resolver).lookupIPAddr.func1()
        /usr/lib/go/src/net/lookup.go:296  0x9f
internal/singleflight.(*Group).doCall(0x842370, 0xc002a1b270, {0xc00035cc18, 0x13}, 0xc0014cfb80)
        /usr/lib/go/src/internal/singleflight/singleflight.go:95  0x3b
created by internal/singleflight.(*Group).DoChan
        /usr/lib/go/src/internal/singleflight/singleflight.go:88  0x2f1

goroutine 4028 [chan send]:
net.acquireThread()
        /usr/lib/go/src/net/net.go:675  0x45
net.cgoLookupIPCNAME({0x65d1b4, 0x3}, {0xc00073aa38, 0x4})
        /usr/lib/go/src/net/cgo_unix.go:146  0x85
net.cgoIPLookup(0x842370, {0x65d1b4, 0xc00035cc18}, {0xc00073aa38, 0xc0014cfb80})
        /usr/lib/go/src/net/cgo_unix.go:220  0x3b
created by net.cgoLookupIP
        /usr/lib/go/src/net/cgo_unix.go:230  0x125

goroutine 4053 [select]:
net.cgoLookupIP({0x6b8778, 0xc002f25140}, {0x65d1b4, 0xb}, {0xc000842f50, 0xc000040610})
        /usr/lib/go/src/net/cgo_unix.go:231  0x1b7
net.(*Resolver).lookupIP(0x842360, {0x6b8778, 0xc002f25140}, {0x65d1b4, 0x3}, {0xc000842f50, 0xb})
        /usr/lib/go/src/net/lookup_unix.go:97  0x128
net.glob..func1({0x6b8778, 0xc002f25140}, 0x0, {0x65d1b4, 0x0}, {0xc000842f50, 0x5ea7d0})
        /usr/lib/go/src/net/hook.go:23  0x3d
net.(*Resolver).lookupIPAddr.func1()
        /usr/lib/go/src/net/lookup.go:296  0x9f
internal/singleflight.(*Group).doCall(0x842370, 0xc002a97680, {0xc0006b2530, 0xf}, 0xc0014cfc30)
        /usr/lib/go/src/internal/singleflight/singleflight.go:95  0x3b
created by internal/singleflight.(*Group).DoChan
        /usr/lib/go/src/internal/singleflight/singleflight.go:88  0x2f1

goroutine 4054 [chan send]:
net.acquireThread()
        /usr/lib/go/src/net/net.go:675  0x45
net.cgoLookupIPCNAME({0x65d1b4, 0x3}, {0xc000842f50, 0x4})
        /usr/lib/go/src/net/cgo_unix.go:146  0x85
net.cgoIPLookup(0x842370, {0x65d1b4, 0xc0006b2530}, {0xc000842f50, 0xc0014cfc30})
        /usr/lib/go/src/net/cgo_unix.go:220  0x3b
created by net.cgoLookupIP
        /usr/lib/go/src/net/cgo_unix.go:230  0x125

goroutine 4029 [select]:
net.(*Resolver).lookupIPAddr(0x842360, {0x6b87e8, 0xc00173b4a0}, {0x65d1b4, 0x10}, {0xc000842f60, 0xd})
        /usr/lib/go/src/net/lookup.go:302  0x5c7
net.(*Resolver).internetAddrList(0x6b87e8, {0x6b87e8, 0xc00173b4a0}, {0x65d1b4, 0x3}, {0xc000842f60, 0x10})
        /usr/lib/go/src/net/ipsock.go:288  0x67a
net.(*Resolver).resolveAddrList(0x872790, {0x6b87e8, 0xc00173b4a0}, {0x65d30e, 0x4}, {0x65d1b4, 0xc000718d80}, {0xc000842f60, 0x10}, {0x0, ...})
        /usr/lib/go/src/net/dial.go:221  0x41b
net.(*Dialer).DialContext(0xc00002c2a0, {0x6b87e8, 0xc00173b4a0}, {0x65d1b4, 0x7f61b406d4b8}, {0xc000842f60, 0x118})
        /usr/lib/go/src/net/dial.go:406  0x448
net/http.(*Transport).dial(0xc00173b4a0, {0x6b87e8, 0xc00173b4a0}, {0x65d1b4, 0x5dcff9}, {0xc000842f60, 0xc00181bd00})
        /usr/lib/go/src/net/http/transport.go:1166  0xda
net/http.(*Transport).dialConn(0x83cd80, {0x6b87e8, 0xc00173b4a0}, {{}, 0x0, {0xc00073aa80, 0x4}, {0xc000842f60, 0x10}, 0x0})
        /usr/lib/go/src/net/http/transport.go:1604  0x845
net/http.(*Transport).dialConnFor(0xc00073aa38, 0xc0014cfd90)
        /usr/lib/go/src/net/http/transport.go:1446  0xb0
created by net/http.(*Transport).queueForDial
        /usr/lib/go/src/net/http/transport.go:1415  0x3d7

goroutine 4040 [select]:
net.cgoLookupIP({0x6b8778, 0xc002cb8640}, {0x65d1b4, 0xf}, {0xc00073aa68, 0xc000033e10})
        /usr/lib/go/src/net/cgo_unix.go:231  0x1b7
net.(*Resolver).lookupIP(0x842360, {0x6b8778, 0xc002cb8640}, {0x65d1b4, 0x3}, {0xc00073aa68, 0xf})
        /usr/lib/go/src/net/lookup_unix.go:97  0x128
net.glob..func1({0x6b8778, 0xc002cb8640}, 0x0, {0x65d1b4, 0x0}, {0xc00073aa68, 0x5ea7d0})
        /usr/lib/go/src/net/hook.go:23  0x3d
net.(*Resolver).lookupIPAddr.func1()
        /usr/lib/go/src/net/lookup.go:296  0x9f
internal/singleflight.(*Group).doCall(0x842370, 0xc0013fff90, {0xc00073b4a0, 0x13}, 0xc0014cfce0)
        /usr/lib/go/src/internal/singleflight/singleflight.go:95  0x3b
created by internal/singleflight.(*Group).DoChan
        /usr/lib/go/src/internal/singleflight/singleflight.go:88  0x2f1

goroutine 4041 [chan send]:
net.acquireThread()
        /usr/lib/go/src/net/net.go:675  0x45
net.cgoLookupIPCNAME({0x65d1b4, 0x3}, {0xc00073aa68, 0x4})
        /usr/lib/go/src/net/cgo_unix.go:146  0x85
net.cgoIPLookup(0x842370, {0x65d1b4, 0xc00073b4a0}, {0xc00073aa68, 0xc0014cfce0})
        /usr/lib/go/src/net/cgo_unix.go:220  0x3b
created by net.cgoLookupIP
        /usr/lib/go/src/net/cgo_unix.go:230  0x125

goroutine 4055 [select]:
net.(*Resolver).lookupIPAddr(0x842360, {0x6b87e8, 0xc001566720}, {0x65d1b4, 0x15}, {0xc0015462e8, 0x12})
        /usr/lib/go/src/net/lookup.go:302  0x5c7
net.(*Resolver).internetAddrList(0x6b87e8, {0x6b87e8, 0xc001566720}, {0x65d1b4, 0x3}, {0xc0015462e8, 0x15})
        /usr/lib/go/src/net/ipsock.go:288  0x67a
net.(*Resolver).resolveAddrList(0x872790, {0x6b87e8, 0xc001566720}, {0x65d30e, 0x4}, {0x65d1b4, 0xc00006e000}, {0xc0015462e8, 0x15}, {0x0, ...})
        /usr/lib/go/src/net/dial.go:221  0x41b
net.(*Dialer).DialContext(0xc00002c2a0, {0x6b87e8, 0xc001566720}, {0x65d1b4, 0x7f61b417e378}, {0xc0015462e8, 0x118})
        /usr/lib/go/src/net/dial.go:406  0x448
net/http.(*Transport).dial(0xc001566720, {0x6b87e8, 0xc001566720}, {0x65d1b4, 0x5dcff9}, {0xc0015462e8, 0xc0013d2c00})
        /usr/lib/go/src/net/http/transport.go:1166  0xda
net/http.(*Transport).dialConn(0x83cd80, {0x6b87e8, 0xc001566720}, {{}, 0x0, {0xc00047c160, 0x4}, {0xc0015462e8, 0x15}, 0x0})
        /usr/lib/go/src/net/http/transport.go:1604  0x845
net/http.(*Transport).dialConnFor(0xc000842f50, 0xc000fff1e0)
        /usr/lib/go/src/net/http/transport.go:1446  0xb0
created by net/http.(*Transport).queueForDial
        /usr/lib/go/src/net/http/transport.go:1415  0x3d7

goroutine 4030 [select]:
net.cgoLookupIP({0x6b8778, 0xc002dff280}, {0x65d1b4, 0xd}, {0xc000842f60, 0xc00003de10})
        /usr/lib/go/src/net/cgo_unix.go:231  0x1b7
net.(*Resolver).lookupIP(0x842360, {0x6b8778, 0xc002dff280}, {0x65d1b4, 0x3}, {0xc000842f60, 0xd})
        /usr/lib/go/src/net/lookup_unix.go:97  0x128
net.glob..func1({0x6b8778, 0xc002dff280}, 0x0, {0x65d1b4, 0x0}, {0xc000842f60, 0x5ea7d0})
        /usr/lib/go/src/net/hook.go:23  0x3d
net.(*Resolver).lookupIPAddr.func1()
        /usr/lib/go/src/net/lookup.go:296  0x9f
internal/singleflight.(*Group).doCall(0x842370, 0xc002a1b2c0, {0xc00035cc30, 0x11}, 0xc0014cfd90)
        /usr/lib/go/src/internal/singleflight/singleflight.go:95  0x3b
created by internal/singleflight.(*Group).DoChan
        /usr/lib/go/src/internal/singleflight/singleflight.go:88  0x2f1

goroutine 4031 [chan send]:
net.acquireThread()
        /usr/lib/go/src/net/net.go:675  0x45
net.cgoLookupIPCNAME({0x65d1b4, 0x3}, {0xc000842f60, 0x4})
        /usr/lib/go/src/net/cgo_unix.go:146  0x85
net.cgoIPLookup(0x842370, {0x65d1b4, 0xc00035cc30}, {0xc000842f60, 0xc0014cfd90})
        /usr/lib/go/src/net/cgo_unix.go:220  0x3b
created by net.cgoLookupIP
        /usr/lib/go/src/net/cgo_unix.go:230  0x125

goroutine 4042 [select]:
net.(*Resolver).lookupIPAddr(0x842360, {0x6b87e8, 0xc001eb28a0}, {0x65d1b4, 0x12}, {0xc00073ad68, 0xf})
        /usr/lib/go/src/net/lookup.go:302  0x5c7
net.(*Resolver).internetAddrList(0x6b87e8, {0x6b87e8, 0xc001eb28a0}, {0x65d1b4, 0x3}, {0xc00073ad68, 0x12})
        /usr/lib/go/src/net/ipsock.go:288  0x67a
net.(*Resolver).resolveAddrList(0x872790, {0x6b87e8, 0xc001eb28a0}, {0x65d30e, 0x4}, {0x65d1b4, 0xc0007193e0}, {0xc00073ad68, 0x12}, {0x0, ...})
        /usr/lib/go/src/net/dial.go:221  0x41b
net.(*Dialer).DialContext(0xc00002c2a0, {0x6b87e8, 0xc001eb28a0}, {0x65d1b4, 0x7f61b41711f8}, {0xc00073ad68, 0x118})
        /usr/lib/go/src/net/dial.go:406  0x448
net/http.(*Transport).dial(0xc001eb28a0, {0x6b87e8, 0xc001eb28a0}, {0x65d1b4, 0x5dcff9}, {0xc00073ad68, 0xc00147a000})
        /usr/lib/go/src/net/http/transport.go:1166  0xda
net/http.(*Transport).dialConn(0x83cd80, {0x6b87e8, 0xc001eb28a0}, {{}, 0x0, {0xc000690840, 0x4}, {0xc00073ad68, 0x12}, 0x0})
        /usr/lib/go/src/net/http/transport.go:1604  0x845
net/http.(*Transport).dialConnFor(0xc00073aa68, 0xc001ecc000)
        /usr/lib/go/src/net/http/transport.go:1446  0xb0
created by net/http.(*Transport).queueForDial
        /usr/lib/go/src/net/http/transport.go:1415  0x3d7

goroutine 4032 [select]:
net.(*Resolver).lookupIPAddr(0x842360, {0x6b87e8, 0xc00159cf00}, {0x65d1b4, 0x13}, {0xc0000bea38, 0x10})
        /usr/lib/go/src/net/lookup.go:302  0x5c7
net.(*Resolver).internetAddrList(0x6b87e8, {0x6b87e8, 0xc00159cf00}, {0x65d1b4, 0x3}, {0xc0000bea38, 0x13})
        /usr/lib/go/src/net/ipsock.go:288  0x67a
net.(*Resolver).resolveAddrList(0x872790, {0x6b87e8, 0xc00159cf00}, {0x65d30e, 0x4}, {0x65d1b4, 0xc0003a6b60}, {0xc0000bea38, 0x13}, {0x0, ...})
        /usr/lib/go/src/net/dial.go:221  0x41b
net.(*Dialer).DialContext(0xc00002c2a0, {0x6b87e8, 0xc00159cf00}, {0x65d1b4, 0x7f61b406d4b8}, {0xc0000bea38, 0x118})
        /usr/lib/go/src/net/dial.go:406  0x448
net/http.(*Transport).dial(0xc00159cf00, {0x6b87e8, 0xc00159cf00}, {0x65d1b4, 0x5dcff9}, {0xc0000bea38, 0xc0015bb800})
        /usr/lib/go/src/net/http/transport.go:1166  0xda
net/http.(*Transport).dialConn(0x83cd80, {0x6b87e8, 0xc00159cf00}, {{}, 0x0, {0xc0003c0750, 0x4}, {0xc0000bea38, 0x13}, 0x0})
        /usr/lib/go/src/net/http/transport.go:1604  0x845
net/http.(*Transport).dialConnFor(0xc000842f60, 0xc001379970)
        /usr/lib/go/src/net/http/transport.go:1446  0xb0
created by net/http.(*Transport).queueForDial
        /usr/lib/go/src/net/http/transport.go:1415  0x3d7

goroutine 4056 [select]:
net.cgoLookupIP({0x6b8778, 0xc002f25200}, {0x65d1b4, 0x12}, {0xc0015462e8, 0xc000040610})
        /usr/lib/go/src/net/cgo_unix.go:231  0x1b7
net.(*Resolver).lookupIP(0x842360, {0x6b8778, 0xc002f25200}, {0x65d1b4, 0x3}, {0xc0015462e8, 0x12})
        /usr/lib/go/src/net/lookup_unix.go:97  0x128
net.glob..func1({0x6b8778, 0xc002f25200}, 0x0, {0x65d1b4, 0x0}, {0xc0015462e8, 0x5ea7d0})
        /usr/lib/go/src/net/hook.go:23  0x3d
net.(*Resolver).lookupIPAddr.func1()
        /usr/lib/go/src/net/lookup.go:296  0x9f
internal/singleflight.(*Group).doCall(0x842370, 0xc002a976d0, {0xc000690f00, 0x16}, 0xc000fff1e0)
        /usr/lib/go/src/internal/singleflight/singleflight.go:95  0x3b
created by internal/singleflight.(*Group).DoChan
        /usr/lib/go/src/internal/singleflight/singleflight.go:88  0x2f1

goroutine 4057 [chan send]:
net.acquireThread()
        /usr/lib/go/src/net/net.go:675  0x45
net.cgoLookupIPCNAME({0x65d1b4, 0x3}, {0xc0015462e8, 0x4})
        /usr/lib/go/src/net/cgo_unix.go:146  0x85
net.cgoIPLookup(0x842370, {0x65d1b4, 0xc000690f00}, {0xc0015462e8, 0xc000fff1e0})
        /usr/lib/go/src/net/cgo_unix.go:220  0x3b
created by net.cgoLookupIP
        /usr/lib/go/src/net/cgo_unix.go:230  0x125

goroutine 4043 [select]:
net.cgoLookupIP({0x6b8778, 0xc002cb8700}, {0x65d1b4, 0xf}, {0xc00073ad68, 0xc000033e10})
        /usr/lib/go/src/net/cgo_unix.go:231  0x1b7
net.(*Resolver).lookupIP(0x842360, {0x6b8778, 0xc002cb8700}, {0x65d1b4, 0x3}, {0xc00073ad68, 0xf})
        /usr/lib/go/src/net/lookup_unix.go:97  0x128
net.glob..func1({0x6b8778, 0xc002cb8700}, 0x0, {0x65d1b4, 0x0}, {0xc00073ad68, 0x5ea7d0})
        /usr/lib/go/src/net/hook.go:23  0x3d
net.(*Resolver).lookupIPAddr.func1()
        /usr/lib/go/src/net/lookup.go:296  0x9f
internal/singleflight.(*Group).doCall(0x842370, 0xc002ce2000, {0xc00073b4b8, 0x13}, 0xc001ecc000)
        /usr/lib/go/src/internal/singleflight/singleflight.go:95  0x3b
created by internal/singleflight.(*Group).DoChan
        /usr/lib/go/src/internal/singleflight/singleflight.go:88  0x2f1

goroutine 4033 [select]:
net.cgoLookupIP({0x6b8778, 0xc002dff340}, {0x65d1b4, 0x10}, {0xc0000bea38, 0xc00003de10})
        /usr/lib/go/src/net/cgo_unix.go:231  0x1b7
net.(*Resolver).lookupIP(0x842360, {0x6b8778, 0xc002dff340}, {0x65d1b4, 0x3}, {0xc0000bea38, 0x10})
        /usr/lib/go/src/net/lookup_unix.go:97  0x128
net.glob..func1({0x6b8778, 0xc002dff340}, 0x0, {0x65d1b4, 0x0}, {0xc0000bea38, 0x5ea7d0})
        /usr/lib/go/src/net/hook.go:23  0x3d
net.(*Resolver).lookupIPAddr.func1()
        /usr/lib/go/src/net/lookup.go:296  0x9f
internal/singleflight.(*Group).doCall(0x842370, 0xc002a1b310, {0xc00035cc48, 0x14}, 0xc001379970)
        /usr/lib/go/src/internal/singleflight/singleflight.go:95  0x3b
created by internal/singleflight.(*Group).DoChan
        /usr/lib/go/src/internal/singleflight/singleflight.go:88  0x2f1

goroutine 4044 [chan send]:
net.acquireThread()
        /usr/lib/go/src/net/net.go:675  0x45
net.cgoLookupIPCNAME({0x65d1b4, 0x3}, {0xc00073ad68, 0x4})
        /usr/lib/go/src/net/cgo_unix.go:146  0x85
net.cgoIPLookup(0x842370, {0x65d1b4, 0xc00073b4b8}, {0xc00073ad68, 0xc001ecc000})
        /usr/lib/go/src/net/cgo_unix.go:220  0x3b
created by net.cgoLookupIP
        /usr/lib/go/src/net/cgo_unix.go:230  0x125

goroutine 4066 [chan send]:
net.acquireThread()
        /usr/lib/go/src/net/net.go:675  0x45
net.cgoLookupIPCNAME({0x65d1b4, 0x3}, {0xc0000bea38, 0x4})
        /usr/lib/go/src/net/cgo_unix.go:146  0x85
net.cgoIPLookup(0x842370, {0x65d1b4, 0xc00035cc48}, {0xc0000bea38, 0xc001379970})
        /usr/lib/go/src/net/cgo_unix.go:220  0x3b
created by net.cgoLookupIP
        /usr/lib/go/src/net/cgo_unix.go:230  0x125

goroutine 4001 [chan send]:
net.acquireThread()
        /usr/lib/go/src/net/net.go:675  0x45
net.cgoLookupIPCNAME({0x65d1b4, 0x3}, {0xc0005908e0, 0x4})
        /usr/lib/go/src/net/cgo_unix.go:146  0x85
net.cgoIPLookup(0x842370, {0x65d1b4, 0xc0005908f0}, {0xc0005908e0, 0xc0020ec420})
        /usr/lib/go/src/net/cgo_unix.go:220  0x3b
created by net.cgoLookupIP
        /usr/lib/go/src/net/cgo_unix.go:230  0x125

goroutine 1744 [select]:
net.(*netFD).connect.func2()
        /usr/lib/go/src/net/fd_unix.go:119  0x9e
created by net.(*netFD).connect
        /usr/lib/go/src/net/fd_unix.go:118  0x385
exit status 2
...

CodePudding user response:

In line with the answers given, I understood that there is a data race case.

I solved it with an external function with sync.Mutex

package main

import (
    "bufio"
    "fmt"
    "log"
    "net/http"
    "os"
    "sync"
    "time"

    "github.com/panjf2000/ants"
)

var (
    sitelist = make(map[string]int8)
)

var wg sync.WaitGroup
var m sync.Mutex

func add(address string, status int8, m *sync.Mutex) {
    m.Lock()
    sitelist[address] = status
    m.Unlock()
}

func check(pool interface{}) {
    address := pool.(string)

    client := &http.Client{}
    client.Timeout = time.Second * 45

    req, _ := http.NewRequest("GET", "http://" address, nil)

    if response, err := client.Do(req); err == nil {

        status := response.StatusCode
        if status == 200 {

            add(address, 1, &m)

        } else {

            add(address, 0, &m)

        }
    } else {
        add(address, 0, &m)

    }

}

func main() {

    defer ants.Release()
    p, _ := ants.NewPoolWithFunc(1000, func(i interface{}) {
        check(i)
        wg.Done()
    })

    defer p.Release()
    file, err := os.Open("example.txt")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        a := scanner.Text()
        wg.Add(1)
        _ = p.Invoke(string(a))

    }
    wg.Wait()

    fmt.Println(len(sitelist))

}

  •  Tags:  
  • go
  • Related