From 9d8a95f83602fae47d5efd6d1df4e4806ed12580 Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Fri, 21 Feb 2020 13:01:41 +0300 Subject: [PATCH] *(dnsforward): fix in-memory log entries sort orderr --- querylog/qlog.go | 5 ++++- querylog/querylog_test.go | 19 +++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/querylog/qlog.go b/querylog/qlog.go index cafcdb5c..fb49fa35 100644 --- a/querylog/qlog.go +++ b/querylog/qlog.go @@ -206,7 +206,10 @@ func (l *queryLog) getData(params getDataParams) map[string]interface{} { l.bufferLock.Lock() total += len(l.buffer) memoryEntries := make([]*logEntry, 0) - for _, entry := range l.buffer { + + // go through the buffer in the reverse order + for i := len(l.buffer) - 1; i >= 0; i-- { + entry := l.buffer[i] if !matchesGetDataParams(entry, params) { continue } diff --git a/querylog/querylog_test.go b/querylog/querylog_test.go index 8d587b7c..06de4101 100644 --- a/querylog/querylog_test.go +++ b/querylog/querylog_test.go @@ -41,6 +41,7 @@ func TestQueryLog(t *testing.T) { _ = l.flushLogBuffer(true) // add memory entries addEntry(l, "test.example.org", "1.1.1.3", "2.2.2.3") + addEntry(l, "example.com", "1.1.1.4", "2.2.2.4") // get all entries params := getDataParams{ @@ -48,10 +49,11 @@ func TestQueryLog(t *testing.T) { } d := l.getData(params) mdata := d["data"].([]map[string]interface{}) - assert.Equal(t, 3, len(mdata)) - assert.True(t, checkEntry(t, mdata[0], "test.example.org", "1.1.1.3", "2.2.2.3")) - assert.True(t, checkEntry(t, mdata[1], "example.org", "1.1.1.2", "2.2.2.2")) - assert.True(t, checkEntry(t, mdata[2], "example.org", "1.1.1.1", "2.2.2.1")) + assert.Equal(t, 4, len(mdata)) + assert.True(t, checkEntry(t, mdata[0], "example.com", "1.1.1.4", "2.2.2.4")) + assert.True(t, checkEntry(t, mdata[1], "test.example.org", "1.1.1.3", "2.2.2.3")) + assert.True(t, checkEntry(t, mdata[2], "example.org", "1.1.1.2", "2.2.2.2")) + assert.True(t, checkEntry(t, mdata[3], "example.org", "1.1.1.1", "2.2.2.1")) // search by domain (strict) params = getDataParams{ @@ -96,10 +98,11 @@ func TestQueryLog(t *testing.T) { } d = l.getData(params) mdata = d["data"].([]map[string]interface{}) - assert.Equal(t, 3, len(mdata)) - assert.True(t, checkEntry(t, mdata[0], "test.example.org", "1.1.1.3", "2.2.2.3")) - assert.True(t, checkEntry(t, mdata[1], "example.org", "1.1.1.2", "2.2.2.2")) - assert.True(t, checkEntry(t, mdata[2], "example.org", "1.1.1.1", "2.2.2.1")) + assert.Equal(t, 4, len(mdata)) + assert.True(t, checkEntry(t, mdata[0], "example.com", "1.1.1.4", "2.2.2.4")) + assert.True(t, checkEntry(t, mdata[1], "test.example.org", "1.1.1.3", "2.2.2.3")) + assert.True(t, checkEntry(t, mdata[2], "example.org", "1.1.1.2", "2.2.2.2")) + assert.True(t, checkEntry(t, mdata[3], "example.org", "1.1.1.1", "2.2.2.1")) } func addEntry(l *queryLog, host, answerStr, client string) {