...

Source file src/scheduler/faas_containers/services_generic.go

Documentation: scheduler/faas_containers

     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 faas_containers
    20  
    21  import (
    22  	"io/ioutil"
    23  	"scheduler/log"
    24  	"scheduler/types"
    25  	"scheduler/utils"
    26  )
    27  
    28  func GenFunctionExecute(functionName string, payload []byte, contentType string) (*types.FaasApiResponse, error) {
    29  	var res *types.FaasApiResponse
    30  	var err error
    31  
    32  	if payload == nil {
    33  		res, err = functionExecuteApiCall(functionName)
    34  	} else {
    35  		res, err = functionExecutePostApiCall(functionName, payload, contentType)
    36  	}
    37  
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	if res.StatusCode == 404 {
    42  		return res, ErrorFunctionNotFound{}
    43  	}
    44  	if res.StatusCode >= 500 {
    45  		return res, ErrorInternal{string(res.Body)}
    46  	}
    47  	if res.StatusCode < 200 || res.StatusCode >= 300 {
    48  		return res, ErrorGeneric{string(res.Body)}
    49  	}
    50  
    51  	return res, nil
    52  }
    53  
    54  /*
    55   * Utils
    56   */
    57  
    58  func functionExecuteApiCall(functionName string) (*types.FaasApiResponse, error) {
    59  	res, err := utils.HttpGet(GetApiFunctionUrl(functionName))
    60  	if err != nil {
    61  		log.Log.Debugf("Cannot create GET request to %s", err.Error(), GetApiFunctionUrl(functionName))
    62  		return nil, err
    63  	}
    64  
    65  	body, _ := ioutil.ReadAll(res.Body)
    66  	_ = res.Body.Close()
    67  
    68  	response := types.FaasApiResponse{
    69  		Headers:    res.Header,
    70  		Body:       body,
    71  		StatusCode: res.StatusCode,
    72  	}
    73  
    74  	return &response, err
    75  }
    76  
    77  func functionExecutePostApiCall(functionName string, payload []byte, contentType string) (*types.FaasApiResponse, error) {
    78  	res, err := utils.HttpPost(GetApiFunctionUrl(functionName), payload, contentType)
    79  	if err != nil {
    80  		log.Log.Debugf("Cannot create POST request to %s", err.Error(), GetApiFunctionUrl(functionName))
    81  		return nil, err
    82  	}
    83  
    84  	body, _ := ioutil.ReadAll(res.Body)
    85  	_ = res.Body.Close()
    86  
    87  	response := types.FaasApiResponse{
    88  		Headers:    res.Header,
    89  		Body:       body,
    90  		StatusCode: res.StatusCode,
    91  	}
    92  
    93  	return &response, err
    94  }
    95  

View as plain text