()
| 52 | } |
| 53 | |
| 54 | func New() (*Proxy, error) { |
| 55 | p := &Proxy{} |
| 56 | var err error |
| 57 | if config.Get().RedisDB.Type == config.TypeNode { |
| 58 | p.proxyClient = &redisclient.Pool{ |
| 59 | MaxIdle: config.Get().RedisDB.ConnPoolSize, |
| 60 | IdleTimeout: time.Duration(config.Get().RedisDB.ConnAliveTimeOut) * time.Second, |
| 61 | Dial: func() (redisclient.Conn, error) { |
| 62 | c, err := redisclient.Dial("tcp", config.Get().RedisDB.StartNodes, |
| 63 | redisclient.DialConnectTimeout(time.Duration(config.Get().RedisDB.ConnTimeOut)*time.Second), |
| 64 | redisclient.DialReadTimeout(time.Duration(config.Get().RedisDB.ConnReadTimeOut)*time.Second), |
| 65 | redisclient.DialWriteTimeout(time.Duration(config.Get().RedisDB.ConnWriteTimeOut)*time.Second)) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | return c, err |
| 70 | }, |
| 71 | TestOnBorrow: func(c redisclient.Conn, t time.Time) error { |
| 72 | _, err := c.Do("PING") |
| 73 | return err |
| 74 | }, |
| 75 | } |
| 76 | p.router = proxynode.NewRouter(p.proxyClient) |
| 77 | } else { |
| 78 | p.proxyCluster, err = rediscluster.NewCluster( |
| 79 | &rediscluster.Options{ |
| 80 | StartNodes: strings.Split(config.Get().RedisDB.StartNodes, ","), |
| 81 | ConnTimeout: time.Duration(config.Get().RedisDB.ConnTimeOut) * time.Second, |
| 82 | ReadTimeout: time.Duration(config.Get().RedisDB.ConnReadTimeOut) * time.Second, |
| 83 | WriteTimeout: time.Duration(config.Get().RedisDB.ConnWriteTimeOut) * time.Second, |
| 84 | KeepAlive: config.Get().RedisDB.ConnPoolSize, |
| 85 | AliveTime: time.Duration(config.Get().RedisDB.ConnAliveTimeOut) * time.Second, |
| 86 | }) |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | p.router = proxycluster.NewRouter(p.proxyCluster) |
| 91 | } |
| 92 | |
| 93 | // if enable p2p command pubsub mode,then create p2p pubsub handle |
| 94 | if config.Get().P2P.Enable { |
| 95 | logrus.Println("Starting Pubsub ...") |
| 96 | // create p2p element |
| 97 | p2phost := p2p.NewP2P(config.Get().P2P.ServiceDiscoveryID, config.Get().P2P.NodeHostIP, config.Get().P2P.NodeHostPort) // create p2p |
| 98 | p.P2pHost = p2phost |
| 99 | |
| 100 | logrus.Println("Completed P2P Setup") |
| 101 | |
| 102 | // Connect to peers with the chosen discovery method |
| 103 | switch strings.ToLower(config.Get().P2P.ServiceDiscoverMode) { |
| 104 | case "announce": |
| 105 | p2phost.AnnounceConnect() // KadDHT p2p net create |
| 106 | case "advertise": |
| 107 | p2phost.AdvertiseConnect() |
| 108 | default: |
| 109 | p2phost.AdvertiseConnect() |
| 110 | } |
| 111 |
no test coverage detected