From 0752a7e443c6501a70e7654c313016d121df74bf Mon Sep 17 00:00:00 2001 From: bloved Date: Sun, 17 Jan 2021 16:15:12 +0100 Subject: [PATCH] - FIX: Query logger: moved to different thread to serialize access to log file --- 00.database.go | 2 +- dns_handler.go | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/00.database.go b/00.database.go index 67fdba8..ac4889b 100644 --- a/00.database.go +++ b/00.database.go @@ -40,7 +40,7 @@ func ZabovCreateKDB(conf string) { if err != nil { fmt.Println("Cannot create Killfile db: ", err.Error()) } else { - fmt.Println("Killfile DB created") + fmt.Println("Killfile DB created:", dbname) } MyZabovKDBs[conf] = KDB diff --git a/dns_handler.go b/dns_handler.go index bafd375..12c6801 100644 --- a/dns_handler.go +++ b/dns_handler.go @@ -16,7 +16,20 @@ var reqTypes map[uint16]string var weekdays []string +type logItem struct { + clientIP string + name string + reqType uint16 + config string + timetable string + killed string +} + +// logChannel used by logging thread +var logChannel chan logItem + func init() { + weekdays = []string{"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"} if len(ZabovDebugDBPath) > 0 { @@ -107,13 +120,18 @@ func init() { dns.TypeReserved: "TypeReserved"} fmt.Println("Local Time:", getLocalTime().Format(time.ANSIC)) + + if len(ZabovDebugDBPath) > 0 { + logChannel = make(chan logItem, 1024) + go logWriteThread() + } } -func logQuery(clientIP string, name string, reqType uint16, config string, timetable string, killed string) { - if len(ZabovDebugDBPath) > 0 { +func logWriteThread() { + for item := range logChannel { var header string d := time.Now().Format("2006-01-02") - logpath := path.Join(ZabovDebugDBPath, strings.Replace(clientIP, ":", "_", -1)+"-"+d+".log") + logpath := path.Join(ZabovDebugDBPath, strings.Replace(item.clientIP, ":", "_", -1)+"-"+d+".log") _, err1 := os.Stat(logpath) if os.IsNotExist(err1) { @@ -121,12 +139,12 @@ func logQuery(clientIP string, name string, reqType uint16, config string, timet } f, err := os.OpenFile(logpath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err == nil { - reqTypeName, err := reqTypes[reqType] + reqTypeName, err := reqTypes[item.reqType] if !err { - reqTypeName = fmt.Sprintf("%d", reqType) + reqTypeName = fmt.Sprintf("%d", item.reqType) } ct := time.Now().Format(time.RFC3339) - log := strings.Join([]string{ct, clientIP, strings.TrimRight(name, "."), reqTypeName, config, timetable, killed}, "\t") + log := strings.Join([]string{ct, item.clientIP, strings.TrimRight(item.name, "."), reqTypeName, item.config, item.timetable, item.killed}, "\t") if len(header) > 0 { f.Write([]byte(header)) f.Write([]byte("\n")) @@ -138,6 +156,15 @@ func logQuery(clientIP string, name string, reqType uint16, config string, timet } } +func logQuery(clientIP string, name string, reqType uint16, config string, timetable string, killed string) { + if len(ZabovDebugDBPath) > 0 { + k := logItem{clientIP: clientIP, name: name, reqType: reqType, config: config, timetable: timetable, killed: killed} + + logChannel <- k + + } +} + func getLocalTime() time.Time { return time.Now().Local() }