Skip to content

Commit

Permalink
Working runtime (with integration tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivanshVij committed Aug 31, 2023
1 parent 6f9de17 commit dd9bfe6
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 36 deletions.
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Config[T signature.Signature] struct {
newSignature signature.New[T]
functions []configFunction
context context.Context
pooling bool
}

// NewConfig returns a new Scale Runtime Config
Expand Down Expand Up @@ -110,6 +111,11 @@ func (c *Config[T]) WithContext(ctx context.Context) *Config[T] {
return c
}

func (c *Config[T]) WithPooling(pooling bool) *Config[T] {
c.pooling = pooling
return c
}

// validEnv returns true if the string is valid for use as an environment variable
func validEnv(str string) bool {
return !envStringRegex.MatchString(str)
Expand Down
19 changes: 4 additions & 15 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,16 @@ func (f *function[T]) runWithModule(ctx context.Context, moduleInstance *moduleI
}

func (f *function[T]) run(ctx context.Context, signature T, instance *Instance[T]) error {
var module *moduleInstance[T]
var err error
if f.scaleFunc.Stateless {
module, err = f.modulePool.Get()
if err != nil {
return fmt.Errorf("failed to get module from pool for function %s: %w", f.scaleFunc.Name, err)
}
} else {
module, err = newModuleInstance[T](ctx, f.runtime, f, instance)
if err != nil {
return fmt.Errorf("failed to create module for function %s: %w", f.scaleFunc.Name, err)
}
module, err := f.modulePool.Get()
if err != nil {
return fmt.Errorf("failed to get module from pool for function %s: %w", f.scaleFunc.Name, err)
}

module.init(f.runtime, instance)
module.setSignature(signature)

err = f.runWithModule(ctx, module)
if f.scaleFunc.Stateless {
module.cleanup(f.runtime)
}
module.cleanup(f.runtime)
if err != nil {
return fmt.Errorf("error while running function '%s': %w", f.scaleFunc.Name, err)
}
Expand Down
10 changes: 7 additions & 3 deletions instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Instance[T signature.Signature] struct {
next Next[T]
}

func newInstance[T signature.Signature](r *Scale[T], next ...Next[T]) (*Instance[T], error) {
func newInstanceSetup[T signature.Signature](r *Scale[T], next ...Next[T]) (*Instance[T], error) {
i := &Instance[T]{
runtime: r,
instanceID: make([]byte, 16),
Expand All @@ -54,8 +54,8 @@ func newInstance[T signature.Signature](r *Scale[T], next ...Next[T]) (*Instance
return i, nil
}

func newPersistentInstance[T signature.Signature](ctx context.Context, r *Scale[T], next ...Next[T]) (*Instance[T], error) {
i, err := newInstance[T](r, next...)
func newInstance[T signature.Signature](ctx context.Context, r *Scale[T], next ...Next[T]) (*Instance[T], error) {
i, err := newInstanceSetup[T](r, next...)
if err != nil {
return nil, err
}
Expand All @@ -68,6 +68,10 @@ func newPersistentInstance[T signature.Signature](ctx context.Context, r *Scale[
return i, nil
}

func newPoolingInstance[T signature.Signature](r *Scale[T], next ...Next[T]) (*Instance[T], error) {
return newInstanceSetup[T](r, next...)
}

func (i *Instance[T]) Run(ctx context.Context, signature T) error {
if i.head != nil {
i.head.setSignature(signature)
Expand Down
10 changes: 4 additions & 6 deletions scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ func New[T signature.Signature](config *Config[T]) (*Scale[T], error) {
// Instance returns a new instance of a Scale Function chain
// with the provided and optional next function.
func (r *Scale[T]) Instance(next ...Next[T]) (*Instance[T], error) {
return newInstance(r, next...)
}
if r.config.pooling {
return newPoolingInstance(r, next...)
}

// PersistentInstance returns a new persistent instance of a Scale Function chain
// with the provided context and optional next function.
func (r *Scale[T]) PersistentInstance(ctx context.Context, next ...Next[T]) (*Instance[T], error) {
return newPersistentInstance(ctx, r, next...)
return newInstance(r.config.context, r, next...)
}

func (r *Scale[T]) init() error {
Expand Down
1 change: 0 additions & 1 deletion scalefile/scalefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ type Schema struct {
Signature SignatureSchema `hcl:"signature,block"`
Function string `hcl:"function,attr"`
Initialize string `hcl:"initialize,attr"`
Stateless bool `hcl:"stateless,optional"`
Description string `hcl:"description,optional"`
}

Expand Down
10 changes: 0 additions & 10 deletions scalefunc/scalefunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ type Schema struct {
SignatureHash string `json:"signature_hash" yaml:"signature_hash"`
Language Language `json:"language" yaml:"language"`
Dependencies []Dependency `json:"dependencies" yaml:"dependencies"`
Stateless bool `json:"stateless" yaml:"stateless"`
Function []byte `json:"function" yaml:"function"`
Size uint32 `json:"size" yaml:"size"`
Hash string `json:"hash" yaml:"hash"`
Expand Down Expand Up @@ -128,8 +127,6 @@ func (s *Schema) Encode() []byte {
}
}

e.Bool(s.Stateless)

e.Bytes(s.Function)

size := uint32(len(b.Bytes()))
Expand Down Expand Up @@ -245,11 +242,6 @@ func (s *Schema) Decode(data []byte) error {
}
}

s.Stateless, err = d.Bool()
if err != nil {
s.Stateless = false
}

s.Function, err = d.Bytes(nil)
if err != nil {
return err
Expand Down Expand Up @@ -304,8 +296,6 @@ func (s *Schema) GetHash() []byte {
}
}

e.Bool(s.Stateless)

e.Bytes(s.Function)

hash := sha256.New()
Expand Down
2 changes: 1 addition & 1 deletion signature/generator/rust/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func New() (*Formatter, error) {
return nil, err
}

cfg := scale.NewConfig(signature.New).WithFunction(s)
cfg := scale.NewConfig(signature.New).WithFunction(s).WithPooling(true)
runtime, err := scale.New(cfg)
if err != nil {
return nil, err
Expand Down

0 comments on commit dd9bfe6

Please sign in to comment.