V
V
veryoriginalnickname2021-10-08 19:40:40
go
veryoriginalnickname, 2021-10-08 19:40:40

Will the error array be shared?

There is such a scheme:

// eBase - parent for all other entities.
var eBase *entityBase

// eAuth - manage the authorization.
var eAuth *entityAuth

// выполняется при старте сервера
func bootEntities() {
  eBase = &entityBase{core.NewBaseController()}
  // в eAuth хранится все, что связяано с авторизацией. Контроллеры, обращения к БД, и так далее.
  eAuth = &entityAuth{entityBase: eBase}
}

// entityAuth - manage authorization.
type entityAuth struct {
  *entityBase
  bodyLogin *bodyAuthLogin
}

type entityBase struct {
  *core.BaseController
}

//// пакет core
// NewBaseController - create BaseController.
func NewBaseController() *BaseController {
  return &BaseController{Logger: Logger, EC: errorCollector.New()}
}
// BaseController - template for controller with cool features.
type BaseController struct {
  // Logger - its a logger.
  Logger *logger.Logger
  // EC - errorCollector. Used to collect errors and send it in JSON.
  EC *errorCollector.ErrorCollector
}

As you can see, when the application starts, a BaseController is created, and an errorCollector (EC) is created in the NewBaseController() function. EC is something like an array of errors, in which you can add them, and then, upon request, you can send them as JSON.
For example, here comes the request:
func (a *entityAuth) controllerLogin(response http.ResponseWriter, request *http.Request) {
  // при ошибках валидации, валидатор добавляет ошибки в массив EC через что-то типа a.EC.AddError().
  _ = a.validatorControllerLogin(request)
  // и если есть ошибки (длинна массива ошибок больше нуля), то они отправляются пользователю
  if a.EC.HasErrors() {
    // при отправке ответа массив с ошибками очищается
    a.Send(response, a.EC.GetErrors(), 400)
    return
  }

The question is: what if two requests come in at the same time? One user will have validation errors, and the other will not. It turns out that either two users will leave with a 400 error, or will there be something else fun? After all, at the start of the application, only one instance of EC is created, and it turns out that different users will have a common array of errors, or what? If so, how can this be fixed without adding a new instance of errorCollector to each controller? I was googling and stumbled upon sync.Mutex, is that what you need to solve this problem, or is it something else altogether?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-10-08
@veryoriginalnickname

sync.Mutex in this situation is not an option at all, the performance of the whole service will be extremely low
. In your implementation, I would take out the list of errors in a separate object and make the validatorControllerLogin method return it, i. to get something like this.

func (a *entityAuth) controllerLogin(response http.ResponseWriter, request *http.Request) {
  // при ошибках валидации, валидатор добавляет ошибки в массив EC через что-то типа a.EC.AddError().
  errs = a.validatorControllerLogin(request)
  // и если есть ошибки (длинна массива ошибок больше нуля), то они отправляются пользователю
  if errs.HasErrors() {
    // при отправке ответа массив с ошибками очищается
    a.Send(response, errs.GetErrors(), 400)
    return
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question