From 4b44400700d0fb12cc7d86ecc020a30d78aa74c6 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Fri, 5 Jul 2024 18:26:47 +0530 Subject: bpf: better error management --- bpf/main.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/bpf/main.go b/bpf/main.go index 1f5610d..4f1360e 100644 --- a/bpf/main.go +++ b/bpf/main.go @@ -58,10 +58,16 @@ func Run(iface *net.Interface, queries *db.Queries, ctxDb context.Context) { for { select { case <-bpfTicker.C: - usageMap.update(objs.IngressIp4UsageMap, objs.EgressIp4UsageMap) + err := usageMap.update(objs.IngressIp4UsageMap, objs.EgressIp4UsageMap) + if err != nil { + log.Printf("updating usageMap: %s", err) + } + case <-dbTicker.C: - usageMap.dbPush(queries, ctxDb) - continue + err := usageMap.updateDb(queries, ctxDb) + if err != nil { + log.Printf("updating Database: %s", err) + } } } } @@ -80,7 +86,7 @@ func (usageStat UsageStat) expired(timeStart *time.Time) bool { return false } -func (usageMap UsageMap) dbPush(queries *db.Queries, ctxDb context.Context) { +func (usageMap UsageMap) updateDb(queries *db.Queries, ctxDb context.Context) error { timeStart := time.Now() for key, value := range usageMap { @@ -102,14 +108,16 @@ func (usageMap UsageMap) dbPush(queries *db.Queries, ctxDb context.Context) { Ingress: int32(value.ingress), }) if err != nil { - log.Println(err) + return err } delete(usageMap, key) } + + return nil } -func (usageMap UsageMap) update(ingress *ebpf.Map, egress *ebpf.Map) { +func (usageMap UsageMap) update(ingress *ebpf.Map, egress *ebpf.Map) error { timeStart := time.Now() batchKeys := make([]uint32, 4096) batchValues := make([]uint64, 4096) @@ -137,8 +145,7 @@ func (usageMap UsageMap) update(ingress *ebpf.Map, egress *ebpf.Map) { if errors.Is(err, ebpf.ErrKeyNotExist) { break } else if err != nil { - fmt.Println(err) - break + return err; } } @@ -164,8 +171,9 @@ func (usageMap UsageMap) update(ingress *ebpf.Map, egress *ebpf.Map) { if errors.Is(err, ebpf.ErrKeyNotExist) { break } else if err != nil { - fmt.Println(err) - break + return err } } + + return nil } -- cgit v1.2.3