code: func (m *Main) Demote() error {
  	m.mu.Lock()
  	defer m.mu.Unlock()
  
  	m.logger.Println("demotion requested")
  
  	var leaderAddr string
  	if m.store != nil {
  		leaderAddr := m.store.Leader()
  		if leaderAddr == "" {
  			return server.ErrNoKnownLeader
  		}
  	} else {
  		leader, err := discoverd.DefaultClient.RaftLeader()
  		if err != nil || (err == nil && leader.Host == "") {
  			return fmt.Errorf("failed to get leader address from configured peers")
  		}
  		leaderAddr = leader.Host
  	}
  
  	// Update the dns server to use proxy store
  	if m.dnsServer != nil {
  		m.dnsServer.SetStore(&server.ProxyStore{Peers: m.peers})
  	}
  
  	// Set handler into proxy mode
  	m.handler.Proxy.Store(true)
  
  	// If we fail from here on out we should attempt to rollback
  	rollback := func() {
  		if m.dnsServer != nil {
  			m.dnsServer.SetStore(m.store)
  		}
  		m.handler.Proxy.Store(false)
  	}
  
  	// If we are the leader remove ourselves directly,
  	// otherwise request the leader remove us from the peer set.
  	if leaderAddr == m.advertiseAddr {
  		if err := m.store.RemovePeer(m.advertiseAddr); err != nil {
  			rollback()
  			return err
  		}
  	} else {
  		if err := discoverd.DefaultClient.RaftRemovePeer(m.advertiseAddr); err != nil {
  			rollback()
  			return err
  		}
  	}
  
  	// Close the raft store.
  	if m.store != nil {
  		m.store.Close()
  		m.store = nil
  	}
  	m.handler.Store = nil
  
  	m.logger.Println("demoted successfully")
  	return nil
  }
docs: // Leave the consensus set, demoting ourselves to proxy from raft node.