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

using the slog #25

Merged
merged 1 commit into from
Jun 17, 2024
Merged
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
12 changes: 8 additions & 4 deletions elect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package goelect

import (
"context"
"log/slog"
"os"
"time"

"github.com/danl5/goelect/pkg/config"
"github.com/danl5/goelect/pkg/consensus"
"github.com/danl5/goelect/pkg/log"
"github.com/danl5/goelect/pkg/model"
)

Expand All @@ -26,7 +27,10 @@ func NewElect(
trans model.Transport,
transConfig model.TransportConfig,
cfg *ElectConfig,
logger log.Logger) (*Elect, error) {
logger *slog.Logger) (*Elect, error) {
if logger == nil {
logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
}
var peers []config.NodeConfig
for _, n := range cfg.Peers {
peers = append(peers, config.NodeConfig{
Expand Down Expand Up @@ -73,7 +77,7 @@ func NewElect(
}
return &Elect{
cfg: cfg,
logger: logger,
logger: logger.With("component", "elect"),
callBackTimeout: cfg.CallBackTimeout,
consensus: c,
callBacks: cfg.CallBacks,
Expand All @@ -95,7 +99,7 @@ type Elect struct {
// cfg is the configuration for the election
cfg *ElectConfig
// logger is used for logging
logger log.Logger
logger *slog.Logger
}

// Run is the main function of the Elect struct
Expand Down
3 changes: 2 additions & 1 deletion examples/onenode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"flag"
"fmt"
"log/slog"
"os"
"strings"
"time"

Expand Down Expand Up @@ -42,7 +43,7 @@ func newElect() (*goelect.Elect, error) {
peerNodes = append(peerNodes, goelect.Node{Address: pa, ID: pa})
}

logger := slog.Default()
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))

// rpc transport
rpcTransport, err := rpc.NewRPC(logger)
Expand Down
12 changes: 8 additions & 4 deletions pkg/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package consensus
import (
"context"
"fmt"
"log/slog"
"math/rand"
"sync"
"time"
Expand All @@ -12,7 +13,6 @@ import (

"github.com/danl5/goelect/pkg/common"
"github.com/danl5/goelect/pkg/config"
"github.com/danl5/goelect/pkg/log"
"github.com/danl5/goelect/pkg/model"
)

Expand All @@ -21,14 +21,18 @@ func NewConsensus(
trans model.Transport,
transConfig model.TransportConfig,
cfg *config.Config,
logger log.Logger) (*Consensus, error) {
logger *slog.Logger) (*Consensus, error) {
if err := node.Validate(); err != nil {
return nil, err
}

if logger == nil {
return nil, fmt.Errorf("new consensus, logger is nil")
}

c := &Consensus{
cfg: cfg,
logger: logger,
logger: logger.With("component", "consensus"),
node: node,
termCache: &termCache{},
transport: trans,
Expand All @@ -52,7 +56,7 @@ type Consensus struct {
// cfg is the configuration for the consensus
cfg *config.Config
// logger
logger log.Logger
logger *slog.Logger

// node is the elect node of the model
node model.ElectNode
Expand Down
5 changes: 2 additions & 3 deletions pkg/consensus/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"github.com/stretchr/testify/assert"

"github.com/danl5/goelect/pkg/common"
"github.com/danl5/goelect/pkg/log"
"github.com/danl5/goelect/pkg/model"
)

func TestConsensus_HeartBeat(t *testing.T) {
type fields struct {
termCache *termCache
logger log.Logger
logger *slog.Logger
eventChan chan model.NodeEvent
}
type args struct {
Expand Down Expand Up @@ -93,7 +92,7 @@ func TestConsensus_HeartBeat(t *testing.T) {
func TestConsensus_RequestVote(t *testing.T) {
type fields struct {
termCache *termCache
logger log.Logger
logger *slog.Logger
fsm *fsm.FSM
eventChan chan model.NodeEvent
}
Expand Down
37 changes: 0 additions & 37 deletions pkg/log/log.go

This file was deleted.

4 changes: 3 additions & 1 deletion pkg/model/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package model

import "errors"
import (
"errors"
)

var (
ErrorBadCommand = errors.New("bad command")
Expand Down
16 changes: 10 additions & 6 deletions pkg/transport/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"crypto/x509"
"errors"
"fmt"
"log/slog"
"net"
"net/rpc"
"os"
"reflect"
"sync"
"time"

"github.com/danl5/goelect/pkg/log"
"github.com/danl5/goelect/pkg/model"
"github.com/mitchellh/mapstructure"
"github.com/silenceper/pool"
Expand All @@ -30,13 +30,17 @@ const (
poolMaxCap = 20
)

func NewRPC(logger log.Logger) (*RPC, error) {
func NewRPC(logger *slog.Logger) (*RPC, error) {
if logger == nil {
return nil, fmt.Errorf("new rpc, logger is nil")
}

rpc := &RPC{
Server: Server{
logger: logger,
logger: logger.With("component", "rpc server"),
},
Client: Client{
logger: logger,
logger: logger.With("component", "rpc client"),
},
}

Expand Down Expand Up @@ -102,7 +106,7 @@ func (r *RPC) Decode(raw any, target any) error {

type Server struct {
rpcHandler *RPCHandler
logger log.Logger
logger *slog.Logger
}

// Start initiates the server to begin listening on the specified address.
Expand Down Expand Up @@ -209,7 +213,7 @@ type Client struct {
// string -> pool.Pool
clients sync.Map

logger log.Logger
logger *slog.Logger
}

// InitConnections initializes a set of connections to the given nodes.
Expand Down
Loading