Moved main.go file to cmd subfolder. Renamed src folder to pkg folder as it will contain all the importable package code. Moved config.go to new internals folder

This commit is contained in:
Kelvin Mwinuka
2024-03-25 21:13:40 +08:00
parent c72e982833
commit 5c86fb6215
58 changed files with 2925 additions and 2906 deletions

View File

@@ -1,5 +1,5 @@
build-server:
CC=$(CC) GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o $(DEST)/server ./src/*.go
CC=$(CC) GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o $(DEST)/server ./cmd/main.go
build:
env CC=x86_64-linux-musl-gcc GOOS=linux GOARCH=amd64 DEST=bin/linux/x86_64 make build-server
@@ -8,4 +8,4 @@ run:
make build && docker-compose up --build
test:
go clean -testcache && go test ./src/... -coverprofile coverage/coverage.out
go clean -testcache && go test ./pkg/... -coverprofile coverage/coverage.out

View File

@@ -16,18 +16,19 @@ package main
import (
"context"
"github.com/echovault/echovault/src/echovault"
"github.com/echovault/echovault/src/modules/acl"
"github.com/echovault/echovault/src/modules/admin"
"github.com/echovault/echovault/src/modules/connection"
"github.com/echovault/echovault/src/modules/generic"
"github.com/echovault/echovault/src/modules/hash"
"github.com/echovault/echovault/src/modules/list"
"github.com/echovault/echovault/src/modules/pubsub"
"github.com/echovault/echovault/src/modules/set"
"github.com/echovault/echovault/src/modules/sorted_set"
str "github.com/echovault/echovault/src/modules/string"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/echovault"
"github.com/echovault/echovault/pkg/modules/acl"
"github.com/echovault/echovault/pkg/modules/admin"
"github.com/echovault/echovault/pkg/modules/connection"
"github.com/echovault/echovault/pkg/modules/generic"
"github.com/echovault/echovault/pkg/modules/hash"
"github.com/echovault/echovault/pkg/modules/list"
"github.com/echovault/echovault/pkg/modules/pubsub"
"github.com/echovault/echovault/pkg/modules/set"
"github.com/echovault/echovault/pkg/modules/sorted_set"
str "github.com/echovault/echovault/pkg/modules/string"
"github.com/echovault/echovault/pkg/utils"
"log"
"os"
"os/signal"
@@ -50,7 +51,7 @@ func GetCommands() []utils.Command {
}
func main() {
config, err := utils.GetConfig()
config, err := internal.GetConfig()
if err != nil {
log.Fatal(err)

File diff suppressed because it is too large Load Diff

View File

@@ -12,13 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package utils
package internal
import (
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/echovault/echovault/pkg/utils"
"log"
"os"
"path"
@@ -98,7 +99,7 @@ The options are 'always' for syncing on each command, 'everysec' to sync every s
flag.Func("max-memory", `Upper memory limit before triggering eviction.
Supported units (kb, mb, gb, tb, pb). When 0 is passed, there will be no memory limit.
There is no limit by default.`, func(memory string) error {
b, err := ParseMemory(memory)
b, err := utils.ParseMemory(memory)
if err != nil {
return err
}
@@ -106,7 +107,7 @@ There is no limit by default.`, func(memory string) error {
return nil
})
evictionPolicy := NoEviction
evictionPolicy := utils.NoEviction
flag.Func("eviction-policy", `The eviction policy used to remove keys when max-memory is reached. The options are:
1) noeviction - Do not evict any keys even when max-memory is exceeded.
2) allkeys-lfu - Evict the least frequently used keys.
@@ -116,9 +117,9 @@ There is no limit by default.`, func(memory string) error {
6) allkeys-random - Evict random keys until we get under the max-memory limit.
7) volatile-random - Evict random keys with an expiration.`, func(policy string) error {
policies := []string{
NoEviction,
AllKeysLFU, AllKeysLRU, AllKeysRandom,
VolatileLFU, VolatileLRU, VolatileRandom,
utils.NoEviction,
utils.AllKeysLFU, utils.AllKeysLRU, utils.AllKeysRandom,
utils.VolatileLFU, utils.VolatileLRU, utils.VolatileRandom,
}
policyIdx := slices.Index(policies, strings.ToLower(policy))
if policyIdx == -1 {
@@ -131,7 +132,7 @@ There is no limit by default.`, func(memory string) error {
tls := flag.Bool("tls", false, "Start the echovault in TLS mode. Default is false.")
mtls := flag.Bool("mtls", false, "Use mTLS to verify the client.")
port := flag.Int("port", 7480, "Port to use. Default is 7480")
serverId := flag.String("echovault-id", "1", "EchoVault ID in raft cluster. Leave empty for client.")
serverId := flag.String("server-id", "1", "EchoVault ID in raft cluster. Leave empty for client.")
joinAddr := flag.String("join-addr", "", "Address of cluster member in a cluster to you want to join.")
bindAddr := flag.String("bind-addr", "", "Address to bind the echovault to.")
raftBindPort := flag.Uint("raft-port", 7481, "Port to use for intra-cluster communication. Leave on the client.")

View File

@@ -16,9 +16,9 @@ package aof
import (
"fmt"
logstore "github.com/echovault/echovault/src/aof/log"
"github.com/echovault/echovault/src/aof/preamble"
"github.com/echovault/echovault/src/utils"
logstore "github.com/echovault/echovault/pkg/aof/log"
"github.com/echovault/echovault/pkg/aof/preamble"
"github.com/echovault/echovault/pkg/utils"
"log"
"sync"
)

View File

@@ -17,7 +17,7 @@ package preamble
import (
"encoding/json"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"io"
"log"
"os"

View File

@@ -18,7 +18,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"time"
)

View File

@@ -20,12 +20,13 @@ import (
"crypto/x509"
"errors"
"fmt"
"github.com/echovault/echovault/src/aof"
"github.com/echovault/echovault/src/eviction"
"github.com/echovault/echovault/src/memberlist"
"github.com/echovault/echovault/src/raft"
"github.com/echovault/echovault/src/snapshot"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/aof"
"github.com/echovault/echovault/pkg/eviction"
"github.com/echovault/echovault/pkg/memberlist"
"github.com/echovault/echovault/pkg/raft"
"github.com/echovault/echovault/pkg/snapshot"
"github.com/echovault/echovault/pkg/utils"
"io"
"log"
"net"
@@ -37,7 +38,7 @@ import (
type EchoVault struct {
// config holds the echovault configuration variables.
config utils.Config
config internal.Config
// The current index for the latest connection id.
// This number is incremented everytime there's a new connection and
@@ -90,7 +91,7 @@ func WithContext(ctx context.Context) func(echovault *EchoVault) {
}
}
func WithConfig(config utils.Config) func(echovault *EchoVault) {
func WithConfig(config internal.Config) func(echovault *EchoVault) {
return func(echovault *EchoVault) {
echovault.config = config
}

View File

@@ -18,7 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"log"
"math/rand"
"runtime"

View File

@@ -18,7 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"net"
"strings"
)

View File

@@ -18,7 +18,8 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/utils"
"github.com/hashicorp/memberlist"
"github.com/hashicorp/raft"
"log"
@@ -30,7 +31,7 @@ type Delegate struct {
}
type DelegateOpts struct {
config utils.Config
config internal.Config
broadcastQueue *memberlist.TransmitLimitedQueue
addVoter func(id raft.ServerID, address raft.ServerAddress, prevIndex uint64, timeout time.Duration) error
isRaftLeader func() bool

View File

@@ -18,10 +18,11 @@ import (
"context"
"crypto/md5"
"fmt"
"github.com/echovault/echovault/internal"
"log"
"time"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"github.com/hashicorp/memberlist"
"github.com/hashicorp/raft"
"github.com/sethvargo/go-retry"
@@ -34,7 +35,7 @@ type NodeMeta struct {
}
type Opts struct {
Config utils.Config
Config internal.Config
HasJoinedCluster func() bool
AddVoter func(id raft.ServerID, address raft.ServerAddress, prevIndex uint64, timeout time.Duration) error
RemoveRaftServer func(meta NodeMeta) error

View File

@@ -20,7 +20,8 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/utils"
"github.com/gobwas/glob"
"gopkg.in/yaml.v3"
"log"
@@ -43,11 +44,11 @@ type ACL struct {
Users []*User // List of ACL user profiles
UsersMutex sync.RWMutex // RWMutex for concurrency control when accessing ACL profile list
Connections map[*net.Conn]Connection // Connections to the echovault that are currently registered with the ACL module
Config utils.Config // EchoVault configuration that contains the relevant ACL config options
Config internal.Config // EchoVault configuration that contains the relevant ACL config options
GlobPatterns map[string]glob.Glob
}
func NewACL(config utils.Config) *ACL {
func NewACL(config internal.Config) *ACL {
var users []*User
// 1. Initialise default ACL user

View File

@@ -19,7 +19,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"gopkg.in/yaml.v3"
"log"
"net"

View File

@@ -18,8 +18,9 @@ import (
"context"
"crypto/sha256"
"fmt"
"github.com/echovault/echovault/src/echovault"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/echovault"
"github.com/echovault/echovault/pkg/utils"
"github.com/tidwall/resp"
"net"
"slices"
@@ -45,7 +46,7 @@ func init() {
}
func setUpServer(bindAddr string, port uint16, requirePass bool, aclConfig string) *echovault.EchoVault {
config := utils.Config{
config := internal.Config{
BindAddr: bindAddr,
Port: port,
DataDir: "",

View File

@@ -18,7 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"github.com/gobwas/glob"
"net"
"slices"

View File

@@ -18,15 +18,16 @@ import (
"bytes"
"context"
"fmt"
"github.com/echovault/echovault/src/echovault"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/echovault"
"github.com/echovault/echovault/pkg/utils"
"github.com/tidwall/resp"
"testing"
)
func Test_CommandsHandler(t *testing.T) {
mockServer := echovault.NewEchoVault(
echovault.WithConfig(utils.Config{
echovault.WithConfig(internal.Config{
DataDir: "",
EvictionPolicy: utils.NoEviction,
}),

View File

@@ -18,7 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"net"
)

View File

@@ -18,8 +18,9 @@ import (
"bytes"
"context"
"errors"
"github.com/echovault/echovault/src/echovault"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/echovault"
"github.com/echovault/echovault/pkg/utils"
"github.com/tidwall/resp"
"testing"
)
@@ -28,7 +29,7 @@ var mockServer *echovault.EchoVault
func init() {
mockServer = echovault.NewEchoVault(
echovault.WithConfig(utils.Config{
echovault.WithConfig(internal.Config{
DataDir: "",
EvictionPolicy: utils.NoEviction,
}),

View File

@@ -19,7 +19,7 @@ import (
"errors"
"flag"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"log"
"net"
"strconv"

View File

@@ -19,8 +19,9 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/echovault"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/echovault"
"github.com/echovault/echovault/pkg/utils"
"github.com/tidwall/resp"
"testing"
"time"
@@ -30,7 +31,7 @@ var mockServer *echovault.EchoVault
func init() {
mockServer = echovault.NewEchoVault(
echovault.WithConfig(utils.Config{
echovault.WithConfig(internal.Config{
DataDir: "",
EvictionPolicy: utils.NoEviction,
}),

View File

@@ -16,7 +16,7 @@ package generic
import (
"errors"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
)
func setKeyFunc(cmd []string) ([]string, error) {

View File

@@ -18,7 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"math/rand"
"net"
"slices"

View File

@@ -19,8 +19,9 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/echovault"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/echovault"
"github.com/echovault/echovault/pkg/utils"
"github.com/tidwall/resp"
"slices"
"testing"
@@ -30,7 +31,7 @@ var mockServer *echovault.EchoVault
func init() {
mockServer = echovault.NewEchoVault(
echovault.WithConfig(utils.Config{
echovault.WithConfig(internal.Config{
DataDir: "",
EvictionPolicy: utils.NoEviction,
}),

View File

@@ -16,7 +16,7 @@ package hash
import (
"errors"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
)
func hsetKeyFunc(cmd []string) ([]string, error) {

View File

@@ -18,7 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"math"
"net"
"slices"

View File

@@ -19,8 +19,9 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/echovault"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/echovault"
"github.com/echovault/echovault/pkg/utils"
"github.com/tidwall/resp"
"testing"
)
@@ -29,7 +30,7 @@ var mockServer *echovault.EchoVault
func init() {
mockServer = echovault.NewEchoVault(
echovault.WithConfig(utils.Config{
echovault.WithConfig(internal.Config{
DataDir: "",
EvictionPolicy: utils.NoEviction,
}),

View File

@@ -16,7 +16,7 @@ package list
import (
"errors"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
)
func lpushKeyFunc(cmd []string) ([]string, error) {

View File

@@ -18,7 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"net"
"strings"
)

View File

@@ -18,8 +18,9 @@ import (
"bytes"
"context"
"fmt"
"github.com/echovault/echovault/src/echovault"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/echovault"
"github.com/echovault/echovault/pkg/utils"
"github.com/tidwall/resp"
"net"
"slices"
@@ -45,7 +46,7 @@ func setUpServer(pubsub *PubSub, bindAddr string, port uint16) *echovault.EchoVa
return echovault.NewEchoVault(
echovault.WithPubSub(pubsub),
echovault.WithCommands(Commands()),
echovault.WithConfig(utils.Config{
echovault.WithConfig(internal.Config{
BindAddr: bindAddr,
Port: port,
DataDir: "",

View File

@@ -18,7 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"net"
"slices"
"strings"

View File

@@ -19,8 +19,9 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/echovault"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/echovault"
"github.com/echovault/echovault/pkg/utils"
"github.com/tidwall/resp"
"slices"
"testing"
@@ -30,7 +31,7 @@ var mockServer *echovault.EchoVault
func init() {
mockServer = echovault.NewEchoVault(
echovault.WithConfig(utils.Config{
echovault.WithConfig(internal.Config{
DataDir: "",
EvictionPolicy: utils.NoEviction,
}),

View File

@@ -16,7 +16,7 @@ package set
import (
"errors"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"slices"
"strings"
)

View File

@@ -15,7 +15,7 @@
package set
import (
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"math/rand"
"slices"
)

View File

@@ -19,7 +19,7 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"math"
"net"
"slices"

View File

@@ -19,8 +19,9 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/echovault"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/echovault"
"github.com/echovault/echovault/pkg/utils"
"github.com/tidwall/resp"
"math"
"slices"
@@ -32,7 +33,7 @@ var mockServer *echovault.EchoVault
func init() {
mockServer = echovault.NewEchoVault(
echovault.WithConfig(utils.Config{
echovault.WithConfig(internal.Config{
DataDir: "",
EvictionPolicy: utils.NoEviction,
}),

View File

@@ -16,7 +16,7 @@ package sorted_set
import (
"errors"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"slices"
"strings"
)

View File

@@ -17,7 +17,7 @@ package sorted_set
import (
"cmp"
"errors"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"math"
"math/rand"
"slices"

View File

@@ -18,7 +18,7 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"net"
)

View File

@@ -19,8 +19,9 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/echovault"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/echovault"
"github.com/echovault/echovault/pkg/utils"
"github.com/tidwall/resp"
"strconv"
"testing"
@@ -30,7 +31,7 @@ var mockServer *echovault.EchoVault
func init() {
mockServer = echovault.NewEchoVault(
echovault.WithConfig(utils.Config{
echovault.WithConfig(internal.Config{
DataDir: "",
EvictionPolicy: utils.NoEviction,
}),

View File

@@ -16,7 +16,7 @@ package str
import (
"errors"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
)
func setRangeKeyFunc(cmd []string) ([]string, error) {

View File

@@ -16,14 +16,15 @@ package raft
import (
"encoding/json"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/utils"
"github.com/hashicorp/raft"
"strconv"
"strings"
)
type SnapshotOpts struct {
config utils.Config
config internal.Config
data map[string]utils.KeyData
startSnapshot func()
finishSnapshot func()

View File

@@ -18,7 +18,8 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/utils"
"github.com/hashicorp/raft"
"io"
"log"
@@ -26,7 +27,7 @@ import (
)
type FSMOpts struct {
Config utils.Config
Config internal.Config
EchoVault utils.EchoVault
GetCommand func(command string) (utils.Command, error)
DeleteKey func(ctx context.Context, key string) error

View File

@@ -18,20 +18,21 @@ import (
"context"
"errors"
"fmt"
"github.com/echovault/echovault/src/memberlist"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/pkg/memberlist"
"log"
"net"
"os"
"path/filepath"
"time"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"github.com/hashicorp/raft"
raftboltdb "github.com/hashicorp/raft-boltdb"
)
type Opts struct {
Config utils.Config
Config internal.Config
EchoVault utils.EchoVault
GetCommand func(command string) (utils.Command, error)
DeleteKey func(ctx context.Context, key string) error

View File

@@ -19,7 +19,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/echovault/echovault/src/utils"
"github.com/echovault/echovault/pkg/utils"
"io"
"io/fs"
"log"