Skip to content

Commit 9104010

Browse files
cmagliesilvanocerza
authored andcommitted
Using callback instead of channel+goroutine for Init
1 parent 1bb327c commit 9104010

File tree

8 files changed

+137
-150
lines changed

8 files changed

+137
-150
lines changed

cli/instance/instance.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,12 @@ func Init(instance *rpc.Instance) []*status.Status {
6868
return append(errs, err)
6969
}
7070

71-
initChan, err := commands.Init(&rpc.InitRequest{
72-
Instance: instance,
73-
})
74-
if err != nil {
75-
return append(errs, err)
76-
}
77-
7871
downloadCallback := output.ProgressBar()
7972
taskCallback := output.TaskProgress()
8073

81-
for res := range initChan {
74+
err := commands.Init(&rpc.InitRequest{
75+
Instance: instance,
76+
}, func(res *rpc.InitResponse) {
8277
if err := res.GetError(); err != nil {
8378
errs = append(errs, status.FromProto(err))
8479
}
@@ -91,6 +86,9 @@ func Init(instance *rpc.Instance) []*status.Status {
9186
taskCallback(progress.TaskProgress)
9287
}
9388
}
89+
})
90+
if err != nil {
91+
return append(errs, err)
9492
}
9593

9694
return errs

commands/core/install.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallRequest,
5454
return nil, err
5555
}
5656

57-
_, status := commands.Init(&rpc.InitRequest{Instance: &rpc.Instance{Id: req.Instance.Id}})
57+
status := commands.Init(&rpc.InitRequest{Instance: req.Instance}, nil)
5858
if status != nil {
5959
return nil, status.Err()
6060
}

commands/core/uninstall.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func PlatformUninstall(ctx context.Context, req *rpc.PlatformUninstallRequest, t
6767
}
6868
}
6969

70-
_, status := commands.Init(&rpc.InitRequest{Instance: &rpc.Instance{Id: req.Instance.Id}})
70+
status := commands.Init(&rpc.InitRequest{Instance: req.Instance}, nil)
7171
if status != nil {
7272
return nil, status.Err()
7373
}

commands/core/upgrade.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func PlatformUpgrade(ctx context.Context, req *rpc.PlatformUpgradeRequest,
4949
return nil, err
5050
}
5151

52-
_, status := commands.Init(&rpc.InitRequest{Instance: &rpc.Instance{Id: req.Instance.Id}})
52+
status := commands.Init(&rpc.InitRequest{Instance: req.Instance}, nil)
5353
if status != nil {
5454
return nil, status.Err()
5555
}

