Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimized storage #13

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
go: [ '1.18' ]
go: [ '1.22.x' ]
name: Go v${{ matrix.go }}
steps:
- uses: actions/checkout@v2
Expand Down
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# gets last tag
VERSION := $(shell git describe --abbrev=0 --tags)

test:
go vet ./...
go test ./...
.PHONY: test
test: unit cover

.PHONY: unit
unit:
go vet ./...
go test -v -timeout 30s -coverprofile=coverage.out ./...

.PHONY: cover
cover:
go tool cover -func=coverage.out

build-cli:
go build -o dracula-cli cmd/cli/main.go
Expand Down
22 changes: 16 additions & 6 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package client

import (
"math"
"os"
"path"
"sync"
"testing"
"time"
Expand All @@ -11,11 +13,15 @@ import (
"github.com/stretchr/testify/assert"
)

var (
storageDirectory, _ = os.MkdirTemp(os.TempDir(), "dracula-client-*")
)

func TestClient_Auth(t *testing.T) {
// there are already tests with empty secret as well

storagePath := path.Join(storageDirectory, "TestClient_Auth.db")
secret := "asdf-jkl-HOHOHO!"
s := server.NewServer(60, secret)
s := server.NewServer(60, secret, storagePath)
s.DebugEnable("9000")
err := s.Listen(9000, 9000)
if err != nil {
Expand Down Expand Up @@ -65,15 +71,17 @@ func TestClient_Auth(t *testing.T) {
}

func TestClient_Healthcheck(t *testing.T) {
s1 := server.NewServer(60, "sec1")
storagePath := path.Join(storageDirectory, "TestClient_Healthcheck1.db")
s1 := server.NewServer(60, "sec1", storagePath)
s1.DebugEnable("9000")
err := s1.Listen(9000, 9000)
if err != nil {
t.Fatal(err)
}
defer s1.Close()

s2 := server.NewServer(60, "sec1")
storagePath = path.Join(storageDirectory, "TestClient_Healthcheck2.db")
s2 := server.NewServer(60, "sec1", storagePath)
s2.DebugEnable("9100")
err = s2.Listen(9100, 9010)
if err != nil {
Expand Down Expand Up @@ -129,8 +137,9 @@ func TestClient_messageIDThreadSafe(t *testing.T) {

func TestClient_TcpKeyMatch(t *testing.T) {
t.Run("returns ordered keys with secret", func(t *testing.T) {
storagePath := path.Join(storageDirectory, "TestClient_TcpKeyMatch.db")
secret := "asdf-!!?!|asdf"
s := server.NewServer(60, secret)
s := server.NewServer(60, secret, storagePath)
s.DebugEnable("9000")
err := s.Listen(9000, 9000)
if err != nil {
Expand Down Expand Up @@ -167,8 +176,9 @@ func TestClient_TcpKeyMatch(t *testing.T) {

func TestClient_TcpListNamespaces(t *testing.T) {
t.Run("returns a list of namespaces", func(t *testing.T) {
storagePath := path.Join(storageDirectory, "TestClient_TcpListNamespaces.db")
secret := "asdf-!!?!|asdf"
s := server.NewServer(60, secret)
s := server.NewServer(60, secret, storagePath)
s.DebugEnable("9011")
err := s.Listen(9011, 9011)
if err != nil {
Expand Down
18 changes: 12 additions & 6 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package main
import (
"flag"
"fmt"
"github.com/mailsac/dracula/server"
"os"
"strings"
"sync"

"github.com/mailsac/dracula/server"
)

var (
Expand All @@ -15,12 +16,13 @@ var (
port = flag.Int("p", 3509, "UDP this server will run on")
tcpPort = flag.Int("tcp", 3509, "TCP port this server will run on")
restHostPort = flag.String("http", "0.0.0.0:3510", "Enable HTTP REST interface. Example: '0.0.0.0:3510'")
secret = flag.String("s", "", "Optional pre-shared auth secret if not using env var DRACULA_SECRET")
key = flag.String("k", "", "Optional pre-shared auth secret key if not using env var DRACULA_SECRET")
peerIPPort = flag.String("i", "", "Self peer IP and host like 192.168.0.1:3509 to identify self in the cluster")
peers = flag.String("c", "", "Enable cluster replication. Peers must be comma-separated ip:port like `192.168.0.1:3509,192.168.0.2:3555`.")
verbose = flag.Bool("v", false, "Verbose logging")
printVersion = flag.Bool("version", false, "Print version")
promHostPort = flag.String("prom", "", "Enable prometheus metrics. May cause pauses. Example: '0.0.0.0:9090'")
storage = flag.String("s", "", "Set path to file location for persistent storage. Data will be stored in memopry if not set.")
)

// Version should be replaced at build time
Expand All @@ -31,6 +33,7 @@ var Build = "unknown"

func main() {
preSharedSecret := os.Getenv("DRACULA_SECRET")
storagePath := ""
flag.Parse()
if *help {
flag.Usage()
Expand All @@ -40,8 +43,11 @@ func main() {
fmt.Println(Version, Build)
return
}
if *secret != "" {
preSharedSecret = *secret
if *key != "" {
preSharedSecret = *key
}
if *storage != "" {
storagePath = *storage
}
var s *server.Server
peerList := strings.Trim(*peers, " \n")
Expand All @@ -51,12 +57,12 @@ func main() {
os.Exit(1)
}
if len(peerList) > 0 {
s = server.NewServerWithPeers(*expireAfterSecs, preSharedSecret, *peerIPPort, peerList)
s = server.NewServerWithPeers(*expireAfterSecs, preSharedSecret, *peerIPPort, peerList, storagePath)
if *verbose {
fmt.Printf("dracula server cluster mode enabled: self=%s; peers=%s \n", *peerIPPort, s.Peers())
}
} else {
s = server.NewServer(*expireAfterSecs, preSharedSecret)
s = server.NewServer(*expireAfterSecs, preSharedSecret, storagePath)
}
if *verbose {
s.DebugEnable(fmt.Sprintf("udp:%d, tcp:%d, http:%s -", *port, *tcpPort, *restHostPort))
Expand Down
37 changes: 35 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
module github.com/mailsac/dracula

go 1.16
go 1.22.4

require (
github.com/OneOfOne/xxhash v1.2.8
github.com/emirpasic/gods v1.18.1
github.com/maxtek6/keybase-go v0.0.0-20240628230334-94dfb6206525
github.com/prometheus/client_golang v1.16.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/huandu/go-sqlbuilder v1.27.3 // indirect
github.com/huandu/xstrings v1.5.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
golang.org/x/sys v0.21.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/gc/v3 v3.0.0-20240304020402-f0dba7c97c2b // indirect
modernc.org/libc v1.53.4 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.8.0 // indirect
modernc.org/sqlite v1.30.1 // indirect
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
)
Loading
Loading