@@ -35,7 +35,7 @@ import (
35
35
// New constructs an Enterprise coderd API instance.
36
36
// This handler is designed to wrap the AGPL Coder code and
37
37
// layer Enterprise functionality on top as much as possible.
38
- func New (ctx context.Context , options * Options ) (* API , error ) {
38
+ func New (ctx context.Context , options * Options ) (api * API , err error ) {
39
39
if options .EntitlementsUpdateInterval == 0 {
40
40
options .EntitlementsUpdateInterval = 10 * time .Minute
41
41
}
@@ -52,13 +52,18 @@ func New(ctx context.Context, options *Options) (*API, error) {
52
52
options .Options .Authorizer = rbac .NewCachingAuthorizer (options .PrometheusRegistry )
53
53
}
54
54
ctx , cancelFunc := context .WithCancel (ctx )
55
- api : = & API {
55
+ api = & API {
56
56
ctx : ctx ,
57
57
cancel : cancelFunc ,
58
58
59
59
AGPL : coderd .New (options .Options ),
60
60
Options : options ,
61
61
}
62
+ defer func () {
63
+ if err != nil {
64
+ _ = api .Close ()
65
+ }
66
+ }()
62
67
63
68
api .AGPL .Options .SetUserGroups = api .setUserGroups
64
69
@@ -312,8 +317,12 @@ type API struct {
312
317
313
318
func (api * API ) Close () error {
314
319
api .cancel ()
315
- _ = api .replicaManager .Close ()
316
- _ = api .derpMesh .Close ()
320
+ if api .replicaManager != nil {
321
+ _ = api .replicaManager .Close ()
322
+ }
323
+ if api .derpMesh != nil {
324
+ _ = api .derpMesh .Close ()
325
+ }
317
326
return api .AGPL .Close ()
318
327
}
319
328
0 commit comments