...

Source file src/scheduler/scheduler_service/api_peer.go

Documentation: scheduler/scheduler_service

     1  /*
     2   * P2PFaaS - A framework for FaaS Load Balancing
     3   * Copyright (c) 2019 - 2022. Gabriele Proietti Mattia <pm.gabriele@outlook.com>
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17   */
    18  
    19  package scheduler_service
    20  
    21  import (
    22  	"encoding/json"
    23  	"io/ioutil"
    24  	"scheduler/log"
    25  	"scheduler/types"
    26  	"scheduler/utils"
    27  )
    28  
    29  func peerFunctionApiCall(host string, peerRequest *types.PeerJobRequest) (*APIResponse, error) {
    30  	payload, err := json.Marshal(peerRequest)
    31  	if err != nil {
    32  		log.Log.Errorf("Cannot encode to json payload")
    33  		return nil, err
    34  	}
    35  
    36  	log.Log.Debugf("Calling POST to %s", GetPeerFunctionUrl(host, peerRequest.FunctionName))
    37  	// log.Log.Debugf("len(payload)=%d len(peers)=%d content_type=%s", len(payload), len(peerRequest.PeersList), peerRequest.PayloadContentType)
    38  
    39  	// prepare headers
    40  	var headers *[]utils.HttpHeader = nil
    41  	if peerRequest.ServiceIdTracing != "" {
    42  		headers = &[]utils.HttpHeader{
    43  			{Key: utils.HttpHeaderP2PFaaSSchedulerTracingId, Value: peerRequest.ServiceIdTracing},
    44  		}
    45  	}
    46  
    47  	res, err := utils.HttpMachinePostJSONWithHeaders(GetPeerFunctionUrl(host, peerRequest.FunctionName), string(payload), headers)
    48  	if err != nil {
    49  		log.Log.Errorf("Cannot create POST peerRequest to %s: %s", GetPeerFunctionUrl(host, peerRequest.FunctionName), err.Error())
    50  		return nil, err
    51  	}
    52  	defer res.Body.Close()
    53  
    54  	body, _ := ioutil.ReadAll(res.Body)
    55  	response := APIResponse{
    56  		Headers:    res.Header,
    57  		Body:       body,
    58  		StatusCode: res.StatusCode,
    59  	}
    60  
    61  	return &response, err
    62  }
    63  

View as plain text