mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 13:41:37 +00:00
oracle: process requests concurrently
This commit is contained in:
parent
43e4d3af88
commit
25d734cbad
3 changed files with 46 additions and 7 deletions
|
@ -7,6 +7,7 @@ type OracleConfiguration struct {
|
|||
Enabled bool `yaml:"Enabled"`
|
||||
AllowPrivateHost bool `yaml:"AllowPrivateHost"`
|
||||
Nodes []string `yaml:"Nodes"`
|
||||
MaxConcurrentRequests int `yaml:"MaxConcurrentRequests"`
|
||||
RequestTimeout time.Duration `yaml:"RequestTimeout"`
|
||||
ResponseTimeout time.Duration `yaml:"ResponseTimeout"`
|
||||
UnlockWallet Wallet `yaml:"UnlockWallet"`
|
||||
|
|
|
@ -34,6 +34,7 @@ type (
|
|||
oracleSignContract []byte
|
||||
|
||||
close chan struct{}
|
||||
requestCh chan request
|
||||
requestMap chan map[uint64]*state.OracleRequest
|
||||
|
||||
// respMtx protects responses map.
|
||||
|
@ -93,6 +94,10 @@ func NewOracle(cfg Config) (*Oracle, error) {
|
|||
if o.MainCfg.RequestTimeout == 0 {
|
||||
o.MainCfg.RequestTimeout = defaultRequestTimeout
|
||||
}
|
||||
if o.MainCfg.MaxConcurrentRequests == 0 {
|
||||
o.MainCfg.MaxConcurrentRequests = defaultMaxConcurrentRequests
|
||||
}
|
||||
o.requestCh = make(chan request, o.MainCfg.MaxConcurrentRequests)
|
||||
|
||||
var err error
|
||||
w := cfg.MainCfg.UnlockWallet
|
||||
|
@ -136,12 +141,20 @@ func (o *Oracle) Shutdown() {
|
|||
|
||||
// Run runs must be executed in a separate goroutine.
|
||||
func (o *Oracle) Run() {
|
||||
for i := 0; i < o.MainCfg.MaxConcurrentRequests; i++ {
|
||||
go o.runRequestWorker()
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-o.close:
|
||||
return
|
||||
case reqs := <-o.requestMap:
|
||||
o.ProcessRequestsInternal(reqs)
|
||||
for id, req := range reqs {
|
||||
o.requestCh <- request{
|
||||
ID: id,
|
||||
Req: req,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,31 @@ import (
|
|||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const defaultMaxConcurrentRequests = 10
|
||||
|
||||
type request struct {
|
||||
ID uint64
|
||||
Req *state.OracleRequest
|
||||
}
|
||||
|
||||
func (o *Oracle) runRequestWorker() {
|
||||
for {
|
||||
select {
|
||||
case <-o.close:
|
||||
return
|
||||
case req := <-o.requestCh:
|
||||
acc := o.getAccount()
|
||||
if acc == nil {
|
||||
continue
|
||||
}
|
||||
err := o.processRequest(acc.PrivateKey(), req.ID, req.Req)
|
||||
if err != nil {
|
||||
o.Log.Debug("can't process request", zap.Uint64("id", req.ID), zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveRequests removes all data associated with requests
|
||||
// which have been processed by oracle contract.
|
||||
func (o *Oracle) RemoveRequests(ids []uint64) {
|
||||
|
|
Loading…
Reference in a new issue