Mutex for Bleve Write/Read Operation

Hi,

Supposed I have a system for asynchronous PvP matchmaking using Bleve which has three functionalities.

  • The first one is for periodically cleaning up data in Bleve if the data in Bleve is outdated.
  • The second one is for reading the data from Bleve.
  • The third one is for writing data to Bleve from Nakama Storage.

Based on this functionality, the cleaning up functionality can only be executed by 1 instance, while the read and write functionality can be executed by multiple instances concurrently.

To ensure the data integrity, I know a mutex should be applied to all 3 functionalities. However, I’m not sure how to implement the mutex correctly. I’m afraid that by using mutex, the read functionality will be blocked. This is concerning since the read functionality needs to respond quickly.

Any thoughts on what kind of mutex that I should use? Also, where should I put the mutex? Should I put it to all 3 of the functionalities?

Here is my simple code for the read and write functionality, I haven’t implemented it in nakama yet.

 func Read() {
	index, err := bleve.Open("player.bleve")
	defer index.Close()
	if err != nil {
		log.Fatal(err)
		return
	}

	query := bleve.NewQueryStringQuery("*")
	req := bleve.NewSearchRequest(query)

	res, err := index.Search(req)
	if err != nil {
		log.Fatal(err)
		return
	}
	fmt.Println(res)
}

func Write(player *Player) {
	index, err := bleve.Open("player.bleve")
	defer index.Close()
	if err != nil {
		log.Fatal(err)
		return
	}

	index.Index(player.ID, player)
}