Skip to content

Commit

Permalink
all: refactor stats & add mutexes
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Aug 3, 2022
1 parent b3f11c4 commit f2b2de7
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 220 deletions.
2 changes: 1 addition & 1 deletion internal/dhcpd/dhcpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type ServerConfig struct {
ConfigModified func() `yaml:"-"`

// Register an HTTP handler
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request)) `yaml:"-"`
HTTPRegister func(string, string, http.HandlerFunc) `yaml:"-"`

Enabled bool `yaml:"enabled"`
InterfaceName string `yaml:"interface_name"`
Expand Down
2 changes: 1 addition & 1 deletion internal/dnsforward/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ type ServerConfig struct {
ConfigModified func()

// Register an HTTP handler
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request))
HTTPRegister func(string, string, http.HandlerFunc)

// ResolveClients signals if the RDNS should resolve clients' addresses.
ResolveClients bool
Expand Down
4 changes: 2 additions & 2 deletions internal/dnsforward/dnsforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Server struct {
dnsFilter *filtering.DNSFilter // DNS filter instance
dhcpServer dhcpd.ServerInterface // DHCP server instance (optional)
queryLog querylog.QueryLog // Query log instance
stats stats.Stats
stats stats.Interface
access *accessCtx

// localDomainSuffix is the suffix used to detect internal hosts. It
Expand Down Expand Up @@ -107,7 +107,7 @@ const defaultLocalDomainSuffix = "lan"
// DNSCreateParams are parameters to create a new server.
type DNSCreateParams struct {
DNSFilter *filtering.DNSFilter
Stats stats.Stats
Stats stats.Interface
QueryLog querylog.QueryLog
DHCPServer dhcpd.ServerInterface
PrivateNets netutil.SubnetSet
Expand Down
2 changes: 1 addition & 1 deletion internal/dnsforward/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (l *testQueryLog) Add(p *querylog.AddParams) {
type testStats struct {
// Stats is embedded here simply to make testStats a stats.Stats without
// actually implementing all methods.
stats.Stats
stats.Interface

lastEntry stats.Entry
}
Expand Down
2 changes: 1 addition & 1 deletion internal/filtering/filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type Config struct {
ConfigModified func() `yaml:"-"`

// Register an HTTP handler
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request)) `yaml:"-"`
HTTPRegister func(string, string, http.HandlerFunc) `yaml:"-"`

// CustomResolver is the resolver used by DNSFilter.
CustomResolver Resolver `yaml:"-"`
Expand Down
2 changes: 1 addition & 1 deletion internal/home/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func registerControlHandlers() {
RegisterAuthHandlers()
}

func httpRegister(method, url string, handler func(http.ResponseWriter, *http.Request)) {
func httpRegister(method, url string, handler http.HandlerFunc) {
if method == "" {
// "/dns-query" handler doesn't need auth, gzip and isn't restricted by 1 HTTP method
Context.mux.HandleFunc(url, postInstall(handler))
Expand Down
2 changes: 1 addition & 1 deletion internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type homeContext struct {
// --

clients clientsContainer // per-client-settings module
stats stats.Stats // statistics module
stats stats.Interface // statistics module
queryLog querylog.QueryLog // query log module
dnsServer *dnsforward.Server // DNS module
rdns *RDNS // rDNS module
Expand Down
2 changes: 1 addition & 1 deletion internal/querylog/querylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Config struct {
ConfigModified func()

// HTTPRegister registers an HTTP handler.
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request))
HTTPRegister func(string, string, http.HandlerFunc)

// FindClient returns client information by their IDs.
FindClient func(ids []string) (c *Client, err error)
Expand Down
47 changes: 16 additions & 31 deletions internal/stats/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,15 @@ func (s *statsCtx) handleStats(w http.ResponseWriter, r *http.Request) {
start := time.Now()

var resp statsResponse
if s.conf.limit == 0 {
resp = statsResponse{
TimeUnits: "days",

TopBlocked: []topAddrs{},
TopClients: []topAddrs{},
TopQueried: []topAddrs{},

BlockedFiltering: []uint64{},
DNSQueries: []uint64{},
ReplacedParental: []uint64{},
ReplacedSafebrowsing: []uint64{},
}
} else {
var ok bool
resp, ok = s.getData()

log.Debug("stats: prepared data in %v", time.Since(start))

if !ok {
aghhttp.Error(r, w, http.StatusInternalServerError, "Couldn't get statistics data")

return
}
var ok bool
resp, ok = s.getData()

log.Debug("stats: prepared data in %v", time.Since(start))

if !ok {
aghhttp.Error(r, w, http.StatusInternalServerError, "Couldn't get statistics data")

return
}

w.Header().Set("Content-Type", "application/json")
Expand All @@ -86,7 +71,7 @@ type config struct {
// Get configuration
func (s *statsCtx) handleStatsInfo(w http.ResponseWriter, r *http.Request) {
resp := config{}
resp.IntervalDays = s.conf.limit / 24
resp.IntervalDays = s.limitHours / 24

data, err := json.Marshal(resp)
if err != nil {
Expand Down Expand Up @@ -118,7 +103,7 @@ func (s *statsCtx) handleStatsConfig(w http.ResponseWriter, r *http.Request) {
}

s.setLimit(int(reqData.IntervalDays))
s.conf.ConfigModified()
s.configModified()
}

// Reset data
Expand All @@ -128,12 +113,12 @@ func (s *statsCtx) handleStatsReset(w http.ResponseWriter, r *http.Request) {

// Register web handlers
func (s *statsCtx) initWeb() {
if s.conf.HTTPRegister == nil {
if s.httpRegister == nil {
return
}

s.conf.HTTPRegister(http.MethodGet, "/control/stats", s.handleStats)
s.conf.HTTPRegister(http.MethodPost, "/control/stats_reset", s.handleStatsReset)
s.conf.HTTPRegister(http.MethodPost, "/control/stats_config", s.handleStatsConfig)
s.conf.HTTPRegister(http.MethodGet, "/control/stats_info", s.handleStatsInfo)
s.httpRegister(http.MethodGet, "/control/stats", s.handleStats)
s.httpRegister(http.MethodPost, "/control/stats_reset", s.handleStatsReset)
s.httpRegister(http.MethodPost, "/control/stats_config", s.handleStatsConfig)
s.httpRegister(http.MethodGet, "/control/stats_info", s.handleStatsInfo)
}
22 changes: 11 additions & 11 deletions internal/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"net/http"
)

type unitIDCallback func() uint32
// UnitIDGenFunc generates a unique ID for the statistics unit.
type UnitIDGenFunc func() (id uint32)

// DiskConfig - configuration settings that are stored on disk
type DiskConfig struct {
Expand All @@ -16,26 +17,24 @@ type DiskConfig struct {

// Config - module configuration
type Config struct {
Filename string // database file name
LimitDays uint32 // time limit (in days)
UnitID unitIDCallback // user function to get the current unit ID. If nil, the current time hour is used.
Filename string // database file name
LimitDays uint32 // time limit (in days)
UnitID UnitIDGenFunc // user function to get the current unit ID. If nil, the current time hour is used.

// Called when the configuration is changed by HTTP request
ConfigModified func()

// Register an HTTP handler
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request))

limit uint32 // maximum time we need to keep data for (in hours)
HTTPRegister func(string, string, http.HandlerFunc)
}

// New - create object
func New(conf Config) (Stats, error) {
func New(conf Config) (Interface, error) {
return createObject(conf)
}

// Stats - main interface
type Stats interface {
// Interface - main interface
type Interface interface {
Start()

// Close object.
Expand Down Expand Up @@ -72,7 +71,8 @@ const (
RSafeBrowsing
RSafeSearch
RParental
rLast

resultsNumber = RParental + 1
)

// Entry is a statistics data entry.
Expand Down
Loading

0 comments on commit f2b2de7

Please sign in to comment.