simplify call to peer
This commit is contained in:
parent
762cc19c14
commit
8e80fa88be
@ -15,7 +15,7 @@ type Peer struct {
|
|||||||
Url string `json:"url,omitempty" bson:"url,omitempty" validate:"required,base64url"`
|
Url string `json:"url,omitempty" bson:"url,omitempty" validate:"required,base64url"`
|
||||||
PublicKey string `json:"public_key,omitempty" bson:"public_key,omitempty"`
|
PublicKey string `json:"public_key,omitempty" bson:"public_key,omitempty"`
|
||||||
Services map[string]int `json:"services,omitempty" bson:"services,omitempty"`
|
Services map[string]int `json:"services,omitempty" bson:"services,omitempty"`
|
||||||
FailedExecution []PeerExecution `json:"failed_execution,omitempty" bson:"failed_execution,omitempty"`
|
FailedExecution []PeerExecution `json:"failed_execution" bson:"failed_execution"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ao *Peer) AddExecution(exec PeerExecution) {
|
func (ao *Peer) AddExecution(exec PeerExecution) {
|
||||||
|
@ -71,8 +71,6 @@ func (p *PeerCache) GetAccessor(caller *tools.HTTPCaller) utils.Accessor {
|
|||||||
|
|
||||||
func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
|
func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
|
||||||
dt utils.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
|
dt utils.DataType, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) (*PeerExecution, error) {
|
||||||
var err error
|
|
||||||
b := []byte{}
|
|
||||||
methods := caller.URLS[dt.String()]
|
methods := caller.URLS[dt.String()]
|
||||||
if _, ok := methods[method]; !ok {
|
if _, ok := methods[method]; !ok {
|
||||||
return nil, errors.New("no path found")
|
return nil, errors.New("no path found")
|
||||||
@ -84,14 +82,15 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
|
|||||||
meth = strings.ReplaceAll(meth, ":id", dataID)
|
meth = strings.ReplaceAll(meth, ":id", dataID)
|
||||||
}
|
}
|
||||||
url := ""
|
url := ""
|
||||||
pexec := &PeerExecution{
|
|
||||||
Method: method.String(),
|
|
||||||
Url: url + methods[method],
|
|
||||||
Body: body,
|
|
||||||
DataType: dt.EnumIndex(),
|
|
||||||
DataID: dataID,
|
|
||||||
}
|
|
||||||
if mypeer, ok := p.checkPeerStatus(peerID, dt.API(), caller); !ok {
|
if mypeer, ok := p.checkPeerStatus(peerID, dt.API(), caller); !ok {
|
||||||
|
pexec := &PeerExecution{
|
||||||
|
Method: method.String(),
|
||||||
|
Url: p.urlFormat((mypeer.Url)+meth, dt),
|
||||||
|
Body: body,
|
||||||
|
DataType: dt.EnumIndex(),
|
||||||
|
DataID: dataID,
|
||||||
|
}
|
||||||
mypeer.AddExecution(*pexec)
|
mypeer.AddExecution(*pexec)
|
||||||
mypeer.GetAccessor(nil).UpdateOne(mypeer, peerID)
|
mypeer.GetAccessor(nil).UpdateOne(mypeer, peerID)
|
||||||
return nil, errors.New("peer is not reachable")
|
return nil, errors.New("peer is not reachable")
|
||||||
@ -102,9 +101,15 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
|
|||||||
mypeer.FailedExecution = []PeerExecution{}
|
mypeer.FailedExecution = []PeerExecution{}
|
||||||
mypeer.GetAccessor(nil).UpdateOne(mypeer, peerID)
|
mypeer.GetAccessor(nil).UpdateOne(mypeer, peerID)
|
||||||
for _, v := range tmp {
|
for _, v := range tmp {
|
||||||
go p.LaunchPeerExecution(peerID, v.DataID, utils.DataType(v.DataType), tools.ToMethod(v.Method), v.Body, caller)
|
go p.exec(v.Url, tools.ToMethod(v.Method), v.Body, caller)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil, p.exec(url, method, body, caller)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PeerCache) exec(url string, method tools.METHOD, body map[string]interface{}, caller *tools.HTTPCaller) error {
|
||||||
|
var b []byte
|
||||||
|
var err error
|
||||||
if method == tools.POST {
|
if method == tools.POST {
|
||||||
b, err = caller.CallPost(url, "", body)
|
b, err = caller.CallPost(url, "", body)
|
||||||
}
|
}
|
||||||
@ -117,12 +122,12 @@ func (p *PeerCache) LaunchPeerExecution(peerID string, dataID string,
|
|||||||
var m map[string]interface{}
|
var m map[string]interface{}
|
||||||
json.Unmarshal(b, &m)
|
json.Unmarshal(b, &m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println("LaunchPeerExecution AFT 3", m, url)
|
fmt.Println("LaunchPeerExecution AFT 3", m, url)
|
||||||
|
|
||||||
if e, ok := m["error"]; !ok && e != "" {
|
if e, ok := m["error"]; !ok && e != "" {
|
||||||
return nil, errors.New(fmt.Sprintf("%v", m["error"]))
|
return errors.New(fmt.Sprintf("%v", m["error"]))
|
||||||
}
|
}
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user