Skip to content

Commit

Permalink
fix: minor perf fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Jan 21, 2023
1 parent 43e619b commit e00878e
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 104 deletions.
89 changes: 42 additions & 47 deletions core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ type graphjin struct {
tracer tracer
pf []byte
opts []Option
done chan bool
}

type GraphJin struct {
atomic.Value
done chan bool
}

type Option func(*graphjin) error
Expand All @@ -90,46 +92,40 @@ type Option func(*graphjin) error
func NewGraphJin(conf *Config, db *sql.DB, options ...Option) (g *GraphJin, err error) {
bp, err := basePath(conf)
if err != nil {
return nil, err
return
}
fs := fs.NewOsFSWithBase(bp)

gj, err := newGraphJin(conf, db, nil, fs, options...)
if err != nil {
return nil, err
g = &GraphJin{done: make(chan bool)}
if err = g.newGraphJin(conf, db, nil, fs, options...); err != nil {
return
}

g = &GraphJin{}
g.Store(gj)

if err := g.initDBWatcher(); err != nil {
return nil, err
if err = g.initDBWatcher(); err != nil {
return
}
return g, nil
return
}

func NewGraphJinWithFS(conf *Config, db *sql.DB, fs plugin.FS, options ...Option) (g *GraphJin, err error) {
gj, err := newGraphJin(conf, db, nil, fs, options...)
if err != nil {
return nil, err
g = &GraphJin{done: make(chan bool)}
if err = g.newGraphJin(conf, db, nil, fs, options...); err != nil {
return
}

g = &GraphJin{}
g.Store(gj)

if err := g.initDBWatcher(); err != nil {
return nil, err
if err = g.initDBWatcher(); err != nil {
return
}
return g, nil
return
}

// newGraphJin helps with writing tests and benchmarks
func newGraphJin(conf *Config,
// it all starts here
func (g *GraphJin) newGraphJin(conf *Config,
db *sql.DB,
dbinfo *sdata.DBInfo,
fs plugin.FS,
options ...Option,
) (*graphjin, error) {
) (err error) {
if conf == nil {
conf = &Config{Debug: true}
}
Expand All @@ -148,6 +144,7 @@ func newGraphJin(conf *Config,
opts: options,
scriptMap: make(map[string]plugin.ScriptCompiler),
fs: fs,
done: g.done,
}

if gj.conf.DisableProdSecurity {
Expand All @@ -156,42 +153,42 @@ func newGraphJin(conf *Config,

// ordering of these initializer matter, do not re-order!

if err := gj.initCache(); err != nil {
return nil, err
if err = gj.initCache(); err != nil {
return
}

if err := gj.initConfig(); err != nil {
return nil, err
if err = gj.initConfig(); err != nil {
return
}

for _, op := range options {
if err := op(gj); err != nil {
return nil, err
if err = op(gj); err != nil {
return
}
}

if err := gj.initDiscover(); err != nil {
return nil, err
if err = gj.initDiscover(); err != nil {
return
}

if err := gj.initResolvers(); err != nil {
return nil, err
if err = gj.initResolvers(); err != nil {
return
}

if err := gj.initSchema(); err != nil {
return nil, err
if err = gj.initSchema(); err != nil {
return
}

if err := gj.initAllowList(); err != nil {
return nil, err
if err = gj.initAllowList(); err != nil {
return
}

if err := gj.initCompilers(); err != nil {
return nil, err
if err = gj.initCompilers(); err != nil {
return
}

if err := gj.prepareRoleStmt(); err != nil {
return nil, err
if err = gj.prepareRoleStmt(); err != nil {
return
}

if conf.SecretKey != "" {
Expand All @@ -200,7 +197,8 @@ func newGraphJin(conf *Config,
gj.encKeySet = true
}

return gj, nil
g.Store(gj)
return
}

func OptionSetNamespace(namespace string) Option {
Expand Down Expand Up @@ -516,13 +514,10 @@ func (g *GraphJin) Reload() error {
return g.reload(nil)
}

func (g *GraphJin) reload(di *sdata.DBInfo) error {
func (g *GraphJin) reload(di *sdata.DBInfo) (err error) {
gj := g.Load().(*graphjin)
gjNew, err := newGraphJin(gj.conf, gj.db, di, gj.fs, gj.opts...)
if err == nil {
g.Store(gjNew)
}
return err
err = g.newGraphJin(gj.conf, gj.db, di, gj.fs, gj.opts...)
return
}

// IsProd return true for production mode or false for development mode
Expand Down
46 changes: 23 additions & 23 deletions core/internal/util/util_mit_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ian Coleman
* Copyright (c) 2018 Ma_124, <github.com/Ma124>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, Subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or Substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
The MIT License (MIT)
Copyright (c) 2015 Ian Coleman
Copyright (c) 2018 Ma_124, <github.com/Ma124>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, Subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or Substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package util

Expand Down
50 changes: 17 additions & 33 deletions core/internal/valid/validex.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,30 @@ import (
"github.com/dosco/graphjin/v2/core/internal/qcode"
)

const (
alphaRegexString = "^[a-zA-Z]+$"
alphaNumericRegexString = "^[a-zA-Z0-9]+$"
alphaUnicodeRegexString = "^[\\p{L}]+$"
alphaUnicodeNumericRegexString = "^[\\p{L}\\p{N}]+$"
numericRegexString = "^[-+]?[0-9]+(?:\\.[0-9]+)?$"
numberRegexString = "^[0-9]+$"
emailRegexString = "^(?:(?:(?:(?:[a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(?:\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|(?:(?:\\x22)(?:(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(?:\\x20|\\x09)+)?(?:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(\\x20|\\x09)+)?(?:\\x22))))@(?:(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
uUID3RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$"
uUID4RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
uUID5RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
uUIDRegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
uUID3RFC4122RegexString = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-3[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
uUID4RFC4122RegexString = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
uUID5RFC4122RegexString = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-5[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
uUIDRFC4122RegexString = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
uLIDRegexString = "^[A-HJKMNP-TV-Z0-9]{26}$"
)

var Formats = map[string]*regexp.Regexp{
"alpha": regexp.MustCompile(alphaRegexString),
"alphaNumeric": regexp.MustCompile(alphaNumericRegexString),
"alphaUnicode": regexp.MustCompile(alphaUnicodeRegexString),
"alphaUnicodeNumeric": regexp.MustCompile(alphaUnicodeNumericRegexString),
"numeric": regexp.MustCompile(numericRegexString),
"number": regexp.MustCompile(numberRegexString),
"email": regexp.MustCompile(emailRegexString),
"uuid": regexp.MustCompile(uUIDRegexString),
"uuid3": regexp.MustCompile(uUID3RegexString),
"uuid4": regexp.MustCompile(uUID4RegexString),
"uuid5": regexp.MustCompile(uUID5RegexString),
"ulid": regexp.MustCompile(uLIDRegexString),
var Formats = map[string]string{
"alpha": "^[a-zA-Z]+$",
"alphaNumeric": "^[a-zA-Z0-9]+$",
"alphaUnicode": "^[\\p{L}]+$",
"alphaUnicodeNumeric": "^[\\p{L}\\p{N}]+$",
"numeric": "^[-+]?[0-9]+(?:\\.[0-9]+)?$",
"number": "^[0-9]+$",
"email": "^(?:(?:(?:(?:[a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(?:\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|(?:(?:\\x22)(?:(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(?:\\x20|\\x09)+)?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3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$",
"uuid5": "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$",
"ulid": "^[A-HJKMNP-TV-Z0-9]{26}$",
}

func format(args []string) (fn qcode.ValidFn, err error) {
re, ok := Formats[args[0]]
reStr, ok := Formats[args[0]]
if !ok {
err = fmt.Errorf("unknown format: %s", args[0])
return
}
re, err := regexp.Compile(reStr)
if err != nil {
return
}
fn = func(vars qcode.Vars, c qcode.Constraint) (ok bool) {
v1, ok := vars[c.VarName]
if ok {
Expand Down
3 changes: 3 additions & 0 deletions core/subs.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ func (gj *graphjin) subController(sub *sub) {

case <-time.After(ps):
sub.fanOutJobs(gj)

case <-gj.done:
return
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions core/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,11 @@ func (g *GraphJin) startDBWatcher(ps time.Duration) {
if err := g.reload(latestDi); err != nil {
gj.log.Println(err)
}

select {
case <-g.done:
return
default:
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphjin",
"version": "2.0.27",
"version": "2.0.29",
"description": "GraphJin - Build APIs in 5 minutes with GraphQL",
"type": "module",
"main": "./wasm/js/graphjin.js",
Expand Down
Binary file modified wasm/graphjin.wasm
Binary file not shown.

1 comment on commit e00878e

@vercel
Copy link

@vercel vercel bot commented on e00878e Jan 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.