forked from cloudflare/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(protobuf): add and populate the sampler_hostname field
- Loading branch information
Showing
6 changed files
with
124 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package sampler | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type cache struct { | ||
items map[string]string | ||
sync.RWMutex | ||
} | ||
|
||
func newcache() *cache { | ||
cache := &cache{ | ||
map[string]string{}, | ||
sync.RWMutex{}, | ||
} | ||
go func() { | ||
for range time.Tick(time.Hour) { | ||
go cache.update() | ||
} | ||
}() | ||
return cache | ||
} | ||
|
||
func (c *cache) get(ip net.IP) (string, bool) { | ||
c.RLock() | ||
defer c.RUnlock() | ||
v, ok := c.items[ip.String()] | ||
return v, ok | ||
} | ||
|
||
func (c *cache) set(ip net.IP) { | ||
c.Lock() | ||
defer c.Unlock() | ||
c.items[ip.String()] = getHostnameFromIp(ip) | ||
fmt.Printf("New sampler discovered: (%s)=> %s\n", ip.String(), c.items[ip.String()]) | ||
} | ||
|
||
func getHostnameFromIp(ip net.IP) string { | ||
hostname, err := net.LookupAddr(ip.String()) | ||
if err != nil || len(hostname) <= 0 { | ||
return "" | ||
} | ||
if hostname[0][len(hostname[0])-1] == '.' { | ||
return hostname[0][:len(hostname[0])-1] | ||
} | ||
return hostname[0] | ||
} | ||
|
||
func (c *cache) update() { | ||
c.Lock() | ||
defer c.Unlock() | ||
for k := range c.items { | ||
newHostname := getHostnameFromIp(net.ParseIP(k)) | ||
if newHostname != "" && newHostname != c.items[k] { | ||
c.items[k] = newHostname | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package sampler | ||
|
||
import "fmt" | ||
|
||
var reverse *cache | ||
|
||
func Init() { | ||
reverse = newcache() | ||
fmt.Println("Reverse DNS cache initialized") | ||
} | ||
|
||
func GetHostnameByByteSlice(ip []byte) string { | ||
if reverse, ok := reverse.get(ip); ok { | ||
return reverse | ||
} | ||
|
||
// Latency will be low at the cost of missing items during the discovery phase | ||
go reverse.set(ip) | ||
return "" | ||
} |