...

Source file src/scheduler/faas_openfaas/faas.go

Documentation: scheduler/faas_openfaas

     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_openfaas implements a faas execution logic based on the OpenFaaS framework
    20  package faas_openfaas
    21  
    22  import (
    23  	"bytes"
    24  	"encoding/base64"
    25  	"fmt"
    26  	"net"
    27  	"net/http"
    28  	"scheduler/config"
    29  	"scheduler/log"
    30  	"time"
    31  )
    32  
    33  var httpTransport *http.Transport
    34  
    35  func init() {
    36  	httpTransport = &http.Transport{
    37  		MaxIdleConnsPerHost: 8,
    38  		MaxIdleConns:        24,
    39  		IdleConnTimeout:     64,
    40  		DisableKeepAlives:   false,
    41  		DialContext: (&net.Dialer{
    42  			Timeout:   30 * time.Second,
    43  			KeepAlive: 120 * time.Second,
    44  		}).DialContext,
    45  	}
    46  }
    47  
    48  /*
    49   * APIs
    50   */
    51  
    52  func GetApiUrl(host string) string {
    53  	return fmt.Sprintf("http://%s:%d", host, config.GetOpenFaasListeningPort())
    54  }
    55  
    56  func GetApiSystemFunctionsUrl(host string) string {
    57  	return fmt.Sprintf("%s/system/functions", GetApiUrl(host))
    58  }
    59  
    60  func GetApiFunctionUrl(host string, functionName string) string {
    61  	return fmt.Sprintf("%s/function/%s", GetApiUrl(host), functionName)
    62  }
    63  
    64  func GetApiSystemFunctionUrl(host string, functionName string) string {
    65  	return fmt.Sprintf("%s/system/function/%s", GetApiUrl(host), functionName)
    66  }
    67  
    68  func GetApiScaleFunction(host string, functionName string) string {
    69  	return fmt.Sprintf("%s/system/scale-function/%s", GetApiUrl(host), functionName)
    70  }
    71  
    72  /*
    73   * Http utils
    74   */
    75  
    76  func SetAuthHeader(req *http.Request) {
    77  	auth := config.OpenFaaSUsername + ":" + config.OpenFaaSPassword
    78  	req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
    79  }
    80  
    81  /*
    82   * Http methods
    83   */
    84  
    85  type ErrorHttpCannotCreateRequest struct{}
    86  
    87  func (e ErrorHttpCannotCreateRequest) Error() string {
    88  	return "cannot create http request."
    89  }
    90  
    91  func HttpPostJSON(url string, json string) (*http.Response, error) {
    92  	req, err := http.NewRequest("POST", url, bytes.NewBufferString(json))
    93  	if err != nil {
    94  		return nil, ErrorHttpCannotCreateRequest{}
    95  	}
    96  
    97  	req.Header.Set("Content-Type", "application/json")
    98  	SetAuthHeader(req)
    99  
   100  	client := &http.Client{Transport: httpTransport}
   101  	res, err := client.Do(req)
   102  	if err != nil {
   103  		log.Log.Debugf("cannot POST to %s: %s", url, err.Error())
   104  	}
   105  
   106  	return res, err
   107  }
   108  
   109  func HttpGet(url string) (*http.Response, error) {
   110  	req, err := http.NewRequest("GET", url, nil)
   111  	if err != nil {
   112  		return nil, ErrorHttpCannotCreateRequest{}
   113  	}
   114  
   115  	SetAuthHeader(req)
   116  
   117  	client := &http.Client{Transport: httpTransport}
   118  	res, err := client.Do(req)
   119  	if err != nil {
   120  		log.Log.Debugf("cannot GET to %s: %s", url, err.Error())
   121  	}
   122  
   123  	return res, err
   124  }
   125  
   126  func HttpPost(url string, payload []byte, contentType string) (*http.Response, error) {
   127  	req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
   128  	if err != nil {
   129  		return nil, ErrorHttpCannotCreateRequest{}
   130  	}
   131  
   132  	if contentType != "" {
   133  		req.Header.Set("Content-Type", contentType)
   134  	}
   135  
   136  	SetAuthHeader(req)
   137  
   138  	client := &http.Client{Transport: httpTransport}
   139  	res, err := client.Do(req)
   140  	if err != nil {
   141  		log.Log.Debugf("cannot POST to %s: %s", url, err.Error())
   142  	}
   143  
   144  	return res, err
   145  }
   146  

View as plain text