filters.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package gocql
  2. // HostFilter interface is used when a host is discovered via server sent events.
  3. type HostFilter interface {
  4. // Called when a new host is discovered, returning true will cause the host
  5. // to be added to the pools.
  6. Accept(host *HostInfo) bool
  7. }
  8. // HostFilterFunc converts a func(host HostInfo) bool into a HostFilter
  9. type HostFilterFunc func(host *HostInfo) bool
  10. func (fn HostFilterFunc) Accept(host *HostInfo) bool {
  11. return fn(host)
  12. }
  13. // AcceptAllFilter will accept all hosts
  14. func AcceptAllFilter() HostFilter {
  15. return HostFilterFunc(func(host *HostInfo) bool {
  16. return true
  17. })
  18. }
  19. func DenyAllFilter() HostFilter {
  20. return HostFilterFunc(func(host *HostInfo) bool {
  21. return false
  22. })
  23. }
  24. // DataCentreHostFilter filters all hosts such that they are in the same data centre
  25. // as the supplied data centre.
  26. func DataCentreHostFilter(dataCentre string) HostFilter {
  27. return HostFilterFunc(func(host *HostInfo) bool {
  28. return host.DataCenter() == dataCentre
  29. })
  30. }
  31. // WhiteListHostFilter filters incoming hosts by checking that their address is
  32. // in the initial hosts whitelist.
  33. func WhiteListHostFilter(hosts ...string) HostFilter {
  34. m := make(map[string]bool, len(hosts))
  35. for _, host := range hosts {
  36. m[host] = true
  37. }
  38. return HostFilterFunc(func(host *HostInfo) bool {
  39. return m[host.Peer()]
  40. })
  41. }