<commit_before>// Package libcentrifugo is a real-time core for Centrifugo server.
package libcentrifugo

import (
	"encoding/json"
	"runtime"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/centrifugal/centrifugo/Godeps/_workspace/src/github.com/gorilla/securecookie"
	"github.com/centrifugal/centrifugo/Godeps/_workspace/src/github.com/nu7hatch/gouuid"
	"github.com/centrifugal/centrifugo/Godeps/_workspace/src/github.com/rcrowley/go-metrics"
	"github.com/centrifugal/centrifugo/libcentrifugo/logger"
)

// Application is a heart of Centrifugo – it internally manages connection, subscription
// and admin hubs, maintains information about other Centrifugo nodes, keeps references to
// config, engine and structure.
type Application struct {
	sync.RWMutex

	// unique id for this application (node).
	uid string

	// started is unix time of node start.
	started int64

	// nodes is a map with information about nodes known.
	nodes map[string]NodeInfo
	// nodesMu allows to synchronize access to nodes.
	nodesMu sync.Mutex

	// hub to manage client connections.
	clients *clientHub
	// hub to manage admin connections.
	admins *adminHub

	// engine to use - in memory or redis.
	engine Engine

	// config for application.
	config *Config

	// mediator allows integrate libcentrifugo Application with external go code.
	mediator Mediator

	// shutdown is a flag which is only true when application is going to shut down.
	shutdown bool

	// metrics holds various counters and timers different parts of Centrifugo update.
	metrics *metricsRegistry
}

type Stats struct {
	Nodes    []NodeInfo `json:"nodes"`
	Interval int64      `json:"interval"`
}

type Metrics struct {
	NumMsgPublished   int64 `json:"num_msg_published"`
	NumMsgQueued      int64 `json:"num_msg_queued"`
	NumMsgSent        int64 `json:"num_msg_sent"`
	NumAPIRequests    int64 `json:"num_api_requests"`
	NumClientRequests int64 `json:"num_client_requests"`
	TimeAPIMean       int64 `json:"time_api_mean"`
	TimeClientMean    int64 `json:"time_client_mean"`
	TimeAPIMax        int64 `json:"time_api_max"`
	TimeClientMax     int64 `json:"time_client_max"`
}

type metricsRegistry struct {
	sync.RWMutex
	numMsgPublished   metrics.Counter
	numMsgQueued      metrics.Counter
	numMsgSent        metrics.Counter
	numAPIRequests    metrics.Counter
	numClientRequests metrics.Counter
	timeAPI           metrics.Timer
	timeClient        metrics.Timer
	metrics           *Metrics
}

func NewMetricsRegistry() *metricsRegistry {
	return &metricsRegistry{
		numMsgPublished:   metrics.NewCounter(),
		numMsgQueued:      metrics.NewCounter(),
		numMsgSent:        metrics.NewCounter(),
		numAPIRequests:    metrics.NewCounter(),
		numClientRequests: metrics.NewCounter(),
		timeAPI:           metrics.NewCustomTimer(metrics.NewHistogram(metrics.NewExpDecaySample(1028, 2)), metrics.NewMeter()),
		timeClient:        metrics.NewCustomTimer(metrics.NewHistogram(metrics.NewExpDecaySample(1028, 2)), metrics.NewMeter()),
		metrics:           &Metrics{},
	}
}

type NodeInfo struct {
	Uid        string `json:"uid"`
	Name       string `json:"name"`
	Goroutines int    `json:"num_goroutine"`
	Clients    int    `json:"num_clients"`
	Unique     int    `json:"num_unique_clients"`
	Channels   int    `json:"num_channels"`
	Started    int64  `json:"started"`
	Metrics
	updated int64 `json:"-"`
}

