Skip to content

Commit 520be07

Browse files
authored
fix: make pooler async on challenge create (#648)
1 parent dd10b3a commit 520be07

File tree

2 files changed

+2
-66
lines changed

2 files changed

+2
-66
lines changed

api/v1/challenge/create.go

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/hex"
77
"fmt"
88
"os"
9-
"sync"
109
"time"
1110

1211
"go.opentelemetry.io/otel/trace"
@@ -20,11 +19,8 @@ import (
2019
"github.com/ctfer-io/chall-manager/global"
2120
errs "github.com/ctfer-io/chall-manager/pkg/errors"
2221
"github.com/ctfer-io/chall-manager/pkg/fs"
23-
"github.com/ctfer-io/chall-manager/pkg/iac"
24-
"github.com/ctfer-io/chall-manager/pkg/identity"
2522
"github.com/ctfer-io/chall-manager/pkg/lock"
2623
"github.com/ctfer-io/chall-manager/pkg/scenario"
27-
"github.com/pkg/errors"
2824
)
2925

3026
func (store *Store) CreateChallenge(ctx context.Context, req *CreateChallengeRequest) (*Challenge, error) {
@@ -129,68 +125,8 @@ func (store *Store) CreateChallenge(ctx context.Context, req *CreateChallengeReq
129125

130126
// 6. Spin up instances if pool is configured. Lock is acquired at challenge level
131127
// hence don't need to be held too.
132-
wg := sync.WaitGroup{}
133-
wg.Add(int(req.Min))
134-
cerr := make(chan error, req.Min)
135128
for range req.Min {
136-
go func() {
137-
defer wg.Done()
138-
139-
id := identity.New()
140-
stack, err := iac.NewStack(ctx, id, fschall)
141-
if err != nil {
142-
cerr <- errors.Wrap(err, "building new stack")
143-
return
144-
}
145-
146-
if err := iac.Additional(ctx, stack, fschall.Additional, req.Additional); err != nil {
147-
cerr <- errors.Wrap(err, "configuring additionals on stack")
148-
return
149-
}
150-
151-
sr, err := stack.Up(ctx)
152-
if err != nil {
153-
cerr <- errors.Wrap(err, "stack up")
154-
return
155-
}
156-
157-
now := time.Now()
158-
fsist := &fs.Instance{
159-
Identity: id,
160-
ChallengeID: req.Id,
161-
Since: now,
162-
LastRenew: now,
163-
Until: common.ComputeUntil(fschall.Until, fschall.Timeout),
164-
Additional: req.Additional,
165-
}
166-
if err := iac.Extract(ctx, stack, sr, fsist); err != nil {
167-
cerr <- errors.Wrap(err, "extracting stack info")
168-
return
169-
}
170-
171-
if err := fsist.Save(); err != nil {
172-
cerr <- errors.Wrap(err, "exporting instance information to filesystem")
173-
return
174-
}
175-
176-
logger.Info(ctx, "instance created successfully")
177-
common.InstancesUDCounter().Add(ctx, 1)
178-
}()
179-
}
180-
wg.Wait()
181-
close(cerr)
182-
183-
var merr error
184-
for err := range cerr {
185-
merr = multierr.Append(merr, err)
186-
}
187-
if merr != nil {
188-
// TODO retry if any problem ? -> resiliency, sagas, cleaner API for challenge/instances mamagement
189-
err := &errs.ErrInternal{Sub: merr}
190-
logger.Error(ctx, "pooling instances",
191-
zap.Error(err),
192-
)
193-
return nil, errs.ErrInternalNoSub
129+
go instance.SpinUp(ctx, req.Id)
194130
}
195131

196132
// 7. Save challenge on filesystem, and respond to API call

deploy/integration/pooler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Test_I_UpdatePooler(t *testing.T) {
6464
require.NoError(t, err)
6565

6666
// Sleep enough just to make sure the pool has time to fill
67-
time.Sleep(10 * time.Second)
67+
time.Sleep(20 * time.Second)
6868

6969
// Create an instance of the challenge (should be fast i.e. <1s)
7070
before := time.Now()

0 commit comments

Comments
 (0)