zoukankan      html  css  js  c++  java
  • golang官方实现如何对httpserver做频率限制(最大连接数限制)

    一般海量处理服务,都会对服务做个最大连接数限制,超过该限制之后,拒绝服务,避免发生雪崩,压坏服务。

    使用golang来编写httpserver时,如何进行呢?官方已经有实现好的包。

    使用示例:

    import(
    "golang.org/x/net/netutil"
    )

    l, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { fmt.Fatalf("Listen: %v", err) } defer l.Close() l = LimitListener(l, max) http.Serve(l, http.HandlerFunc()) //bla bla bla.................

    源码如下(url : https://github.com/golang/net/blob/master/netutil/listen.go),基本思路就是为连接数计数,通过make chan来建立一个最大连接数的channel, 每次accept就+1,close时候就-1. 当到达最大连接数时,就等待空闲连接出来之后再accept。

    // Copyright 2013 The Go Authors.  All rights reserved.
    // Use of this source code is governed by a BSD-style
    // license that can be found in the LICENSE file.
    
    // Package netutil provides network utility functions, complementing the more
    // common ones in the net package.
    package netutil // import "golang.org/x/net/netutil"
    
    import (
        "net"
        "sync"
    )
    
    // LimitListener returns a Listener that accepts at most n simultaneous
    // connections from the provided Listener.
    func LimitListener(l net.Listener, n int) net.Listener {
        return &limitListener{l, make(chan struct{}, n)}
    }
    
    type limitListener struct {
        net.Listener
        sem chan struct{}
    }
    
    func (l *limitListener) acquire() { l.sem <- struct{}{} }
    func (l *limitListener) release() { <-l.sem }
    
    func (l *limitListener) Accept() (net.Conn, error) {
    //若connect chan已满,则会阻塞在此处 l.acquire() c, err :
    = l.Listener.Accept() if err != nil { l.release() return nil, err } return &limitListenerConn{Conn: c, release: l.release}, nil } type limitListenerConn struct { net.Conn releaseOnce sync.Once release func() } func (l *limitListenerConn) Close() error { err := l.Conn.Close() l.releaseOnce.Do(l.release) return err }
  • 相关阅读:
    Go 实现一个简单的TCP服务端
    将博客搬至CSDN
    My solution for Git Client Error: Permission denied (publickey)
    The Key to final data
    Design Pattern
    Difference between TCP and UDP
    Oracle SQL自带函数整理
    Java JDBC Batch
    Javascript Date 判断输入日期是否正确
    Javascript 数字保留2位小数
  • 原文地址:https://www.cnblogs.com/zhangqingping/p/4344278.html
Copyright © 2011-2022 走看看