Last change
on this file since 145 was 145, checked in by Izuru Yakumo, 22 months ago |
Updated the Makefile and vendored depedencies
Signed-off-by: Izuru Yakumo <yakumo.izuru@…>
|
File size:
1.7 KB
|
Rev | Line | |
---|
[145] | 1 | package fasthttp
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
| 4 | "fmt"
|
---|
| 5 | "net"
|
---|
| 6 | "sync"
|
---|
| 7 | )
|
---|
| 8 |
|
---|
| 9 | type perIPConnCounter struct {
|
---|
| 10 | pool sync.Pool
|
---|
| 11 | lock sync.Mutex
|
---|
| 12 | m map[uint32]int
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | func (cc *perIPConnCounter) Register(ip uint32) int {
|
---|
| 16 | cc.lock.Lock()
|
---|
| 17 | if cc.m == nil {
|
---|
| 18 | cc.m = make(map[uint32]int)
|
---|
| 19 | }
|
---|
| 20 | n := cc.m[ip] + 1
|
---|
| 21 | cc.m[ip] = n
|
---|
| 22 | cc.lock.Unlock()
|
---|
| 23 | return n
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | func (cc *perIPConnCounter) Unregister(ip uint32) {
|
---|
| 27 | cc.lock.Lock()
|
---|
| 28 | if cc.m == nil {
|
---|
| 29 | cc.lock.Unlock()
|
---|
| 30 | panic("BUG: perIPConnCounter.Register() wasn't called")
|
---|
| 31 | }
|
---|
| 32 | n := cc.m[ip] - 1
|
---|
| 33 | if n < 0 {
|
---|
| 34 | cc.lock.Unlock()
|
---|
| 35 | panic(fmt.Sprintf("BUG: negative per-ip counter=%d for ip=%d", n, ip))
|
---|
| 36 | }
|
---|
| 37 | cc.m[ip] = n
|
---|
| 38 | cc.lock.Unlock()
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | type perIPConn struct {
|
---|
| 42 | net.Conn
|
---|
| 43 |
|
---|
| 44 | ip uint32
|
---|
| 45 | perIPConnCounter *perIPConnCounter
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | func acquirePerIPConn(conn net.Conn, ip uint32, counter *perIPConnCounter) *perIPConn {
|
---|
| 49 | v := counter.pool.Get()
|
---|
| 50 | if v == nil {
|
---|
| 51 | v = &perIPConn{
|
---|
| 52 | perIPConnCounter: counter,
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | c := v.(*perIPConn)
|
---|
| 56 | c.Conn = conn
|
---|
| 57 | c.ip = ip
|
---|
| 58 | return c
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | func releasePerIPConn(c *perIPConn) {
|
---|
| 62 | c.Conn = nil
|
---|
| 63 | c.perIPConnCounter.pool.Put(c)
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | func (c *perIPConn) Close() error {
|
---|
| 67 | err := c.Conn.Close()
|
---|
| 68 | c.perIPConnCounter.Unregister(c.ip)
|
---|
| 69 | releasePerIPConn(c)
|
---|
| 70 | return err
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | func getUint32IP(c net.Conn) uint32 {
|
---|
| 74 | return ip2uint32(getConnIP4(c))
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | func getConnIP4(c net.Conn) net.IP {
|
---|
| 78 | addr := c.RemoteAddr()
|
---|
| 79 | ipAddr, ok := addr.(*net.TCPAddr)
|
---|
| 80 | if !ok {
|
---|
| 81 | return net.IPv4zero
|
---|
| 82 | }
|
---|
| 83 | return ipAddr.IP.To4()
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | func ip2uint32(ip net.IP) uint32 {
|
---|
| 87 | if len(ip) != 4 {
|
---|
| 88 | return 0
|
---|
| 89 | }
|
---|
| 90 | return uint32(ip[0])<<24 | uint32(ip[1])<<16 | uint32(ip[2])<<8 | uint32(ip[3])
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | func uint322ip(ip uint32) net.IP {
|
---|
| 94 | b := make([]byte, 4)
|
---|
| 95 | b[0] = byte(ip >> 24)
|
---|
| 96 | b[1] = byte(ip >> 16)
|
---|
| 97 | b[2] = byte(ip >> 8)
|
---|
| 98 | b[3] = byte(ip)
|
---|
| 99 | return b
|
---|
| 100 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.