Skip to content

Commit

Permalink
fix: handled goroutine exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashk767 committed Mar 19, 2024
1 parent feb1cfc commit a32cde4
Showing 1 changed file with 44 additions and 16 deletions.
60 changes: 44 additions & 16 deletions utils/struct-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/ecdsa"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"math/big"
Expand All @@ -29,7 +30,6 @@ import (
)

var RPCTimeout int64
var HTTPTimeout int64

func StartRazor(optionsPackageStruct OptionsPackageStruct) Utils {
UtilsInterface = optionsPackageStruct.UtilsInterface
Expand Down Expand Up @@ -59,30 +59,58 @@ func StartRazor(optionsPackageStruct OptionsPackageStruct) Utils {
}

func InvokeFunctionWithTimeout(interfaceName interface{}, methodName string, args ...interface{}) []reflect.Value {
var functionCall []reflect.Value
var gotFunction = make(chan bool)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(RPCTimeout)*time.Second)
defer cancel()

resultChan := make(chan []reflect.Value)
errChan := make(chan error)

go func() {
defer close(resultChan)
defer close(errChan)

inputs := make([]reflect.Value, len(args))
for i := range args {
inputs[i] = reflect.ValueOf(args[i])
for i, arg := range args {
inputs[i] = reflect.ValueOf(arg)
}
log.Debug("Blockchain function: ", methodName)
functionCall = reflect.ValueOf(interfaceName).MethodByName(methodName).Call(inputs)
gotFunction <- true
}()
for {

log.Debug("Invoking blockchain function: ", methodName)

// Attempt to call the function
var result []reflect.Value
func() {
defer func() {
if r := recover(); r != nil {
errChan <- errors.New(fmt.Sprintf("panic during function call: %v", r))

Check failure on line 84 in utils/struct-utils.go

View workflow job for this annotation

GitHub Actions / test

S1028: should use fmt.Errorf(...) instead of errors.New(fmt.Sprintf(...)) (gosimple)
}
}()
result = reflect.ValueOf(interfaceName).MethodByName(methodName).Call(inputs)
}()

// Send the result back or indicate the function has completed
select {
case <-ctx.Done():
log.Errorf("%s function timeout!", methodName)
log.Debug("Kindly check your connection")
return nil

case <-gotFunction:
return functionCall
// If the context is done, return the context-related error
errChan <- ctx.Err()
default:
resultChan <- result
}
}()

select {
case <-ctx.Done():
log.Errorf("%s function timeout!", methodName)
log.Debug("Please check your connection")
return nil

case err := <-errChan:
// Handle any errors that occurred during the function call
log.Errorf("Error:%v occurred in %s function!", err, methodName)
return nil

case result := <-resultChan:
// Function completed successfully
return result
}
}

Expand Down

0 comments on commit a32cde4

Please sign in to comment.