mirror of
https://github.com/openfaas/faas.git
synced 2025-06-09 00:36:46 +00:00
Update comments
- updates comments and adds where missing - updates locks so that unlock is done via defer instead of at the end of the statement - extracts timeout variable in two places - remove makeClient() unused method from metrics package No-harm changes tested via go build. Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
This commit is contained in:
parent
609ef28cad
commit
bd39b9267a
@ -14,23 +14,25 @@ import (
|
|||||||
"github.com/openfaas/faas/gateway/requests"
|
"github.com/openfaas/faas/gateway/requests"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultMinReplicas is the minimal amount of replicas for a service.
|
const (
|
||||||
const DefaultMinReplicas = 1
|
// DefaultMinReplicas is the minimal amount of replicas for a service.
|
||||||
|
DefaultMinReplicas = 1
|
||||||
|
|
||||||
// DefaultMaxReplicas is the amount of replicas a service will auto-scale up to.
|
// DefaultMaxReplicas is the amount of replicas a service will auto-scale up to.
|
||||||
const DefaultMaxReplicas = 20
|
DefaultMaxReplicas = 20
|
||||||
|
|
||||||
// DefaultScalingFactor is the defining proportion for the scaling increments.
|
// DefaultScalingFactor is the defining proportion for the scaling increments.
|
||||||
const DefaultScalingFactor = 20
|
DefaultScalingFactor = 20
|
||||||
|
|
||||||
// MinScaleLabel label indicating min scale for a function
|
// MinScaleLabel label indicating min scale for a function
|
||||||
const MinScaleLabel = "com.openfaas.scale.min"
|
MinScaleLabel = "com.openfaas.scale.min"
|
||||||
|
|
||||||
// MaxScaleLabel label indicating max scale for a function
|
// MaxScaleLabel label indicating max scale for a function
|
||||||
const MaxScaleLabel = "com.openfaas.scale.max"
|
MaxScaleLabel = "com.openfaas.scale.max"
|
||||||
|
|
||||||
// ScalingFactorLabel label indicates the scaling factor for a function
|
// ScalingFactorLabel label indicates the scaling factor for a function
|
||||||
const ScalingFactorLabel = "com.openfaas.scale.factor"
|
ScalingFactorLabel = "com.openfaas.scale.factor"
|
||||||
|
)
|
||||||
|
|
||||||
// MakeAlertHandler handles alerts from Prometheus Alertmanager
|
// MakeAlertHandler handles alerts from Prometheus Alertmanager
|
||||||
func MakeAlertHandler(service ServiceQuery) http.HandlerFunc {
|
func MakeAlertHandler(service ServiceQuery) http.HandlerFunc {
|
||||||
@ -130,5 +132,6 @@ func CalculateReplicas(status string, currentReplicas uint64, maxReplicas uint64
|
|||||||
} else { // Resolved event.
|
} else { // Resolved event.
|
||||||
newReplicas = minReplicas
|
newReplicas = minReplicas
|
||||||
}
|
}
|
||||||
|
|
||||||
return newReplicas
|
return newReplicas
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ import (
|
|||||||
// functionMatcher parses out the service name (group 1) and rest of path (group 2).
|
// functionMatcher parses out the service name (group 1) and rest of path (group 2).
|
||||||
var functionMatcher = regexp.MustCompile("^/?(?:async-)?function/([^/?]+)([^?]*)")
|
var functionMatcher = regexp.MustCompile("^/?(?:async-)?function/([^/?]+)([^?]*)")
|
||||||
|
|
||||||
// Indicies and meta-data for functionMatcher regex parts
|
// Indices and meta-data for functionMatcher regex parts
|
||||||
const (
|
const (
|
||||||
hasPathCount = 3
|
hasPathCount = 3
|
||||||
routeIndex = 0 // routeIndex corresponds to /function/ or /async-function/
|
routeIndex = 0 // routeIndex corresponds to /function/ or /async-function/
|
||||||
|
@ -31,6 +31,7 @@ type FunctionCache struct {
|
|||||||
// Set replica count for functionName
|
// Set replica count for functionName
|
||||||
func (fc *FunctionCache) Set(functionName string, serviceQueryResponse ServiceQueryResponse) {
|
func (fc *FunctionCache) Set(functionName string, serviceQueryResponse ServiceQueryResponse) {
|
||||||
fc.Sync.Lock()
|
fc.Sync.Lock()
|
||||||
|
defer fc.Sync.Unlock()
|
||||||
|
|
||||||
if _, exists := fc.Cache[functionName]; !exists {
|
if _, exists := fc.Cache[functionName]; !exists {
|
||||||
fc.Cache[functionName] = &FunctionMeta{}
|
fc.Cache[functionName] = &FunctionMeta{}
|
||||||
@ -40,23 +41,23 @@ func (fc *FunctionCache) Set(functionName string, serviceQueryResponse ServiceQu
|
|||||||
entry.LastRefresh = time.Now()
|
entry.LastRefresh = time.Now()
|
||||||
entry.ServiceQueryResponse = serviceQueryResponse
|
entry.ServiceQueryResponse = serviceQueryResponse
|
||||||
|
|
||||||
fc.Sync.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get replica count for functionName
|
// Get replica count for functionName
|
||||||
func (fc *FunctionCache) Get(functionName string) (ServiceQueryResponse, bool) {
|
func (fc *FunctionCache) Get(functionName string) (ServiceQueryResponse, bool) {
|
||||||
|
|
||||||
|
fc.Sync.Lock()
|
||||||
|
defer fc.Sync.Unlock()
|
||||||
|
|
||||||
replicas := ServiceQueryResponse{
|
replicas := ServiceQueryResponse{
|
||||||
AvailableReplicas: 0,
|
AvailableReplicas: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
hit := false
|
hit := false
|
||||||
fc.Sync.Lock()
|
|
||||||
|
|
||||||
if val, exists := fc.Cache[functionName]; exists {
|
if val, exists := fc.Cache[functionName]; exists {
|
||||||
replicas = val.ServiceQueryResponse
|
replicas = val.ServiceQueryResponse
|
||||||
hit = !val.Expired(fc.Expiry)
|
hit = !val.Expired(fc.Expiry)
|
||||||
}
|
}
|
||||||
|
|
||||||
fc.Sync.Unlock()
|
|
||||||
return replicas, hit
|
return replicas, hit
|
||||||
}
|
}
|
||||||
|
@ -13,16 +13,10 @@ import (
|
|||||||
"github.com/openfaas/faas/gateway/requests"
|
"github.com/openfaas/faas/gateway/requests"
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeClient() http.Client {
|
|
||||||
// Fine-tune the client to fail fast.
|
|
||||||
return http.Client{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddMetricsHandler wraps a http.HandlerFunc with Prometheus metrics
|
// AddMetricsHandler wraps a http.HandlerFunc with Prometheus metrics
|
||||||
func AddMetricsHandler(handler http.HandlerFunc, prometheusQuery PrometheusQueryFetcher) http.HandlerFunc {
|
func AddMetricsHandler(handler http.HandlerFunc, prometheusQuery PrometheusQueryFetcher) http.HandlerFunc {
|
||||||
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
// log.Printf("Calling upstream for function info\n")
|
|
||||||
|
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
handler.ServeHTTP(recorder, r)
|
handler.ServeHTTP(recorder, r)
|
||||||
@ -56,7 +50,6 @@ func AddMetricsHandler(handler http.HandlerFunc, prometheusQuery PrometheusQuery
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// log.Printf("Querying Prometheus API\n")
|
|
||||||
expr := url.QueryEscape(`sum(gateway_function_invocation_total{function_name=~".*", code=~".*"}) by (function_name, code)`)
|
expr := url.QueryEscape(`sum(gateway_function_invocation_total{function_name=~".*", code=~".*"}) by (function_name, code)`)
|
||||||
// expr := "sum(gateway_function_invocation_total%7Bfunction_name%3D~%22.*%22%2C+code%3D~%22.*%22%7D)+by+(function_name%2C+code)"
|
// expr := "sum(gateway_function_invocation_total%7Bfunction_name%3D~%22.*%22%2C+code%3D~%22.*%22%7D)+by+(function_name%2C+code)"
|
||||||
results, fetchErr := prometheusQuery.Fetch(expr)
|
results, fetchErr := prometheusQuery.Fetch(expr)
|
||||||
@ -76,7 +69,6 @@ func AddMetricsHandler(handler http.HandlerFunc, prometheusQuery PrometheusQuery
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// log.Printf("Writing bytesOut: %s\n", bytesOut)
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(bytesOut)
|
w.Write(bytesOut)
|
||||||
|
@ -60,11 +60,14 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
|||||||
func (e *Exporter) StartServiceWatcher(endpointURL url.URL, metricsOptions MetricOptions, label string, interval time.Duration) {
|
func (e *Exporter) StartServiceWatcher(endpointURL url.URL, metricsOptions MetricOptions, label string, interval time.Duration) {
|
||||||
ticker := time.NewTicker(interval)
|
ticker := time.NewTicker(interval)
|
||||||
quit := make(chan struct{})
|
quit := make(chan struct{})
|
||||||
|
|
||||||
|
timeout := 3 * time.Second
|
||||||
|
|
||||||
proxyClient := http.Client{
|
proxyClient := http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
Proxy: http.ProxyFromEnvironment,
|
Proxy: http.ProxyFromEnvironment,
|
||||||
DialContext: (&net.Dialer{
|
DialContext: (&net.Dialer{
|
||||||
Timeout: 3 * time.Second,
|
Timeout: timeout,
|
||||||
KeepAlive: 0,
|
KeepAlive: 0,
|
||||||
}).DialContext,
|
}).DialContext,
|
||||||
MaxIdleConns: 1,
|
MaxIdleConns: 1,
|
||||||
|
@ -24,11 +24,13 @@ import (
|
|||||||
|
|
||||||
// NewExternalServiceQuery proxies service queries to external plugin via HTTP
|
// NewExternalServiceQuery proxies service queries to external plugin via HTTP
|
||||||
func NewExternalServiceQuery(externalURL url.URL, credentials *auth.BasicAuthCredentials) handlers.ServiceQuery {
|
func NewExternalServiceQuery(externalURL url.URL, credentials *auth.BasicAuthCredentials) handlers.ServiceQuery {
|
||||||
|
timeout := 3 * time.Second
|
||||||
|
|
||||||
proxyClient := http.Client{
|
proxyClient := http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
Proxy: http.ProxyFromEnvironment,
|
Proxy: http.ProxyFromEnvironment,
|
||||||
DialContext: (&net.Dialer{
|
DialContext: (&net.Dialer{
|
||||||
Timeout: 3 * time.Second,
|
Timeout: timeout,
|
||||||
KeepAlive: 0,
|
KeepAlive: 0,
|
||||||
}).DialContext,
|
}).DialContext,
|
||||||
MaxIdleConns: 1,
|
MaxIdleConns: 1,
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
// Copyright (c) Alex Ellis 2017. All rights reserved.
|
// Copyright (c) Alex Ellis 2017. All rights reserved.
|
||||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
|
||||||
|
// Package requests package provides a client SDK or library for
|
||||||
|
// the OpenFaaS gateway REST API
|
||||||
package requests
|
package requests
|
||||||
|
|
||||||
// CreateFunctionRequest create a function in the swarm.
|
// CreateFunctionRequest create a function in the swarm.
|
||||||
|
@ -51,7 +51,7 @@ func parseIntOrDurationValue(val string, fallback time.Duration) time.Duration {
|
|||||||
return duration
|
return duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read fetches config from environmental variables.
|
// Read fetches gateway server configuration from environmental variables
|
||||||
func (ReadConfig) Read(hasEnv HasEnv) GatewayConfig {
|
func (ReadConfig) Read(hasEnv HasEnv) GatewayConfig {
|
||||||
cfg := GatewayConfig{
|
cfg := GatewayConfig{
|
||||||
PrometheusHost: "prometheus",
|
PrometheusHost: "prometheus",
|
||||||
@ -117,7 +117,7 @@ func (ReadConfig) Read(hasEnv HasEnv) GatewayConfig {
|
|||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
// GatewayConfig for the process.
|
// GatewayConfig provides config for the API Gateway server process
|
||||||
type GatewayConfig struct {
|
type GatewayConfig struct {
|
||||||
|
|
||||||
// HTTP timeout for reading a request from clients.
|
// HTTP timeout for reading a request from clients.
|
||||||
|
@ -1,17 +1,20 @@
|
|||||||
package version
|
package version
|
||||||
|
|
||||||
var (
|
var (
|
||||||
//Version release version of the provider
|
// Version release version of the provider
|
||||||
Version string
|
Version string
|
||||||
//GitCommit SHA of the last git commit
|
|
||||||
|
// GitCommitSHA is the Git SHA of the latest tag/release
|
||||||
GitCommitSHA string
|
GitCommitSHA string
|
||||||
//GitCommit message of the last commit
|
|
||||||
|
// GitCommitMessage as read from the latest tag/release
|
||||||
GitCommitMessage string
|
GitCommitMessage string
|
||||||
//DevVersion string for the development version
|
|
||||||
|
// DevVersion string for the development version
|
||||||
DevVersion = "dev"
|
DevVersion = "dev"
|
||||||
)
|
)
|
||||||
|
|
||||||
//BuildVersion returns current version of the provider
|
// BuildVersion returns current version of the provider
|
||||||
func BuildVersion() string {
|
func BuildVersion() string {
|
||||||
if len(Version) == 0 {
|
if len(Version) == 0 {
|
||||||
return DevVersion
|
return DevVersion
|
||||||
|
Loading…
x
Reference in New Issue
Block a user