Skip to content

Instantly share code, notes, and snippets.

@gallir
Created November 19, 2015 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gallir/b1532dfa447ead84bd20 to your computer and use it in GitHub Desktop.
Save gallir/b1532dfa447ead84bd20 to your computer and use it in GitHub Desktop.
func (acl *ACL) checkServer(ip string) bool {
type Params struct {
IP string `url:"ip"`
Name string `url:"name"`
}
ops := requestOps // To check laer
if _, found := acl.rejected.Get(ip); found {
log.Printf("In rejected %s", ip)
return false
}
params := &Params{IP: ip, Name: configuration.Name}
request, _ := sling.New().Base(configuration.Server).Get("/ip_enabled").QueryStruct(params).Request()
// Avoid request storms to the server
requestMutex.Lock()
defer requestMutex.Unlock()
if ops != requestOps { // Another thread made a request, check again locally
if _, found := acl.authorized.Get(ip); found {
return true
}
}
resp, err := http.DefaultClient.Do(request)
requestOps++
if err == nil {
defer resp.Body.Close()
if resp.StatusCode == 200 {
acl.add(ip, ACCESS_CLIENT, 3600) // One hour for IPs authorized from the server
return true
}
}
acl.rejected.Set(ip, true, cache.DefaultExpiration)
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment