Created
November 19, 2015 19:40
-
-
Save gallir/b1532dfa447ead84bd20 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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