...

Source file src/scheduler/req_timings/req_timings.go

Documentation: scheduler/req_timings

     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 req_timings implements a hashtable for storing timings in a effective way.
    20  package req_timings
    21  
    22  import (
    23  	"fmt"
    24  	"scheduler/hashtable"
    25  	"scheduler/log"
    26  )
    27  
    28  var ht hashtable.ValueHashtable
    29  
    30  func init() {
    31  	ht = hashtable.ValueHashtable{}
    32  }
    33  
    34  func AddTiming(remoteAddress string, timing int64) error {
    35  	// get current values
    36  	values := ht.Get(remoteAddress)
    37  	if values == nil {
    38  		ht.Put(remoteAddress, []int64{timing})
    39  		return nil
    40  	}
    41  
    42  	if valuesArr, ok := values.([]int64); ok {
    43  		valuesArr = append(valuesArr, timing)
    44  		if len(valuesArr) > 2 {
    45  			valuesArr = valuesArr[1:]
    46  		}
    47  		ht.Put(remoteAddress, valuesArr)
    48  	} else {
    49  		log.Log.Errorf("Existing value is not a float arr")
    50  	}
    51  
    52  	return nil
    53  }
    54  
    55  func GetTimings(remoteAddress string) ([]int64, error) {
    56  	values := ht.Get(remoteAddress)
    57  
    58  	if valuesArr, ok := values.([]int64); ok {
    59  		return valuesArr, nil
    60  	}
    61  
    62  	return []int64{}, fmt.Errorf("no timing for address %s", remoteAddress)
    63  }
    64  
    65  func Clear() {
    66  	ht = hashtable.ValueHashtable{}
    67  }
    68  

View as plain text