-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: He Xian <[email protected]>
- Loading branch information
Showing
7 changed files
with
87 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package eventlog | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Recent interface { | ||
Add(timestamp time.Time, message string) | ||
Format(w io.Writer) | ||
} | ||
|
||
type entry struct { | ||
tstamp time.Time | ||
message string | ||
count int | ||
} | ||
|
||
type recent struct { | ||
mu sync.Mutex | ||
elements []entry | ||
sizelimit int | ||
} | ||
|
||
func NewRecent(sizelimit int) Recent { | ||
return &recent{ | ||
elements: []entry{}, | ||
sizelimit: sizelimit, | ||
} | ||
} | ||
|
||
func (p *recent) Add(timestamp time.Time, message string) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
if len(p.elements) > 0 && p.elements[len(p.elements)-1].message == message { | ||
p.elements[len(p.elements)-1].count++ | ||
} | ||
p.elements = append(p.elements, entry{timestamp, message, 1}) | ||
if len(p.elements) >= 2*p.sizelimit { | ||
copy(p.elements, p.elements[len(p.elements)-p.sizelimit:]) | ||
p.elements = p.elements[:p.sizelimit] | ||
} | ||
} | ||
|
||
func (p *recent) Format(w io.Writer) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
n := max(0, len(p.elements)-p.sizelimit) | ||
for i := len(p.elements) - 1; i >= n; i-- { | ||
entry := p.elements[i] | ||
var s string | ||
if entry.count == 1 { | ||
s = fmt.Sprintf("%s %s\n", entry.tstamp.Format(time.RFC3339), entry.message) | ||
} else { | ||
s = fmt.Sprintf("%s %s (x%d)\n", entry.tstamp.Format(time.RFC3339), entry.message, entry.count) | ||
} | ||
w.Write([]byte(s)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
module github.com/hexian000/tlswrapper/v2 | ||
|
||
go 1.20 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/hashicorp/yamux v0.1.1 | ||
github.com/hexian000/gosnippets v0.0.0-20231124140902-d2fe8bf2355d | ||
github.com/hexian000/gosnippets v0.0.0-20240124083900-b330d695ba90 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE= | ||
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= | ||
github.com/hexian000/gosnippets v0.0.0-20231124140902-d2fe8bf2355d h1:OTTGcMF8+0pFlYuM8h0LjNly0TIsOOt5etdL8oSbmMw= | ||
github.com/hexian000/gosnippets v0.0.0-20231124140902-d2fe8bf2355d/go.mod h1:SItpHKzoWxn8Lj2kJiw1ridZGDunCyUIbpL1N/7j6FQ= | ||
github.com/hexian000/gosnippets v0.0.0-20240124083900-b330d695ba90 h1:wdmi39Ie1Afh28nw7XZnEmoMtRL33pFH8W23IXP6ICA= | ||
github.com/hexian000/gosnippets v0.0.0-20240124083900-b330d695ba90/go.mod h1:SItpHKzoWxn8Lj2kJiw1ridZGDunCyUIbpL1N/7j6FQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters