Skip to content

Commit

Permalink
Slog: use mutex pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed Apr 3, 2024
1 parent 5dc81f2 commit 4ad26b6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func (s *Server) RegisterRoutes(routeRegistrer func(*Server, *Router)) {
func (s *Server) Stop() {
if s.sigChannel != nil {
signal.Stop(s.sigChannel)
close(s.sigChannel)
close(s.sigChannel) // FIXME close of closed channel
}
state := s.state.Swap(3)
if state == 0 || state == 3 {
Expand Down
10 changes: 8 additions & 2 deletions slog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ type DevModeHandlerOptions struct {
// performance and its output is not machine-readable.
type DevModeHandler struct {
opts *DevModeHandlerOptions
mu *sync.Mutex
w io.Writer
attrs []slog.Attr
groups []string
mu sync.Mutex
}

// NewHandler creates a new `slog.Handler` with default options.
Expand All @@ -71,7 +71,11 @@ func NewDevModeHandler(w io.Writer, opts *DevModeHandlerOptions) *DevModeHandler
if opts == nil {
opts = &DevModeHandlerOptions{}
}
return &DevModeHandler{w: w, opts: opts}
return &DevModeHandler{
w: w,
mu: &sync.Mutex{},
opts: opts,
}
}

// Handle formats its argument `Record` in an output easily readable by humans.
Expand Down Expand Up @@ -177,6 +181,7 @@ func (h *DevModeHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &DevModeHandler{
opts: h.opts,
w: h.w,
mu: &sync.Mutex{},
attrs: newAttrs,
groups: h.groups,
}
Expand All @@ -189,6 +194,7 @@ func (h *DevModeHandler) WithGroup(name string) slog.Handler {
return &DevModeHandler{
opts: h.opts,
w: h.w,
mu: &sync.Mutex{},
attrs: append(make([]slog.Attr, 0, len(h.attrs)), h.attrs...),
groups: append(h.groups, name),
}
Expand Down
23 changes: 12 additions & 11 deletions slog/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"log/slog"
"runtime"
"sync"
"testing"
"time"

Expand All @@ -29,7 +30,7 @@ func TestNewHandler(t *testing.T) {
{
devMode: true,
w: bytes.NewBuffer(make([]byte, 0, 10)),
want: &DevModeHandler{w: bytes.NewBuffer(make([]byte, 0, 10)), opts: &DevModeHandlerOptions{Level: slog.LevelDebug}},
want: &DevModeHandler{w: bytes.NewBuffer(make([]byte, 0, 10)), mu: &sync.Mutex{}, opts: &DevModeHandlerOptions{Level: slog.LevelDebug}},
},
{
devMode: false,
Expand Down Expand Up @@ -57,12 +58,12 @@ func TestDevModeHandler(t *testing.T) {
{
desc: "nil_options",
opts: nil,
want: &DevModeHandler{w: bytes.NewBuffer(make([]byte, 0, 10)), opts: &DevModeHandlerOptions{}},
want: &DevModeHandler{w: bytes.NewBuffer(make([]byte, 0, 10)), mu: &sync.Mutex{}, opts: &DevModeHandlerOptions{}},
},
{
desc: "log_level_options",
opts: &DevModeHandlerOptions{Level: slog.LevelError},
want: &DevModeHandler{w: bytes.NewBuffer(make([]byte, 0, 10)), opts: &DevModeHandlerOptions{Level: slog.LevelError}},
want: &DevModeHandler{w: bytes.NewBuffer(make([]byte, 0, 10)), mu: &sync.Mutex{}, opts: &DevModeHandlerOptions{Level: slog.LevelError}},
},
}

Expand Down Expand Up @@ -141,31 +142,31 @@ func TestDevModeHandler(t *testing.T) {
}{
{
desc: "copy_empty",
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, attrs: []slog.Attr{}},
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, mu: &sync.Mutex{}, attrs: []slog.Attr{}},
},
{
desc: "copy",
baseAttrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)},
baseGroups: []string{"group"},
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, attrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)}, groups: []string{"group"}},
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, mu: &sync.Mutex{}, attrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)}, groups: []string{"group"}},
},
{
desc: "append_empty",
attrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)},
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, attrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)}},
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, mu: &sync.Mutex{}, attrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)}},
},
{
desc: "append",
baseAttrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)},
attrs: []slog.Attr{slog.String("c", "c"), slog.Int("d", 3)},
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, attrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2), slog.String("c", "c"), slog.Int("d", 3)}},
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, mu: &sync.Mutex{}, attrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2), slog.String("c", "c"), slog.Int("d", 3)}},
},
{
desc: "append_with_group",
baseGroups: []string{"group"},
baseAttrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)},
attrs: []slog.Attr{slog.String("c", "c"), slog.Int("d", 3)},
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, attrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2), slog.String("c", "c"), slog.Int("d", 3)}, groups: []string{"group"}},
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, mu: &sync.Mutex{}, attrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2), slog.String("c", "c"), slog.Int("d", 3)}, groups: []string{"group"}},
},
}

Expand Down Expand Up @@ -195,20 +196,20 @@ func TestDevModeHandler(t *testing.T) {
desc: "copy",
baseAttrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)},
group: "group",
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, attrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)}, groups: []string{"group"}},
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, mu: &sync.Mutex{}, attrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)}, groups: []string{"group"}},
},
{
desc: "append",
baseGroups: []string{"base"},
group: "group",
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, attrs: []slog.Attr{}, groups: []string{"base", "group"}},
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, mu: &sync.Mutex{}, attrs: []slog.Attr{}, groups: []string{"base", "group"}},
},
{
desc: "append_with_attrs",
baseGroups: []string{"base"},
baseAttrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)},
group: "group",
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, attrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)}, groups: []string{"base", "group"}},
want: &DevModeHandler{opts: &DevModeHandlerOptions{}, w: buf, mu: &sync.Mutex{}, attrs: []slog.Attr{slog.String("a", "a"), slog.Int("b", 2)}, groups: []string{"base", "group"}},
},
}

Expand Down

0 comments on commit 4ad26b6

Please sign in to comment.