Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema Slowdown Fix #218

Merged
merged 1 commit into from
Jul 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/server/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Init(ctx types.Context, config gofig.Config) error {
func (sc *serviceContainer) Init(ctx types.Context, config gofig.Config) error {
sc.config = config

if err := sc.taskService.Init(config); err != nil {
if err := sc.taskService.Init(ctx, config); err != nil {
return err
}

Expand Down
60 changes: 41 additions & 19 deletions api/server/services/services_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import (

type task struct {
types.Task
ctx types.Context
runFunc types.TaskRunFunc
storRunFunc types.StorageTaskRunFunc
storService types.StorageService
resultSchema []byte
done chan int
ctx types.Context
runFunc types.TaskRunFunc
storRunFunc types.StorageTaskRunFunc
storService types.StorageService
resultSchema []byte
resultSchemaValidationEnabled bool
done chan int
}

func newTask(ctx types.Context, schema []byte) *task {
Expand Down Expand Up @@ -83,30 +84,50 @@ func execTask(t *task) {
return
}

if t.Result != nil && t.resultSchema != nil {
var buf []byte
if buf, t.Error = json.Marshal(t.Result); t.Error != nil {
return
}
if t.Result == nil {
t.ctx.Debug("skipping response schema validation; result == nil")
return
}

t.Error = schema.Validate(t.ctx, t.resultSchema, buf)
if t.Error != nil {
return
}
if t.resultSchema == nil {
t.ctx.Debug("skipping response schema validation; schema == nil")
return
}

if !t.resultSchemaValidationEnabled {
t.ctx.Debug("skipping response schema validation; disabled")
return
}

var buf []byte
if buf, t.Error = json.Marshal(t.Result); t.Error != nil {
return
}

t.Error = schema.Validate(t.ctx, t.resultSchema, buf)
if t.Error != nil {
return
}
}

type globalTaskService struct {
sync.RWMutex
name string
config gofig.Config
tasks map[int]*task
name string
config gofig.Config
tasks map[int]*task
resultSchemaValidationEnabled bool
}

// Init initializes the service.
func (s *globalTaskService) Init(config gofig.Config) error {
func (s *globalTaskService) Init(ctx types.Context, config gofig.Config) error {
s.tasks = map[int]*task{}
s.config = config

s.resultSchemaValidationEnabled = config.GetBool(
types.ConfigSchemaResponseValidationEnabled)
ctx.WithField("enabled", s.resultSchemaValidationEnabled).Debug(
"configured result schema validation")

return nil
}

Expand Down Expand Up @@ -149,6 +170,7 @@ func (s *globalTaskService) taskTrack(ctx types.Context) *task {
ID: taskID,
QueueTime: now,
},
resultSchemaValidationEnabled: s.resultSchemaValidationEnabled,
ctx: ctx.WithValue(context.TaskKey, fmt.Sprintf("%d", taskID)),
}

Expand Down
4 changes: 4 additions & 0 deletions api/types/types_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,8 @@ const (

// ConfigDeviceScanType is a config key.
ConfigDeviceScanType = ConfigRoot + ".device.scanType"

// ConfigSchemaResponseValidationEnabled is a config key.
ConfigSchemaResponseValidationEnabled = ConfigRoot +
".schema.responseValidationEnabled"
)