Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
discosrv
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Ind.ie Projects
discosrv
Commits
418047df
Commit
418047df
authored
Apr 19, 2014
by
Jakob Borg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
discosrv: Better statistics
parent
29d3e40c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
12 deletions
+46
-12
main.go
main.go
+46
-12
No files found.
main.go
View file @
418047df
...
...
@@ -4,6 +4,7 @@ import (
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"log"
"net"
"os"
...
...
@@ -26,22 +27,28 @@ type Address struct {
}
var
(
nodes
=
make
(
map
[
string
]
Node
)
lock
sync
.
Mutex
queries
=
0
answered
=
0
limited
=
0
debug
=
false
limiter
=
lru
.
New
(
1024
)
nodes
=
make
(
map
[
string
]
Node
)
lock
sync
.
Mutex
queries
=
0
announces
=
0
answered
=
0
limited
=
0
unknowns
=
0
debug
=
false
limiter
=
lru
.
New
(
1024
)
)
func
main
()
{
var
listen
string
var
timestamp
bool
var
statsIntv
int
var
statsFile
string
flag
.
StringVar
(
&
listen
,
"listen"
,
":22025"
,
"Listen address"
)
flag
.
BoolVar
(
&
debug
,
"debug"
,
false
,
"Enable debug output"
)
flag
.
BoolVar
(
&
timestamp
,
"timestamp"
,
true
,
"Timestamp the log output"
)
flag
.
IntVar
(
&
statsIntv
,
"stats-intv"
,
0
,
"Statistics output interval (s)"
)
flag
.
StringVar
(
&
statsFile
,
"stats-file"
,
"/var/log/discosrv.stats"
,
"Statistics file name"
)
flag
.
Parse
()
log
.
SetOutput
(
os
.
Stdout
)
...
...
@@ -55,7 +62,9 @@ func main() {
log
.
Fatal
(
err
)
}
go
logStats
()
if
statsIntv
>
0
{
go
logStats
(
statsFile
,
statsIntv
)
}
var
buf
=
make
([]
byte
,
1024
)
for
{
...
...
@@ -85,6 +94,11 @@ func main() {
case
discover
.
QueryMagicV2
:
handleQueryV2
(
conn
,
addr
,
buf
)
default
:
lock
.
Lock
()
unknowns
++
lock
.
Unlock
()
}
}
}
...
...
@@ -129,6 +143,10 @@ func handleAnnounceV2(addr *net.UDPAddr, buf []byte) {
log
.
Printf
(
"<- %v %#v"
,
addr
,
pkt
)
}
lock
.
Lock
()
announces
++
lock
.
Unlock
()
ip
:=
addr
.
IP
.
To4
()
if
ip
==
nil
{
ip
=
addr
.
IP
.
To16
()
...
...
@@ -197,9 +215,21 @@ func handleQueryV2(conn *net.UDPConn, addr *net.UDPAddr, buf []byte) {
}
}
func
logStats
()
{
func
next
(
intv
int
)
time
.
Time
{
d
:=
time
.
Duration
(
intv
)
*
time
.
Second
t0
:=
time
.
Now
()
t1
:=
t0
.
Add
(
d
)
.
Truncate
(
d
)
time
.
Sleep
(
t1
.
Sub
(
t0
))
return
t1
}
func
logStats
(
file
string
,
intv
int
)
{
f
,
err
:=
os
.
OpenFile
(
file
,
os
.
O_WRONLY
|
os
.
O_CREATE
|
os
.
O_APPEND
,
0644
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
for
{
t
ime
.
Sleep
(
600
*
time
.
Second
)
t
:=
next
(
intv
)
lock
.
Lock
()
...
...
@@ -211,11 +241,15 @@ func logStats() {
}
}
log
.
Printf
(
"Expired %d nodes; %d nodes in registry; %d queries (%d answered)"
,
deleted
,
len
(
nodes
),
queries
,
answered
)
log
.
Printf
(
"Limited %d queries; %d entries in limiter cache"
,
limited
,
limiter
.
Len
())
fmt
.
Fprintf
(
f
,
"%d Nr:%d Ne:%d Qt:%d Qa:%d A:%d U:%d Lq:%d Lc:%d
\n
"
,
t
.
Unix
(),
len
(
nodes
),
deleted
,
queries
,
answered
,
announces
,
unknowns
,
limited
,
limiter
.
Len
())
f
.
Sync
()
queries
=
0
announces
=
0
answered
=
0
limited
=
0
unknowns
=
0
lock
.
Unlock
()
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment