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