hwlog--limiter.go
// Copyright(C) 2021. Huawei Technologies Co.,Ltd. All rights reserved.
// Package limiter implement a token bucket limiter
package limiter
import (
"context"
"huawei.com/npu-exporter/hwlog"
"huawei.com/npu-exporter/utils"
"math"
"net/http"
"time"
)
const (
kilo = 1000.0
)
type limitHandler struct {
concurrency chan struct{}
httpHandler http.Handler
log bool
}
// ServeHTTP implement http.Handler
func (h *limitHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
ctx := context.TODO()
reqID := req.Header.Get(hwlog.ReqID.String())
if reqID != "" {
ctx = context.WithValue(context.Background(), hwlog.ReqID, reqID)
}
id := req.Header.Get(hwlog.UserID.String())
if id != "" {
ctx = context.WithValue(ctx, hwlog.UserID, id)
}
path := req.URL.Path
clientIP := utils.ClientIP(req)
clientUserAgent := req.UserAgent()
select {
case _, ok := <-h.concurrency:
if !ok {
return
}
start := time.Now()
h.httpHandler.ServeHTTP(w, req)
stop := time.Since(start)
latency := int(math.Ceil(float64(stop.Nanoseconds()) / kilo / kilo))
if h.log {
hwlog.RunLog.InfofWithCtx(ctx, "%s %s: %s <%3d> (%dms) |%15s |%s ", req.Proto, req.Method, path,
http.StatusOK, latency, clientIP, clientUserAgent)
}
h.concurrency <- struct{}{}
default:
hwlog.RunLog.WarnfWithCtx(ctx, "Reject Request:%s: %s <%3d> |%15s |%s ", req.Method, path,
http.StatusServiceUnavailable, clientIP, clientUserAgent)
http.Error(w, "503 too busy", http.StatusServiceUnavailable)
}
}
// NewLimitHandler new a bucket-token limiter
func NewLimitHandler(maxConcur, maxConcurrency int, handler http.Handler, printLog bool) http.Handler {
if maxConcur < 1 || maxConcur > maxConcurrency {
hwlog.RunLog.Fatal("maxConcurrency parameter error")
}
h := &limitHandler{
concurrency: make(chan struct{}, maxConcur),
httpHandler: handler,
log: printLog,
}
for i := 0; i < maxConcur; i++ {
h.concurrency <- struct{}{}
}
return h
}
hwlog--limiter.go的更多相关文章
- Cannot send session cache limiter Cannot modify header information
当php报出 Cannot send session cache limiter 或Cannot modify header information 的错误时 其理论上是因为php代码以前有 ...
- Warning: session_start() [function.session-start]: Cannot send session cache limiter
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers alrea ...
- go笔记-限速器(limiter)
参考: https://blog.csdn.net/wdy_yx/article/details/73849713https://www.jianshu.com/p/1ecb513f7632 http ...
- ICD2 VPP limiter for new PIC microcontrollers.
http://www.circuitsathome.com/mcu/pic_vpp_limiter VOUT = 2.5V * ( 1 + 24/10 ) = 2.5 * 3.4 = 8.5V New ...
- rate limiter - system design
1 问题 Whenever you expose a web service / api endpoint, you need to implement a rate limiter to preve ...
- 359. Logger Rate Limiter
/* * 359. Logger Rate Limiter * 2016-7-14 by Mingyang * 很简单的HashMap,不详谈 */ class Logger { HashMap< ...
- Rate Limiter
Whenever you expose a web service / api endpoint, you need to implement a rate limiter to prevent ab ...
- RocksDB Rate Limiter源码解析
这次的项目我们重点关注RocksDB中的一个环节:Rate Limiter.其实Rate Limiter的思想在很多其他系统中也很常用. 在RocksDB中,后台会实时运行compaction和flu ...
- 详解FL Studio压缩器——Fruity Limiter(上)
压缩,是电音制作中重要一步,将声音信号压缩后可过滤噪音并使音质变好.众所周知,音乐编曲软件FL Studio的特色就是电音制作,所以必不可少要用到压缩器,今天我们就用FL Studio20来讲解一下. ...
- 详解FL Studio压缩器——Fruity Limiter(下)
Hello!小伙伴们又见面啦-接上一篇,本篇咱们继续讲解音乐编曲软件FL Studio20压缩器内容. 包络"ENVELOPE"中包含三个旋钮,它们都有什么作用呢?一起来揭晓吧! ...
随机推荐
- 第二章 Kubernetes快速入门
一.四组基本概念 Pod/Pod控制器: Name/Namespace: Label/Label选择器: Service/Ingress. 二.Pod/Pod控制器 2.1 Pod Pod是K8S里能 ...
- flex常用布局
公共样式: <style> * { margin: 0; padding: 0; } .has-flex { display: flex; } </style> 垂直居中 子元 ...
- 树莓派学习笔记 (1) - 安装&初始设置
1. 设备 Raspberry Pi 4B MicorSD card (tf 卡) Windows 10 电脑 Android 手机 2. 烧录系统 利用官网提供的 Raspberry Pi Imag ...
- Java SE 4、继承
继承 基本语法 class 子类 extends 父类{ } 子类就会自动拥有父类定义的属性和方法 父类又叫 超类,基类,子类又叫 派生类 细节 子类继承了所有的属性和方法,非私有的属性和方法可以在子 ...
- 微信小程序-坑,wxml里wx:if 判断 数字 是否在一个数组中。
<view wx:if="{{item.index}} in {{vote_list}}"> 已赞 <image src="/static/zan_y. ...
- Python使用tesserocr识别文字过程中遇到的一个问题
最近在使用Python识别PNG图像中包含的文字时遇到一个问题.解决过程记录如下. (Python使用tesserocr的安装过程不再描述.) 在使用tesserocr识别PNG图像中的文字时,如果P ...
- 使用mtr来判断网络丢包和网络延迟
转载自:https://mp.weixin.qq.com/s/UsjzMS1_rdxenw0TPlqwyQ 常用的 ping,tracert,nslookup 一般用来判断主机的网络连通性,其实 Li ...
- Security Context
概述 Security Context(安全上下文)用来限制容器对宿主节点的可访问范围,以避免容器非法操作宿主节点的系统级别的内容,使得节点的系统或者节点上其他容器组受到影响. Security Co ...
- 容器监控工具WeaveScope初步安装,了解
Weave Scope是Docker和Kubernetes的可视化和监视工具.它提供了自上而下的应用程序视图以及整个基础架构视图,并允许您实时诊断将分布式容器化应用程序部署到云提供商时遇到的任何问题. ...
- SpringBoot常用场景
SpringBoot-常见场景 1.热部署 SpringBoot为我们提供了一个方便我们开发测试的工具dev-tools.使用后可以实现热部署的效果.当我们运行了程序后对程序进行了修改,程序会自动 ...