/*
Copyright (c) [2023] [squallzhao]
fountain is licensed under APACHE LICENSE, VERSION 2.0.
You can use this software according to the terms and conditions of the APACHE LICENSE, VERSION 2.0.
You may obtain a copy of APACHE LICENSE, VERSION 2.0 at: https://www.apache.org/licenses/LICENSE-2.0
*/
package microservice.resttemplate
import std.sync.*
import std.collection.*
public class RoundRobbin <: ILoadBalance{
var restTemplate: RestTemplate
var index = HashMap<String, AtomicInt64>()
public init(restTemplate: RestTemplate){
this.restTemplate = restTemplate
for(service in restTemplate.getRefer().getRefers()){
index.put(service,AtomicInt64(0))
}
}
public func getServiceAddress(service: String):String{
var arr : Option<ArrayList<String> > = restTemplate.getServiceAddressList(service)
var num = index.get(service).getOrThrow()
var pos = num.fetchAdd(1)
return arr.getOrThrow()[pos % arr.getOrThrow().size]
}
}