package cluster import ( "context" "time" "gitlink.org.cn/cloudream/common/pkgs/logger" clirpc "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/rpc/client" ) type Cluster struct { cfg Config cliPool *clirpc.Pool } func New(cfg *Config) *Cluster { c := Config{} if cfg != nil { c = *cfg } else { c.IsMaster = true } return &Cluster{ cfg: c, } } func (c *Cluster) Start() error { log := logger.WithField("Mod", "Cluster") if c.cfg.IsMaster { log.Infof("cluster start as master") return nil } poolCfgJSON := clirpc.PoolConfigJSON{ Address: c.cfg.MasterAddress, RootCA: c.cfg.RootCA, ClientCert: c.cfg.ClientCert, ClientKey: c.cfg.ClientKey, } poolCfg, err := poolCfgJSON.Build() if err != nil { return err } c.cliPool = clirpc.NewPool(*poolCfg) for { cli := c.cliPool.Get() ctx, cancelFn := context.WithTimeout(context.Background(), 10*time.Second) resp, cerr := cli.GetClusterMasterInfo(ctx, &clirpc.GetClusterMasterInfo{}) cancelFn() if cerr != nil { log.Warnf("first report: %v, will retry after 3 seconds", cerr.ToError()) time.Sleep(3 * time.Second) continue } log.Infof("cluster start as slave, master is: %v", resp.Name) break } return nil } func (c *Cluster) IsMaster() bool { return c.cfg.IsMaster } func (c *Cluster) Name() string { return c.cfg.Name } func (c *Cluster) MasterClient() *clirpc.Pool { return c.cliPool } func (c *Cluster) RaftTransport() *Transport { return nil }