summaryrefslogtreecommitdiff
path: root/usage
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-07-06 18:06:13 +0530
committersinanmohd <sinan@sinanmohd.com>2024-07-06 18:06:17 +0530
commit22ae2f9b78dfdd2f59340d272ef598deb56cc245 (patch)
treecc7a305cb69cd915e090c63176a08b9dc247a4d2 /usage
parentaa57cfdf36407148af613e5633e264a22c4459de (diff)
go: Init() -> New(), CleanUp() -> Close()
Diffstat (limited to 'usage')
-rw-r--r--usage/main.go61
1 files changed, 31 insertions, 30 deletions
diff --git a/usage/main.go b/usage/main.go
index cc16311..72cf4fd 100644
--- a/usage/main.go
+++ b/usage/main.go
@@ -31,12 +31,24 @@ type Usage struct {
egressLink, ingressLink link.Link
}
-func (u *Usage) Init(iface *net.Interface) error {
+func Close(u *Usage, queries *db.Queries, ctxDb context.Context) {
+ err := u.UpdateDb(queries, ctxDb, false)
+ if err != nil {
+ log.Printf("updating Database: %s", err)
+ }
+
+ u.objs.Close()
+ u.ingressLink.Close()
+ u.egressLink.Close()
+}
+
+func New(iface *net.Interface) (*Usage, error) {
var err error
+ var u Usage
if err := loadBpfObjects(&u.objs, nil); err != nil {
log.Printf("loading objects: %s", err)
- return err
+ return nil, err
}
defer func() {
if err != nil {
@@ -51,7 +63,7 @@ func (u *Usage) Init(iface *net.Interface) error {
})
if err != nil {
log.Printf("could not attach TCx program: %s", err)
- return err
+ return nil, err
}
defer func() {
if err != nil {
@@ -66,22 +78,11 @@ func (u *Usage) Init(iface *net.Interface) error {
})
if err != nil {
log.Printf("could not attach TCx program: %s", err)
- return err
+ return nil, err
}
u.Data = make(usageMap)
- return nil
-}
-
-func (u *Usage) CleanUp(queries *db.Queries, ctxDb context.Context) {
- err := u.UpdateDb(queries, ctxDb, false)
- if err != nil {
- log.Printf("updating Database: %s", err)
- }
-
- u.objs.Close()
- u.ingressLink.Close()
- u.egressLink.Close()
+ return &u, nil
}
func (u *Usage) Run(iface *net.Interface, queries *db.Queries, ctxDb context.Context) {
@@ -106,20 +107,6 @@ func (u *Usage) Run(iface *net.Interface, queries *db.Queries, ctxDb context.Con
}
}
-func (us *usageStat) expired(timeStart *time.Time) bool {
- timeDiff := timeStart.Sub(us.lastSeen)
- if timeDiff > time.Minute {
- return true
- }
-
- timeDiff = timeStart.Sub(us.lastDbPush)
- if timeDiff > time.Hour {
- return true
- }
-
- return false
-}
-
func (u *Usage) UpdateDb(queries *db.Queries, ctxDb context.Context, ifExpired bool) error {
timeStart := time.Now()
@@ -153,6 +140,20 @@ func (u *Usage) UpdateDb(queries *db.Queries, ctxDb context.Context, ifExpired b
return nil
}
+func (us *usageStat) expired(timeStart *time.Time) bool {
+ timeDiff := timeStart.Sub(us.lastSeen)
+ if timeDiff > time.Minute {
+ return true
+ }
+
+ timeDiff = timeStart.Sub(us.lastDbPush)
+ if timeDiff > time.Hour {
+ return true
+ }
+
+ return false
+}
+
func (u *Usage) update(ingress *ebpf.Map, egress *ebpf.Map) error {
timeStart := time.Now()
batchKeys := make([]uint64, 4096)