Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
aws.sst.Function: make node builder threadsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
thdxr committed Oct 11, 2024
1 parent 695f091 commit 37812b4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
12 changes: 6 additions & 6 deletions pkg/runtime/node/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ var forceExternal = []string{
}

func (r *Runtime) Build(ctx context.Context, input *runtime.BuildInput) (*runtime.BuildOutput, error) {
r.lock.Acquire(ctx, 1)
defer r.lock.Release(1)
r.concurrency.Acquire(ctx, 1)
defer r.concurrency.Release(1)
var properties NodeProperties
json.Unmarshal(input.Properties, &properties)

Expand Down Expand Up @@ -132,14 +132,14 @@ func (r *Runtime) Build(ctx context.Context, input *runtime.BuildInput) (*runtim
options.Target = properties.ESBuild.Target
}

buildContext, ok := r.contexts[input.FunctionID]
buildContext, ok := r.contexts.Load(input.FunctionID)
if !ok {
buildContext, _ = esbuild.Context(options)
r.contexts[input.FunctionID] = buildContext
r.contexts.Store(input.FunctionID, buildContext)
}

result := buildContext.Rebuild()
r.results[input.FunctionID] = result
result := buildContext.(esbuild.BuildContext).Rebuild()
r.results.Store(input.FunctionID, result)
errors := []string{}
for _, error := range result.Errors {
text := error.Text
Expand Down
14 changes: 7 additions & 7 deletions pkg/runtime/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ var LoaderToString = []string{
}

type Runtime struct {
cfgPath string
contexts map[string]esbuild.BuildContext
results map[string]esbuild.BuildResult
lock *semaphore.Weighted
cfgPath string
contexts sync.Map
results sync.Map
concurrency *semaphore.Weighted
}

func New() *Runtime {
Expand All @@ -70,9 +70,9 @@ func New() *Runtime {
weight, _ = strconv.ParseInt(flag.SST_BUILD_CONCURRENCY, 10, 64)
}
return &Runtime{
contexts: map[string]esbuild.BuildContext{},
results: map[string]esbuild.BuildResult{},
lock: semaphore.NewWeighted(weight),
contexts: sync.Map{},
results: sync.Map{},
concurrency: semaphore.NewWeighted(weight),
}
}

Expand Down

0 comments on commit 37812b4

Please sign in to comment.