Skip to content

Commit

Permalink
Fix configuration initialization and node configuration update.
Browse files Browse the repository at this point in the history
This commit addresses multiple issues related to configuration initialization and node configuration update.

The init function InitConfig in the config package has been updated to correctly initialize the GlobalConfig. The config.NewConfig function has been deprecated and removed.

Additionally, the node main function has been updated to use the new InitConfig function to initialize the config.

Moreover, the config.yml file has been updated to include new nodes with their respective host and port configurations.

This change ensures proper configuration initialization and node configuration update, enabling correct functionality of the system.
  • Loading branch information
HannahMarsh committed Jun 19, 2024
1 parent 9a62f19 commit 5c4da3f
Show file tree
Hide file tree
Showing 19 changed files with 802 additions and 332 deletions.
7 changes: 4 additions & 3 deletions cmd/bulletin-board/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ func main() {

ctx, cancel := context.WithCancel(context.Background())

cfg, err := config.NewConfig()
if err != nil {
slog.Error("failed get config", err)
if err := config.InitConfig(); err != nil {
slog.Error("failed to init config", err)
os.Exit(1)
}

cfg := config.GlobalConfig

host := cfg.BulletinBoard.Host
port := cfg.BulletinBoard.Port

Expand Down
7 changes: 4 additions & 3 deletions cmd/clients/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ func main() {

ctx, cancel := context.WithCancel(context.Background())

cfg, err := config.NewConfig()
if err != nil {
slog.Error("failed get config", err)
if err = config.InitConfig(); err != nil {
slog.Error("failed to init config", err)
os.Exit(1)
}

cfg := config.GlobalConfig

slog.Info("⚡ init client", "heartbeat_interval", cfg.HeartbeatInterval)

// set up logrus
Expand Down
20 changes: 11 additions & 9 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Node struct {
Port int `yaml:"port"`
}

type GlobalConfig struct {
type Config struct {
ServerLoad int `yaml:"server_load"`
HeartbeatInterval int `yaml:"heartbeat_interval"`
MinNodes int `yaml:"min_nodes"`
Expand All @@ -30,16 +30,18 @@ type GlobalConfig struct {
Nodes []Node `yaml:"nodes"`
}

func NewConfig() (*GlobalConfig, error) {
cfg := &GlobalConfig{}
var GlobalConfig *Config

func InitConfig() error {
GlobalConfig = &Config{}

if dir, err := os.Getwd(); err != nil {
return nil, fmt.Errorf("config.NewConfig(): global config error: %w", err)
} else if err2 := cleanenv.ReadConfig(dir+"/cmd/config/config.yml", cfg); err2 != nil {
return nil, fmt.Errorf("config.NewConfig(): global config error: %w", err2)
} else if err3 := cleanenv.ReadEnv(cfg); err3 != nil {
return nil, fmt.Errorf("config.NewConfig(): global config error: %w", err3)
return fmt.Errorf("config.NewConfig(): global config error: %w", err)
} else if err2 := cleanenv.ReadConfig(dir+"/cmd/config/config.yml", GlobalConfig); err2 != nil {
return fmt.Errorf("config.NewConfig(): global config error: %w", err2)
} else if err3 := cleanenv.ReadEnv(GlobalConfig); err3 != nil {
return fmt.Errorf("config.NewConfig(): global config error: %w", err3)
} else {
return cfg, nil
return nil
}
}
11 changes: 10 additions & 1 deletion cmd/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ nodes:
port: 8081
- id: 2
host: 'localhost'
port: 8082
port: 8082
- id: 3
host: 'localhost'
port: 8083
- id: 4
host: 'localhost'
port: 8084
- id: 5
host: 'localhost'
port: 8085
7 changes: 4 additions & 3 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ func main() {

ctx, cancel := context.WithCancel(context.Background())

cfg, err := config.NewConfig()
if err != nil {
slog.Error("failed get config", err)
if err = config.InitConfig(); err != nil {
slog.Error("failed to init config", err)
os.Exit(1)
}

cfg := config.GlobalConfig

var nodeConfig *config.Node
for _, n := range cfg.Nodes {
if n.ID == *id {
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
module github.com/HannahMarsh/pi_t-experiment

go 1.19
go 1.21

toolchain go1.21.2

require (
github.com/enriquebris/goconcurrentqueue v0.7.0
github.com/emirpasic/gods v1.18.1
github.com/google/uuid v1.3.0
github.com/google/wire v0.5.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.12.0
github.com/ilyakaznacheev/cleanenv v1.3.0
github.com/jfcg/sorty/v2 v2.1.1
github.com/kyleconroy/sqlc v1.16.0
github.com/lib/pq v1.10.7
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/pkg/errors v0.9.1
github.com/rabbitmq/amqp091-go v1.5.0
github.com/sirupsen/logrus v1.9.0
github.com/thoas/go-funk v0.9.3
go.uber.org/automaxprocs v1.5.1
golang.org/x/exp v0.0.0-20221026153819-32f3d567a233
google.golang.org/genproto v0.0.0-20221014213838-99cd37c6964a
Expand All @@ -26,6 +27,7 @@ require (
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/jfcg/sixb v1.4.1 // indirect
github.com/joho/godotenv v1.4.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
Expand Down
20 changes: 12 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/enriquebris/goconcurrentqueue v0.7.0 h1:JYrDa45N3xo3Sr9mjvlRaWiBHvBEJIhAdLXO3VGVghA=
github.com/enriquebris/goconcurrentqueue v0.7.0/go.mod h1:OZ+KC2BcRYzjg0vgoUs1GFqdAjkD9mz2Ots7Jbm1yS4=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ=
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
Expand All @@ -14,6 +14,7 @@ github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand All @@ -23,6 +24,14 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.12.0 h1:kr3j8iIMR4ywO/O0rvksXaJvauG
github.com/grpc-ecosystem/grpc-gateway/v2 v2.12.0/go.mod h1:ummNFgdgLhhX7aIiy35vVmQNS0rWXknfPE0qe6fmFXg=
github.com/ilyakaznacheev/cleanenv v1.3.0 h1:RapuLclPPUbmdd5Bi5UXScwMEZA6+ZNLU5OW9itPjj0=
github.com/ilyakaznacheev/cleanenv v1.3.0/go.mod h1:i0owW+HDxeGKE0/JPREJOdSCPIyOnmh6C0xhWAkF/xA=
github.com/jfcg/opt v0.3.1 h1:6zgKvv3fR5OlX2nxUYJC4wtosY30N4vypILgXmRNr34=
github.com/jfcg/opt v0.3.1/go.mod h1:3ZUYQhiqKM6vVjMRYV1fVZ9a91EQ47b5kg7KsnfRClk=
github.com/jfcg/rng v1.0.6 h1:JCYvI/GaSSd3lL0zl15J7FNHqMZYNosPmNKDrysKhH0=
github.com/jfcg/rng v1.0.6/go.mod h1:UqYFfcn9XCugyejaC+8cXQgrWtHQtvlWGiLQXu/7Cnw=
github.com/jfcg/sixb v1.4.1 h1:lb/fWXTn7G+Om2K2/NhZByppgPyQ7mWyQ5XOHu9PAfU=
github.com/jfcg/sixb v1.4.1/go.mod h1:hofNeC6Ua8uwQ7X14L2/byXF1xJyUyX4lk53SmeAopI=
github.com/jfcg/sorty/v2 v2.1.1 h1:jMgkME/JZ4dVFxOVtAeQUXbSzLhbPGxjgfhtnCMXXVM=
github.com/jfcg/sorty/v2 v2.1.1/go.mod h1:wFv8kNl8smeqwsx62BPpgxyjxMY+4ylIug1ARe4nLnI=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -36,28 +45,24 @@ github.com/kyleconroy/sqlc v1.16.0 h1:PE5xrrnUiV5T2b97sLWKHgpBPQoPo/N1K/gWU/GFwa
github.com/kyleconroy/sqlc v1.16.0/go.mod h1:m+cX/UyBRnKP58lFfUsq+0gw87UUw9AmxwqU/AaQeDA=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/orcaman/concurrent-map/v2 v2.0.1 h1:jOJ5Pg2w1oeB6PeDurIYf6k9PQ+aTITr/6lP/L/zp6c=
github.com/orcaman/concurrent-map/v2 v2.0.1/go.mod h1:9Eq3TG2oBe5FirmYWQfYO5iH1q0Jv47PLaNK++uCdOM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
github.com/rabbitmq/amqp091-go v1.5.0 h1:VouyHPBu1CrKyJVfteGknGOGCzmOz0zcv/tONLkb7rg=
github.com/rabbitmq/amqp091-go v1.5.0/go.mod h1:JsV0ofX5f1nwOGafb8L5rBItt9GyhfQfcJj+oyz0dGg=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw=
github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.uber.org/automaxprocs v1.5.1 h1:e1YG66Lrk73dn4qhg8WFSvhF0JuFQF0ERIp4rpuV8Qk=
go.uber.org/automaxprocs v1.5.1/go.mod h1:BF4eumQw0P9GtnuxxovUd06vwm1o18oMzFtK66vU6XU=
Expand Down Expand Up @@ -111,7 +116,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
4 changes: 2 additions & 2 deletions internal/api/nodeApi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import "time"
type PublicNodeApi struct {
ID int
Address string
PublicKey []byte
PublicKey string
}

type PrivateNodeApi struct {
TimeOfRequest time.Time
ID int
Address string
PublicKey []byte
PublicKey string
MessageQueue []int
}
2 changes: 1 addition & 1 deletion internal/bulletin_board/NodeView.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type NodeView struct {
ID int
Address string
PublicKey []byte
PublicKey string
MessageQueue []int
mu sync.RWMutex
LastHeartbeat time.Time
Expand Down
4 changes: 2 additions & 2 deletions internal/bulletin_board/bulletin_board.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
type BulletinBoard struct {
Network map[int]*NodeView // Maps node IDs to their queue sizes
mu sync.RWMutex
config *config.GlobalConfig
config *config.Config
}

// NewBulletinBoard creates a new bulletin board
func NewBulletinBoard(config *config.GlobalConfig) *BulletinBoard {
func NewBulletinBoard(config *config.Config) *BulletinBoard {
return &BulletinBoard{
Network: make(map[int]*NodeView),
config: config,
Expand Down
Loading

0 comments on commit 5c4da3f

Please sign in to comment.