mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-08-07 07:14:50 +00:00
NOISSUE - Optimize context deadline and error handling (#34)
Improved the timing logic for setting computation deadlines by replacing the channel-based pattern with a direct time addition. This change enhances the performance by preventing unnecessary channel operations. Additionally, relocated the select block that checks for computation timeout and data reception errors. This alteration ensures that the socket connection acceptance routine starts before any timeouts or errors are checked, thereby avoiding potential race conditions and making the code more resilient to early timeout scenarios. Signed-off-by: SammyOina <sammyoina@gmail.com>
This commit is contained in:
committed by
GitHub
parent
7ea8e48dbb
commit
ee1f5ebb3d
+9
-9
@@ -213,7 +213,7 @@ func (as *agentService) runComputation() {
|
||||
defer as.sm.SendEvent(runComplete)
|
||||
var cancel context.CancelFunc
|
||||
if as.computation.Timeout.Duration != 0 {
|
||||
ctx, cancel = context.WithDeadline(ctx, <-time.After(as.computation.Timeout.Duration))
|
||||
ctx, cancel = context.WithDeadline(ctx, time.Now().Add(as.computation.Timeout.Duration))
|
||||
defer cancel()
|
||||
}
|
||||
result, err := run(ctx, as.algorithms[0], as.datasets[0])
|
||||
@@ -247,13 +247,6 @@ func run(ctx context.Context, algoContent, dataContent []byte) ([]byte, error) {
|
||||
errorChannel := make(chan error)
|
||||
|
||||
var result []byte
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, errors.New("computation timed out")
|
||||
case result = <-dataChannel:
|
||||
case err = <-errorChannel:
|
||||
return nil, fmt.Errorf("error receiving data: %v", err)
|
||||
}
|
||||
|
||||
go socket.AcceptConnection(listener, dataChannel, errorChannel)
|
||||
|
||||
@@ -270,5 +263,12 @@ func run(ctx context.Context, algoContent, dataContent []byte) ([]byte, error) {
|
||||
return nil, fmt.Errorf("python script execution error: %v", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, errors.New("computation timed out")
|
||||
case result = <-dataChannel:
|
||||
return result, nil
|
||||
case err = <-errorChannel:
|
||||
return nil, fmt.Errorf("error receiving data: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user