// NewApplication returns new Application instance, the only required argument is
// config, structure and engine must be set via corresponding methods.
func NewApplication(c *Config) (*Application, error) {
	uid, err := uuid.NewV4()
	if err != nil {
		return nil, err
	}
	app := &Application{
		uid:     uid.String(),
		nodes:   make(map[string]NodeInfo),
		clients: newClientHub(),
		admins:  newAdminHub(),<commit_msg>remove json tag<commit_after>// Package libcentrifugo is a real-time core for Centrifugo server.
package libcentrifugo

import (
	"encoding/json"
	"runtime"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/centrifugal/centrifugo/Godeps/_workspace/src/github.com/gorilla/securecookie"
	"github.com/centrifugal/centrifugo/Godeps/_workspace/src/github.com/nu7hatch/gouuid"
	"github.com/centrifugal/centrifugo/Godeps/_workspace/src/github.com/rcrowley/go-metrics"
	"github.com/centrifugal/centrifugo/libcentrifugo/logger"
)

// Application is a heart of Centrifugo – it internally manages connection, subscription
// and admin hubs, maintains information about other Centrifugo nodes, keeps references to
// config, engine and structure.
type Application struct {
	sync.RWMutex

	// unique id for this application (node).
	uid string

	// started is unix time of node start.
	started int64

	// nodes is a map with information about nodes known.
	nodes map[string]NodeInfo
	// nodesMu allows to synchronize access to nodes.
	nodesMu sync.Mutex

	// hub to manage client connections.
	clients *clientHub
	// hub to manage admin connections.
	admins *adminHub

	// engine to use - in memory or redis.
	engine Engine

	// config for application.
	config *Config

	// mediator allows integrate libcentrifugo Application with external go code.
	mediator Mediator

	// shutdown is a flag which is only true when application is going to shut down.
	shutdown bool

	// metrics holds various counters and timers different parts of Centrifugo update.
	metrics *metricsRegistry
}

type Stats struct {
	Nodes    []NodeInfo `json:"nodes"`
	Interval int64      `json:"interval"`
}

type Metrics struct {
	NumMsgPublished   int64 `json:"num_msg_published"`
	NumMsgQueued      int64 `json:"num_msg_queued"`
	NumMsgSent        int64 `json:"num_msg_sent"`
	NumAPIRequests    int64 `json:"num_api_requests"`
	NumClientRequests int64 `json:"num_client_requests"`
	TimeAPIMean       int64 `json:"time_api_mean"`
	TimeClientMean    int64 `json:"time_client_mean"`
	TimeAPIMax        int64 `json:"time_api_max"`
	TimeClientMax     int64 `json:"time_client_max"`
}

type metricsRegistry struct {
	sync.RWMutex
	numMsgPublished   metrics.Counter
	numMsgQueued      metrics.Counter
	numMsgSent        metrics.Counter
	numAPIRequests    metrics.Counter
	numClientRequests metrics.Counter
	timeAPI           metrics.Timer
	timeClient        metrics.Timer
	metrics           *Metrics
}

func NewMetricsRegistry() *metricsRegistry {
	return &metricsRegistry{
		numMsgPublished:   metrics.NewCounter(),
		numMsgQueued:      metrics.NewCounter(),
		numMsgSent:        metrics.NewCounter(),
		numAPIRequests:    metrics.NewCounter(),
		numClientRequests: metrics.NewCounter(),
		timeAPI:           metrics.NewCustomTimer(metrics.NewHistogram(metrics.NewExpDecaySample(1028, 2)), metrics.NewMeter()),
		timeClient:        metrics.NewCustomTimer(metrics.NewHistogram(metrics.NewExpDecaySample(1028, 2)), metrics.NewMeter()),
		metrics:           &Metrics{},
	}
}

type NodeInfo struct {
	Uid        string `json:"uid"`
	Name       string `json:"name"`
	Goroutines int    `json:"num_goroutine"`
	Clients    int    `json:"num_clients"`
	Unique     int    `json:"num_unique_clients"`
	Channels   int    `json:"num_channels"`
	Started    int64  `json:"started"`
	Metrics
	updated int64
}

// NewApplication returns new Application instance, the only required argument is
// config, structure and engine must be set via corresponding methods.
func NewApplication(c *Config) (*Application, error) {
	uid, err := uuid.NewV4()
	if err != nil {
		return nil, err
	}
	app := &Application{
		uid:     uid.String(),
		nodes:   make(map[string]NodeInfo),
		clients: newClientHub(),
		admins:  newAdminHub(),
{'fraction_non_alphanumeric': 0.09435215946843854, 'fraction_numerical': 0.010099667774086378, 'mean_word_length': 3.9676567656765678, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7459373', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2305, 'n_words': 634}