lxd/cluster/gateway.go
func (g *Gateway) init() error {
	logger.Debugf("Initializing database gateway")
	raft, err := newRaft(g.db, g.cert, g.options.latency)
	if err != nil {
		return errors.Wrap(err, "Failed to create raft factory")
	}

	// If the resulting raft instance is not nil, it means that this node
	// should serve as database node, so create a dqlite driver to be
	// exposed it over gRPC.
	if raft != nil {
		listener, err := net.Listen("unix", "")
		if err != nil {
			return errors.Wrap(err, "Failed to allocate loopback port")
		}

		if raft.HandlerFunc() == nil {
			g.memoryDial = dqliteMemoryDial(listener)
			g.store.inMemory = dqlite.NewInmemServerStore()
			g.store.Set(context.Background(), []dqlite.ServerInfo{{Address: "0"}})
		} else {
			go runDqliteProxy(listener, g.acceptCh)
			g.store.inMemory = nil
		}

		provider := &raftAddressProvider{db: g.db}
		server, err := dqlite.NewServer(
			raft.Raft(), raft.Registry(), listener,
			dqlite.WithServerAddressProvider(provider),
			dqlite.WithServerLogFunc(DqliteLog),
		)
		if err != nil {
			return errors.Wrap(err, "Failed to create dqlite server")
		}

		g.server = server
		g.raft = raft
	} else {
		g.server = nil
		g.raft = nil
		g.store.inMemory = nil
	}

	g.store.onDisk = dqlite.NewServerStore(g.db.DB(), "main", "raft_nodes", "address")

	return nil
}
func (g *Gateway) init() error {
	logger.Debugf("Initializing database gateway")
	raft, err := newRaft(g.db, g.cert, g.options.latency)
	if err != nil {
		return errors.Wrap(err, "Failed to create raft factory")
	}

	// If the resulting raft instance is not nil, it means that this node
	// should serve as database node, so create a dqlite driver to be
	// exposed it over gRPC.
	if raft != nil {
		listener, err := net.Listen("unix", "")
		if err != nil {
			return errors.Wrap(err, "Failed to allocate loopback port")
		}

		if raft.HandlerFunc() == nil {
			g.memoryDial = dqliteMemoryDial(listener)
			g.store.inMemory = dqlite.NewInmemServerStore()
			g.store.Set(context.Background(), []dqlite.ServerInfo{{Address: "0"}})
		} else {
			go runDqliteProxy(listener, g.acceptCh)
			g.store.inMemory = nil
		}

		provider := &raftAddressProvider{db: g.db}
		server, err := dqlite.NewServer(
			raft.Raft(), raft.Registry(), listener,
			dqlite.WithServerAddressProvider(provider),
			dqlite.WithServerLogFunc(DqliteLog),
		)
		if err != nil {
			return errors.Wrap(err, "Failed to create dqlite server")
		}

		g.server = server
		g.raft = raft
	} else {
		g.server = nil
		g.raft = nil
		g.store.inMemory = nil
	}

	g.store.onDisk = dqlite.NewServerStore(g.db.DB(), "main", "raft_nodes", "address")

	return nil
}
// Initialize the gateway, creating a new raft factory and gRPC server (if this
// node is a database node), and a gRPC dialer.