4267a229创建于 2024年8月2日历史提交
/*
    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.web.limit
import std.sync.*
import std.time.*

public class Limit{
    let count = AtomicInt64(0)
    var max = 0
    public init(max: Int64){
        this.max = max
    }

    public func incSync(): Unit{
       while (inc() == false){
          sleep(10*Duration.millisecond)  
       }
    }

    public func inc(): Bool{
        if (count.load() >= this.max){
           return false;
        }
        count.fetchAdd(1)
        return true
    }

    public func dec(): Bool{
        count.fetchAdd(-1)
        return true
    }
}