Hucaru / Valhalla

A Golang MapleStory (v28) server
MIT License
274 stars 71 forks source link

Refactor database handling #33

Closed ErwinsExpertise closed 3 years ago

ErwinsExpertise commented 4 years ago

Currently the database is handled by passing around a DB pointer. This method is not as efficient as it could be, and it would be better to have a database package to handle operations rather than multiple packages performing operations.

All architecture proposals are welcome

Hucaru commented 4 years ago

I think having a function such as in the package:

var SendQuery chan string

func Connect(addr string, bufferSize int) error {
  // connection stuff

  SendQuery = make(chan, string, bufferSize)

  return nil
}

func Executor(query chan int, issue chan err) {
  for {
    select {
      case q := <-Query:
        // execute query
        if err != nil {
          issue <- err
        }
    }
  }
}

That is started during server creation like:

err := db.Connect(address, 512) // get the number from the toml settings files
go db.Executor(db.SendQuery, issue)

The error can be handled in the main select statement in package main of the server and would most likely cause the server to shutdown as database issues compromise game integrity and could lead to all sorts of state problems.

Hucaru commented 3 years ago

After looking at the sql package it has an internal pool, by default it creates a new worker per query. Therefore change to just have a maximum of idle connections to allow re-usable workers and moved into its own package as a global.