dao.go 809 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/bfs/conf"
  6. bm "go-common/library/net/http/blademaster"
  7. "github.com/samuel/go-zookeeper/zk"
  8. )
  9. // Dao dao
  10. type Dao struct {
  11. c *conf.Config
  12. zkcs map[string]*zk.Conn
  13. httpCli *bm.Client
  14. }
  15. // New init mysql db
  16. func New(c *conf.Config) (dao *Dao) {
  17. zkcs := make(map[string]*zk.Conn)
  18. for name, zkCfg := range c.Zookeepers {
  19. zkc, _, err := zk.Connect(zkCfg.Addrs, time.Duration(zkCfg.Timeout))
  20. if err != nil {
  21. panic(err)
  22. }
  23. zkcs[name] = zkc
  24. }
  25. dao = &Dao{
  26. c: c,
  27. zkcs: zkcs,
  28. httpCli: bm.NewClient(c.HTTPClient),
  29. }
  30. return
  31. }
  32. // Ping dao ping
  33. func (d *Dao) Ping(c context.Context) error {
  34. return nil
  35. }
  36. // Close close the resource.
  37. func (d *Dao) Close() {
  38. for _, zkc := range d.zkcs {
  39. zkc.Close()
  40. }
  41. }