filters.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 AcceptAllFilterfunc() HostFilter {
  15. return HostFilterFunc(func(host *HostInfo) bool {
  16. return true
  17. })
  18. }
  19. // DataCentreHostFilter filters all hosts such that they are in the same data centre
  20. // as the supplied data centre.
  21. func DataCentreHostFilter(dataCentre string) HostFilter {
  22. return HostFilterFunc(func(host *HostInfo) bool {
  23. return host.DataCenter() == dataCentre
  24. })
  25. }
  26. // WhiteListHostFilter filters incoming hosts by checking that their address is
  27. // in the initial hosts whitelist.
  28. func WhiteListHostFilter(hosts ...string) HostFilter {
  29. m := make(map[string]bool, len(hosts))
  30. for _, host := range hosts {
  31. m[host] = true
  32. }
  33. return HostFilterFunc(func(host *HostInfo) bool {
  34. return m[host.Peer()]
  35. })
  36. }