Skip to content

Commit

Permalink
[gimmeproxy] adds proxy healthcheck, adds metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Seklfreak committed Jan 3, 2018
1 parent 589b13e commit 8cdcb62
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
66 changes: 65 additions & 1 deletion helpers/gimmeproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"
"net/url"

"time"

"github.com/Seklfreak/Robyul2/cache"
)

Expand All @@ -13,6 +15,10 @@ const (
NUMBER_OF_PROXIES = 100
)

var (
PROXY_CHECK_URLS = []string{"https://instagram.com"}
)

type gimmeProxyResult struct {
SupportsHTTPS bool `json:"supportsHttps"`
Protocol string `json:"protocol"`
Expand Down Expand Up @@ -93,4 +99,62 @@ func GetRandomProxy() (proxy http.Transport, err error) {
return transport, nil
}

// TODO: test proxies, remove dead ones
func CachedProxiesHealthcheckLoop() {
defer Recover()
defer func() {
go func() {
cache.GetLogger().WithField("module", "gimmeproxy").Error(
"The CachedProxiesHealthcheckLoop died. Please investigate! Will be restarted in 60 seconds")
time.Sleep(60 * time.Second)
CachedProxiesHealthcheckLoop()
}()
}()

for {
CachedProxiesHealthcheck()

time.Sleep(1 * time.Hour)
}
}

func CachedProxiesHealthcheck() {
defer Recover()

redis := cache.GetRedisClient()
proxieUrlStrings, err := redis.SMembers(PROXIES_KEY).Result()
RelaxLog(err)

proxiesToDelete := make([]string, 0)

for _, proxyUrlString := range proxieUrlStrings {
randomProxyUrl, err := url.Parse(proxyUrlString)
if err != nil {
cache.GetLogger().WithField("module", "gimmeproxy").Infof(
"removing proxy %s because error: %s", proxyUrlString, err.Error(),
)
proxiesToDelete = append(proxiesToDelete, proxyUrlString)
continue
}

for _, proxyCheckUrl := range PROXY_CHECK_URLS {
_, err := NetGetUAWithErrorAndTransport(proxyCheckUrl, DEFAULT_UA, http.Transport{Proxy: http.ProxyURL(randomProxyUrl)})
if err != nil {
cache.GetLogger().WithField("module", "gimmeproxy").Infof(
"removing proxy %s because error: %s checking %s",
proxyUrlString, err.Error(), proxyCheckUrl,
)
proxiesToDelete = append(proxiesToDelete, proxyUrlString)
continue
}
}
}

cache.GetLogger().WithField("module", "gimmeproxy").Infof(
"deleting %d proxies from cache", len(proxiesToDelete),
)

for _, proxyToDelete := range proxiesToDelete {
_, err = redis.SRem(PROXIES_KEY, proxyToDelete).Result()
RelaxLog(err)
}
}
3 changes: 3 additions & 0 deletions launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ func main() {
})
cache.SetMachineryRedisClient(machineryRedisClient)

// start proxies healthcheck loop
helpers.CachedProxiesHealthcheckLoop()

// Make a channel that waits for a os signal
BotRuntimeChannel = make(chan os.Signal, 1)
signal.Notify(BotRuntimeChannel, os.Interrupt, os.Kill)
Expand Down
7 changes: 7 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ var (

// DiscordRestApiRequests counts all discord rest requests made
DiscordRestApiRequests = expvar.NewInt("discord_rest_api_requests")

// GimmeProxyCachedProxies counts all cached gimmeproxy proxies
GimmeProxyCachedProxies = expvar.NewInt("gimmeproxy_cached_proxies")
)

// Init starts a http server on 127.0.0.1:1337
Expand Down Expand Up @@ -224,6 +227,10 @@ func CollectRuntimeMetrics() {
} else {
YoutubeLeftQuota.Set(0)
}

redis := cache.GetRedisClient()
numberOfProxies, _ := redis.SCard(helpers.PROXIES_KEY).Result()
GimmeProxyCachedProxies.Set(numberOfProxies)
}
}

Expand Down

0 comments on commit 8cdcb62

Please sign in to comment.