Skip to content

Commit

Permalink
Fix: clean closed connection cache (#1)
Browse files Browse the repository at this point in the history
* Fix: clean connection cache

Signed-off-by: Zzde <[email protected]>

* fix actions

Signed-off-by: Zzde <[email protected]>

* Add checkout

Signed-off-by: Zzde <[email protected]>

* Fixed

Signed-off-by: Zzde <[email protected]>

---------

Signed-off-by: Zzde <[email protected]>
  • Loading branch information
zxh326 authored Apr 29, 2023
1 parent 42ac01f commit e3d0b59
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Build And Releases

on:
push:
branches:
- main

tags:
- v*

# Run tests for any PRs.
pull_request:
permissions:
contents: write # publishing releases

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v4
with:
check-latest: true
go-version: "1.20"

- name: Build
run: make -j releases

- name: Upload Releases Files
if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v1
with:
files: "*.tar.gz"
draft: true

- name: clean
run: make clean
3 changes: 0 additions & 3 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ on:
tags:
- v*

# Run tests for any PRs.
pull_request:

env:
IMAGE_NAME: clash-exporter

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

.DS_Store
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
NAME=clash-exporter
VERSION=$(shell git describe --tags --always)

releases: darwin-amd64 darwin-arm64 linux-amd64 linux-arm64 linux-armv6 linux-armv7

darwin-amd64:
GOOS=darwin GOARCH=amd64 go build -o $(NAME)-${VERSION}-$@
tar czf $(NAME)-${VERSION}-$@.tar.gz $(NAME)-${VERSION}-$@
darwin-arm64:
GOOS=darwin GOARCH=arm64 go build -o $(NAME)-${VERSION}-$@
tar czf $(NAME)-${VERSION}-$@.tar.gz $(NAME)-${VERSION}-$@
linux-amd64:
GOOS=linux GOARCH=amd64 go build -o $(NAME)-${VERSION}-$@
tar czf $(NAME)-${VERSION}-$@.tar.gz $(NAME)-${VERSION}-$@
linux-arm64:
GOOS=linux GOARCH=arm64 go build -o $(NAME)-${VERSION}-$@
tar czf $(NAME)-${VERSION}-$@.tar.gz $(NAME)-${VERSION}-$@
linux-armv6:
GOOS=linux GOARCH=arm GOARM=6 go build -o $(NAME)-${VERSION}-$@
tar czf $(NAME)-${VERSION}-$@.tar.gz $(NAME)-${VERSION}-$@
linux-armv7:
GOOS=linux GOARCH=arm GOARM=7 go build -o $(NAME)-${VERSION}-$@
tar czf $(NAME)-${VERSION}-$@.tar.gz $(NAME)-${VERSION}-$@

clean:
rm -rf $(NAME)-*
8 changes: 7 additions & 1 deletion collector/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (c *Connection) Collect(config CollectConfig) error {
downloadTotal.WithLabelValues().Set(float64(m.DownloadTotal))
activeConnections.WithLabelValues().Set(float64(len(m.Connections)))

activeConnectionsMap := make(map[string]interface{})
for _, connection := range m.Connections {
if _, ok := c.connectionCache[connection.ID]; !ok {
c.connectionCache[connection.ID] = Connections{
Expand All @@ -99,8 +100,13 @@ func (c *Connection) Collect(config CollectConfig) error {
networkTrafficTotal.WithLabelValues(connection.Metadata.SourceIP, destination, connection.Chains[0], "download").Add(float64(connection.Download) - float64(c.connectionCache[connection.ID].Download))
networkTrafficTotal.WithLabelValues(connection.Metadata.SourceIP, destination, connection.Chains[0], "upload").Add(float64(connection.Upload) - float64(c.connectionCache[connection.ID].Upload))
c.connectionCache[connection.ID] = connection
activeConnectionsMap[connection.ID] = nil
}
for id := range c.connectionCache {
if _, ok := activeConnectionsMap[id]; !ok {
delete(c.connectionCache, id)
}
}
// TODO: remove closed connections cache
}
}

Expand Down

0 comments on commit e3d0b59

Please sign in to comment.