commands/daemon/daemon.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -183,21 +183,12 @@ func (s *ArduinoCoreServerImpl) Create(_ context.Context, req *rpc.CreateRequest
183183

184184
// Init FIXMEDOC
185185
func (s *ArduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCoreService_InitServer) error {
186-
messageChan, err := commands.Init(req)
186+
err := commands.Init(req, func(message *rpc.InitResponse) {
187+
stream.Send(message)
188+
})
187189
if err != nil {
188-
return stream.Send(&rpc.InitResponse{
189-
Message: &rpc.InitResponse_Error{
190-
Error: err.Proto(),
191-
},
192-
})
190+
return err.Err()
193191
}
194-
195-
for message := range messageChan {
196-
if err := stream.Send(message); err != nil {
197-
return err
198-
}
199-
}
200-
201192
return nil
202193
}
203194

commands/instances.go

+122-124
Original file line numberDiff line numberDiff line change
@@ -166,172 +166,170 @@ func Create(req *rpc.CreateRequest) (*rpc.CreateResponse, *status.Status) {
166166
// Failures don't stop the loading process, in case of loading failure the Platform or library
167167
// is simply skipped and an error gRPC status is sent in the returned channel.
168168
// Channel is closed when loading is finished.
169-
func Init(req *rpc.InitRequest) (<-chan *rpc.InitResponse, *status.Status) {
169+
func Init(req *rpc.InitRequest, resp func(r *rpc.InitResponse)) *status.Status {
170+
if resp == nil {
171+
resp = func(r *rpc.InitResponse) {}
172+
}
170173
instance := instances[req.Instance.Id]
171174
if instance == nil {
172-
return nil, status.Newf(codes.InvalidArgument, "Invalid instance ID")
175+
return status.Newf(codes.InvalidArgument, "Invalid instance ID")
173176
}
174177

175-
outChan := make(chan *rpc.InitResponse)
176-
go func() {
177-
defer close(outChan)
178-
179-
// Load Platforms
180-
urls := []string{globals.DefaultIndexURL}
181-
urls = append(urls, configuration.Settings.GetStringSlice("board_manager.additional_urls")...)
182-
for _, u := range urls {
183-
URL, err := utils.URLParse(u)
184-
if err != nil {
185-
s := status.Newf(codes.InvalidArgument, "Invalid additional URL: %v", err)
186-
outChan <- &rpc.InitResponse{
187-
Message: &rpc.InitResponse_Error{
188-
Error: s.Proto(),
189-
},
190-
}
191-
continue
192-
}
193-
194-
if URL.Scheme == "file" {
195-
indexFile := paths.New(URL.Path)
178+
// Load Platforms
179+
urls := []string{globals.DefaultIndexURL}
180+
urls = append(urls, configuration.Settings.GetStringSlice("board_manager.additional_urls")...)
181+
for _, u := range urls {
182+
URL, err := utils.URLParse(u)
183+
if err != nil {
184+
s := status.Newf(codes.InvalidArgument, "Invalid additional URL: %v", err)
185+
resp(&rpc.InitResponse{
186+
Message: &rpc.InitResponse_Error{
187+
Error: s.Proto(),
188+
},
189+
})
190+
continue
191+
}
196192

197-
_, err := instance.PackageManager.LoadPackageIndexFromFile(indexFile)
198-
if err != nil {
199-
s := status.Newf(codes.FailedPrecondition, "Loading index file: %v", err)
200-
outChan <- &rpc.InitResponse{
201-
Message: &rpc.InitResponse_Error{
202-
Error: s.Proto(),
203-
},
204-
}
205-
}
206-
continue
207-
}
193+
if URL.Scheme == "file" {
194+
indexFile := paths.New(URL.Path)
208195

209-
if err := instance.PackageManager.LoadPackageIndex(URL); err != nil {
196+
_, err := instance.PackageManager.LoadPackageIndexFromFile(indexFile)
197+
if err != nil {
210198
s := status.Newf(codes.FailedPrecondition, "Loading index file: %v", err)
211-
outChan <- &rpc.InitResponse{
199+
resp(&rpc.InitResponse{
212200
Message: &rpc.InitResponse_Error{
213201
Error: s.Proto(),
214202
},
215-
}
203+
})
216204
}
205+
continue
217206
}
218207

219-
// We load hardware before verifying builtin tools are installed
220-
// otherwise we wouldn't find them and reinstall them each time
221-
// and they would never get reloaded.
222-
for _, err := range instance.PackageManager.LoadHardware() {
223-
outChan <- &rpc.InitResponse{
208+
if err := instance.PackageManager.LoadPackageIndex(URL); err != nil {
209+
s := status.Newf(codes.FailedPrecondition, "Loading index file: %v", err)
210+
resp(&rpc.InitResponse{
224211
Message: &rpc.InitResponse_Error{
225-
Error: err.Proto(),
212+
Error: s.Proto(),
226213
},
227-
}
214+
})
228215
}
216+
}
229217

230-
taskCallback := func(msg *rpc.TaskProgress) {
231-
outChan <- &rpc.InitResponse{
232-
Message: &rpc.InitResponse_InitProgress{
233-
InitProgress: &rpc.InitResponse_Progress{
234-
TaskProgress: msg,
235-
},
218+
// We load hardware before verifying builtin tools are installed
219+
// otherwise we wouldn't find them and reinstall them each time
220+
// and they would never get reloaded.
221+
for _, err := range instance.PackageManager.LoadHardware() {
222+
resp(&rpc.InitResponse{
223+
Message: &rpc.InitResponse_Error{
224+
Error: err.Proto(),
225+
},
226+
})
227+
}
228+
229+
taskCallback := func(msg *rpc.TaskProgress) {
230+
resp(&rpc.InitResponse{
231+
Message: &rpc.InitResponse_InitProgress{
232+
InitProgress: &rpc.InitResponse_Progress{
233+
TaskProgress: msg,
236234
},
237-
}
238-
}
235+
},
236+
})
237+
}
239238

240-
downloadCallback := func(msg *rpc.DownloadProgress) {
241-
outChan <- &rpc.InitResponse{
242-
Message: &rpc.InitResponse_InitProgress{
243-
InitProgress: &rpc.InitResponse_Progress{
244-
DownloadProgress: msg,
245-
},
239+
downloadCallback := func(msg *rpc.DownloadProgress) {
240+
resp(&rpc.InitResponse{
241+
Message: &rpc.InitResponse_InitProgress{
242+
InitProgress: &rpc.InitResponse_Progress{
243+
DownloadProgress: msg,
246244
},
247-
}
248-
}
245+
},
246+
})
247+
}
249248

250-
// Install tools if necessary
251-
toolHasBeenInstalled := false
252-
ctagsTool, err := getBuiltinCtagsTool(instance.PackageManager)
249+
// Install tools if necessary
250+
toolHasBeenInstalled := false
251+
ctagsTool, err := getBuiltinCtagsTool(instance.PackageManager)
252+
if err != nil {
253+
s := status.Newf(codes.Internal, err.Error())
254+
resp(&rpc.InitResponse{
255+
Message: &rpc.InitResponse_Error{
256+
Error: s.Proto(),
257+
},
258+
})
259+
} else {
260+
ctagsInstalled, err := instance.installToolIfMissing(ctagsTool, downloadCallback, taskCallback)
261+
toolHasBeenInstalled = toolHasBeenInstalled || ctagsInstalled
253262
if err != nil {
254263
s := status.Newf(codes.Internal, err.Error())
255-
outChan <- &rpc.InitResponse{
264+
resp(&rpc.InitResponse{
256265
Message: &rpc.InitResponse_Error{
257266
Error: s.Proto(),
258267
},
259-
}
260-
} else {
261-
ctagsInstalled, err := instance.installToolIfMissing(ctagsTool, downloadCallback, taskCallback)
262-
toolHasBeenInstalled = toolHasBeenInstalled || ctagsInstalled
263-
if err != nil {
264-
s := status.Newf(codes.Internal, err.Error())
265-
outChan <- &rpc.InitResponse{
266-
Message: &rpc.InitResponse_Error{
267-
Error: s.Proto(),
268-
},
269-
}
270-
}
268+
})
271269
}
270+
}
272271

273-
serialDiscoveryTool, _ := getBuiltinSerialDiscoveryTool(instance.PackageManager)
272+
serialDiscoveryTool, _ := getBuiltinSerialDiscoveryTool(instance.PackageManager)
273+
if err != nil {
274+
s := status.Newf(codes.Internal, err.Error())
275+
resp(&rpc.InitResponse{
276+
Message: &rpc.InitResponse_Error{
277+
Error: s.Proto(),
278+
},
279+
})
280+
} else {
281+
serialDiscoveryToolInstalled, err := instance.installToolIfMissing(serialDiscoveryTool, downloadCallback, taskCallback)
282+
toolHasBeenInstalled = toolHasBeenInstalled || serialDiscoveryToolInstalled
274283
if err != nil {
275284
s := status.Newf(codes.Internal, err.Error())
276-
outChan <- &rpc.InitResponse{
285+
resp(&rpc.InitResponse{
277286
Message: &rpc.InitResponse_Error{
278287
Error: s.Proto(),
279288
},
280-
}
281-
} else {
282-
serialDiscoveryToolInstalled, err := instance.installToolIfMissing(serialDiscoveryTool, downloadCallback, taskCallback)
283-
toolHasBeenInstalled = toolHasBeenInstalled || serialDiscoveryToolInstalled
284-
if err != nil {
285-
s := status.Newf(codes.Internal, err.Error())
286-
outChan <- &rpc.InitResponse{
287-
Message: &rpc.InitResponse_Error{
288-
Error: s.Proto(),
289-
},
290-
}
291-
}
292-
}
293-
294-
if toolHasBeenInstalled {
295-
// We installed at least one new tool after loading hardware
296-
// so we must reload again otherwise we would never found them.
297-
for _, err := range instance.PackageManager.LoadHardware() {
298-
outChan <- &rpc.InitResponse{
299-
Message: &rpc.InitResponse_Error{
300-
Error: err.Proto(),
301-
},
302-
}
303-
}
304-
}
305-
306-
// Load libraries
307-
for _, pack := range instance.PackageManager.Packages {
308-
for _, platform := range pack.Platforms {
309-
if platformRelease := instance.PackageManager.GetInstalledPlatformRelease(platform); platformRelease != nil {
310-
instance.lm.AddPlatformReleaseLibrariesDir(platformRelease, libraries.PlatformBuiltIn)
311-
}
312-
}
289+
})
313290
}
291+
}
314292

315-
if err := instance.lm.LoadIndex(); err != nil {
316-
s := status.Newf(codes.FailedPrecondition, "Loading index file: %v", err)
317-
outChan <- &rpc.InitResponse{
293+
if toolHasBeenInstalled {
294+
// We installed at least one new tool after loading hardware
295+
// so we must reload again otherwise we would never found them.
296+
for _, err := range instance.PackageManager.LoadHardware() {
297+
resp(&rpc.InitResponse{
318298
Message: &rpc.InitResponse_Error{
319-
Error: s.Proto(),
299+
Error: err.Proto(),
320300
},
321-
}
301+
})
322302
}
303+
}
323304

324-
for _, err := range instance.lm.RescanLibraries() {
325-
s := status.Newf(codes.FailedPrecondition, "Loading libraries: %v", err)
326-
outChan <- &rpc.InitResponse{
327-
Message: &rpc.InitResponse_Error{
328-
Error: s.Proto(),
329-
},
305+
// Load libraries
306+
for _, pack := range instance.PackageManager.Packages {
307+
for _, platform := range pack.Platforms {
308+
if platformRelease := instance.PackageManager.GetInstalledPlatformRelease(platform); platformRelease != nil {
309+
instance.lm.AddPlatformReleaseLibrariesDir(platformRelease, libraries.PlatformBuiltIn)
330310
}
331311
}
332-
}()
312+
}
333313

334-
return outChan, nil
314+
if err := instance.lm.LoadIndex(); err != nil {
315+
s := status.Newf(codes.FailedPrecondition, "Loading index file: %v", err)
316+
resp(&rpc.InitResponse{
317+
Message: &rpc.InitResponse_Error{
318+
Error: s.Proto(),
319+
},
320+
})
321+
}
322+
323+
for _, err := range instance.lm.RescanLibraries() {
324+
s := status.Newf(codes.FailedPrecondition, "Loading libraries: %v", err)
325+
resp(&rpc.InitResponse{
326+
Message: &rpc.InitResponse_Error{
327+
Error: s.Proto(),
328+
},
329+
})
330+
}
331+
332+
return nil
335333
}
336334

337335
// Destroy FIXMEDOC

commands/lib/install.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func LibraryInstall(ctx context.Context, req *rpc.LibraryInstallRequest,
7777
}
7878
}
7979

80-
_, status := commands.Init(&rpc.InitRequest{Instance: &rpc.Instance{Id: req.Instance.Id}})
80+
status := commands.Init(&rpc.InitRequest{Instance: req.Instance}, nil)
8181
if status != nil {
8282
return fmt.Errorf("rescanning libraries: %s", status.Err())
8383
}

commands/lib/upgrade.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func LibraryUpgradeAll(instanceID int32, downloadCB commands.DownloadProgressCB,
3333
return err
3434
}
3535

36-
_, status := commands.Init(&rpc.InitRequest{Instance: &rpc.Instance{Id: instanceID}})
36+
status := commands.Init(&rpc.InitRequest{Instance: &rpc.Instance{Id: instanceID}}, nil)
3737
if status != nil {
3838
return fmt.Errorf("rescanning libraries: %s", status.Err())
3939
}

0 commit comments

Comments
 (0)