// NewStore returns a new instance of Store. func NewStore() *Store { // Generate a temporary path. f, _ := ioutil.TempFile("", "discoverd-store-") f.Close() os.Remove(f.Name()) // Initialize store. s := &Store{Store: server.NewStore(f.Name())} // Open listener on random port. ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { panic(err) } _, port, _ := net.SplitHostPort(ln.Addr().String()) // Set default test settings. s.Listener = ln s.Advertise, _ = net.ResolveTCPAddr("tcp", net.JoinHostPort("localhost", port)) s.HeartbeatTimeout = 50 * time.Millisecond s.ElectionTimeout = 50 * time.Millisecond s.LeaderLeaseTimeout = 50 * time.Millisecond s.CommitTimeout = 5 * time.Millisecond s.EnableSingleNode = true // Turn off logs if verbose flag is not set. if !testing.Verbose() { s.LogOutput = ioutil.Discard } return s }
// openStore initializes and opens the store. func (m *Main) openStore() error { // If the advertised address is not in the peer list then we should proxy. // Resolve advertised address. addr, err := net.ResolveTCPAddr("tcp", m.advertiseAddr) if err != nil { return err } // Listen via mux storeLn := m.mux.Listen([]byte{server.StoreHdr}) // Initialize store. s := server.NewStore(m.dataDir) s.Listener = storeLn s.Advertise = addr // Allow single node if there's no peers set. s.EnableSingleNode = len(m.peers) <= 1 // Open store. if err := s.Open(); err != nil { return err } m.store = s // If peers then set peer set. if len(m.peers) > 0 { if err := s.SetPeers(m.peers); err != nil { return fmt.Errorf("set peers: %s", err) } } return nil }
// NewStore returns a new instance of Store. func NewStore() *Store { // Generate a temporary path. f, _ := ioutil.TempFile("", "discoverd-store-") f.Close() os.Remove(f.Name()) // Initialize store. s := &Store{Store: server.NewStore(f.Name())} // Retrieve a random port. port := MustRandomPort() // Set default test settings. s.BindAddress = net.JoinHostPort("", port) s.Advertise, _ = net.ResolveTCPAddr("tcp", net.JoinHostPort("localhost", port)) s.HeartbeatTimeout = 50 * time.Millisecond s.ElectionTimeout = 50 * time.Millisecond s.LeaderLeaseTimeout = 50 * time.Millisecond s.CommitTimeout = 5 * time.Millisecond s.EnableSingleNode = true // Turn off logs if verbose flag is not set. if !testing.Verbose() { s.LogOutput = ioutil.Discard } return s }
// openStore initializes and opens the store. func (m *Main) openStore(path string, ln net.Listener, advertise string, peers []string) error { // If the advertised address is not in the peer list then we should proxy. proxying := true for _, addr := range peers { if addr == advertise { proxying = false } } if proxying { return nil } // Resolve advertised address. addr, err := net.ResolveTCPAddr("tcp", advertise) if err != nil { return err } // Initialize store. s := server.NewStore(path) s.Listener = ln s.Advertise = addr // Allow single node if there's no peers set. s.EnableSingleNode = len(peers) <= 1 // Open store. if err := s.Open(); err != nil { return err } m.store = s // If peers then set peer set. if len(peers) > 0 { if err := s.SetPeers(peers); err != nil { return fmt.Errorf("set peers: %s", err) } } return nil }