~shulhan/karajo

14b1aa3f5fef1c3dd028a08fa989d8a69e080370 — Shulhan 1 year, 8 months ago a7e8ff6
all: fix possible lock on API environment

Sometimes the request to /karajo/api/environment does not return any
result.  The only explanation is something lock the resource so we cannot
lock it and it will wait forever.

Until we found the cause, remove the lock but expect for data race, but
the data race should be safe since the API only doing read.
2 files changed, 0 insertions(+), 35 deletions(-)

M environment.go
M karajo.go
M environment.go => environment.go +0 -30
@@ 204,20 204,6 @@ func (env *Environment) jobHttp(id string) (job *JobHttp) {
	return nil
}

func (env *Environment) jobsLock() {
	var job *Job
	for _, job = range env.Jobs {
		job.Lock()
	}
}

func (env *Environment) jobsUnlock() {
	var job *Job
	for _, job = range env.Jobs {
		job.Unlock()
	}
}

func (env *Environment) init() (err error) {
	var (
		logp = `init`


@@ 325,14 311,6 @@ func (env *Environment) initDirs() (err error) {
	return nil
}

// httpJobsLock lock all the jobs.
func (env *Environment) httpJobsLock() {
	var jobHttp *JobHttp
	for _, jobHttp = range env.HttpJobs {
		jobHttp.Lock()
	}
}

func (env *Environment) httpJobsSave() (err error) {
	var jobHttp *JobHttp
	for _, jobHttp = range env.HttpJobs {


@@ 344,14 322,6 @@ func (env *Environment) httpJobsSave() (err error) {
	return nil
}

// httpJobsUnlock unlock all the jobs.
func (env *Environment) httpJobsUnlock() {
	var jobHttp *JobHttp
	for _, jobHttp = range env.HttpJobs {
		jobHttp.Unlock()
	}
}

// loadConfigJob load jobs configuration from file.
func (env *Environment) loadConfigJob(conf string) (jobs map[string]*Job, err error) {
	type jobContainer struct {

M karajo.go => karajo.go +0 -5
@@ 329,12 329,7 @@ func (k *Karajo) apiEnvironment(epr *libhttp.EndpointRequest) (resbody []byte, e
	res.Code = http.StatusOK
	res.Data = k.env

	k.env.jobsLock()
	k.env.httpJobsLock()
	resbody, err = json.Marshal(res)
	k.env.httpJobsUnlock()
	k.env.jobsUnlock()

	if err != nil {
		return nil, fmt.Errorf(`%s: %w`, logp, err)
	}