Nav apraksta

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package internet
  2. import (
  3. "fmt"
  4. "github.com/garyburd/redigo/redis"
  5. )
  6. //NewASN2ASDescClient returns a new client for quering the. The caller is
  7. //reposible for closing the conn when the client is not used anymore.
  8. func NewASN2ASDescClient(conn redis.Conn) *ASN2ASDescClient {
  9. asnasd := ASN2ASDescClient{
  10. conn: conn,
  11. }
  12. return &asnasd
  13. }
  14. //ASN2ASDescClient is the query client.
  15. type ASN2ASDescClient struct {
  16. conn redis.Conn
  17. }
  18. // importedDates fetches all imported dates from redis
  19. func (i *ASN2ASDescClient) importedDates() ([]string, error) {
  20. return redis.Strings(i.conn.Do("SMEMBERS", "asd:imported_dates"))
  21. }
  22. // Current returns the latest known result for an IP2ASN lookup.
  23. func (i *ASN2ASDescClient) Current(ASN int) (string, error) {
  24. allDates, err := i.importedDates()
  25. if err != nil {
  26. return "", err
  27. }
  28. if len(allDates) < 0 {
  29. return "", err
  30. }
  31. current := allDates[len(allDates)-1]
  32. result, err := redis.String(i.conn.Do("HGET", fmt.Sprintf("asd:%d", ASN), current))
  33. if err != nil {
  34. return "", err
  35. }
  36. return result, nil
  37. }