File: conn_counter.go

package info (click to toggle)
golang-gopkg-rethinkdb-rethinkdb-go.v6 6.2.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,736 kB
  • sloc: python: 1,382; makefile: 16; sh: 9
file content (91 lines) | stat: -rw-r--r-- 1,542 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package utils

import (
	"fmt"
	"net"
	"os"
	"regexp"
	"runtime"
	"runtime/debug"
	"strings"
	"sync"
	"sync/atomic"
	"time"
)

var connsCount int64
var printer sync.Once

var mu sync.Mutex
var createdStacks = make(map[string]int)
var reg = regexp.MustCompile(`0[xX][0-9a-fA-F]+`)

type connCounting struct {
	net.Conn

	closed bool
}

// Socket leak debug net.Conn wrapper
func NewCountingConn(conn net.Conn) net.Conn {
	c := &connCounting{
		Conn:   conn,
		closed: false,
	}
	runtime.SetFinalizer(c, func(cc *connCounting) {
		if !cc.closed {
			atomic.AddInt64(&connsCount, -1)
			cc.closed = true
		}
	})

	atomic.AddInt64(&connsCount, 1)
	printer.Do(func() {
		go func() {
			t := time.NewTicker(time.Second)
			f, err := os.Create("sockets.ticker")
			if err != nil {
				fmt.Fprintf(os.Stderr, "Failed to create sockets.ticker file: %v\n", err)
				return
			}
			for {
				<-t.C
				fmt.Fprintf(f, "Connections count: %v\n", atomic.LoadInt64(&connsCount))
				f.Sync()
			}
		}()
	})

	st := string(debug.Stack())
	st = st[strings.Index(st, "\n")+1:]
	st = reg.ReplaceAllString(st, "")

	mu.Lock()
	_, has := createdStacks[st]
	if !has {
		createdStacks[st] = 1
	} else {
		createdStacks[st]++
	}
	printStacks()
	mu.Unlock()

	return c
}

func (c *connCounting) Close() error {
	if !c.closed {
		atomic.AddInt64(&connsCount, -1)
		c.closed = true
	}
	return c.Conn.Close()
}

func printStacks() {
	f, _ := os.Create("sockets.created")
	for s, c := range createdStacks {
		fmt.Fprintf(f, "%v:\n%v\n\n", c, s)
	}
	f.Sync()
	f.Close()
}