instance.go 696 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package mhayaNats
  2. import (
  3. "time"
  4. cfacade "github.com/mhaya/facade"
  5. )
  6. var (
  7. instance = &Conn{
  8. running: false,
  9. }
  10. )
  11. func SetInstance(conn *Conn) {
  12. instance = conn
  13. }
  14. func NewFromConfig(config cfacade.ProfileJSON) *Conn {
  15. conn := New()
  16. conn.address = config.GetString("address")
  17. conn.maxReconnects = config.GetInt("max_reconnects")
  18. conn.reconnectDelay = config.GetDuration("reconnect_delay", 1) * time.Second
  19. conn.requestTimeout = config.GetDuration("request_timeout", 1) * time.Second
  20. conn.user = config.GetString("user")
  21. conn.password = config.GetString("password")
  22. if conn.address == "" {
  23. panic("address is empty!")
  24. }
  25. return conn
  26. }
  27. func Get() *Conn {
  28. return instance
  29. }