Skip to content

Commit

Permalink
refactor grpc (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong authored Apr 27, 2024
1 parent 6709f77 commit 681cd0e
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 315 deletions.
6 changes: 3 additions & 3 deletions grpc/log/buffer.go → grpc/internal/buffer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2024 The nilgo authors
// Use of this source code is governed by a MIT license found in the LICENSE file.

package log
package internal

import (
"context"
Expand Down Expand Up @@ -49,7 +49,7 @@ type serverStream struct {

func (s serverStream) Context() context.Context { return s.ctx }

func isSamplingHandler(handler slog.Handler) bool {
func IsSamplingHandler(handler slog.Handler) bool {
switch handler.(type) {
case sampling.Handler, *sampling.Handler:
return true
Expand All @@ -71,7 +71,7 @@ func isSamplingHandler(handler slog.Handler) bool {
if v := valueCopy.FieldByName(name); v.IsValid() {
v = reflect.NewAt(v.Type(), unsafe.Pointer(v.UnsafeAddr())).Elem()
if h, ok := v.Interface().(slog.Handler); ok {
return isSamplingHandler(h)
return IsSamplingHandler(h)
}
}
}
Expand Down
86 changes: 82 additions & 4 deletions grpc/log/buffer_test.go → grpc/internal/buffer_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2024 The nilgo authors
// Use of this source code is governed by a MIT license found in the LICENSE file.

package log_test
package internal_test

import (
"bytes"
Expand All @@ -15,7 +15,7 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/grpc"

"github.com/nil-go/nilgo/grpc/log"
"github.com/nil-go/nilgo/grpc/internal"
)

func TestBufferUnaryInterceptor(t *testing.T) {
Expand Down Expand Up @@ -56,7 +56,7 @@ level=ERROR msg=error
func(context.Context) bool { return false },
)

resp, err := log.BufferUnaryInterceptor(context.Background(), nil, nil, func(ctx context.Context, _ any) (any, error) {
resp, err := internal.BufferUnaryInterceptor(context.Background(), nil, nil, func(ctx context.Context, _ any) (any, error) {
for _, record := range testcase.records {
_ = handler.Handle(ctx, record)
}
Expand Down Expand Up @@ -108,7 +108,7 @@ level=ERROR msg=error
func(context.Context) bool { return false },
)

err := log.BufferStreamInterceptor(nil, serverStream{}, nil, func(_ any, stream grpc.ServerStream) error {
err := internal.BufferStreamInterceptor(nil, serverStream{}, nil, func(_ any, stream grpc.ServerStream) error {
for _, record := range testcase.records {
_ = handler.Handle(stream.Context(), record)
}
Expand All @@ -124,3 +124,81 @@ level=ERROR msg=error
type serverStream struct{ grpc.ServerStream }

func (s serverStream) Context() context.Context { return context.Background() }

func TestIsSamplingHandler(t *testing.T) {
t.Parallel()

testcases := []struct {
description string
handler slog.Handler
expected bool
}{
{
description: "nil handler",
},
{
description: "sampling handler",
handler: sampling.Handler{},
expected: true,
},
{
description: "sampling handler (pointer)",
handler: &sampling.Handler{},
expected: true,
},
{
description: "embed sampling handler",
handler: handlerEmbed{sampling.Handler{}},
expected: true,
},
{
description: "embed sampling handler (pointer)",
handler: &handlerEmbed{sampling.Handler{}},
expected: true,
},
{
description: "nested sampling handler",
handler: handlerWrapper{handler: sampling.Handler{}},
expected: true,
},
{
description: "nested sampling handler (pointer)",
handler: &handlerWrapper{handler: sampling.Handler{}},
expected: true,
},
{
description: "deep nested sampling handler",
handler: handlerWrapper{handler: handlerWrapper{handler: sampling.Handler{}}},
expected: true,
},
{
description: "non sampling handler",
handler: slogDiscard{},
},
}

for _, testcase := range testcases {
testcase := testcase
t.Run(testcase.description, func(t *testing.T) {
t.Parallel()

assert.Equal(t, testcase.expected, internal.IsSamplingHandler(testcase.handler))
})
}
}

type handlerWrapper struct {
slogDiscard
handler slog.Handler
}

type slogDiscard struct{}

func (slogDiscard) Enabled(context.Context, slog.Level) bool { return false }
func (slogDiscard) Handle(context.Context, slog.Record) error { return nil }
func (slogDiscard) WithAttrs([]slog.Attr) slog.Handler { return slogDiscard{} }
func (slogDiscard) WithGroup(string) slog.Handler { return slogDiscard{} }

type handlerEmbed struct {
slog.Handler
}
7 changes: 6 additions & 1 deletion grpc/config.go → grpc/internal/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2024 The nilgo authors
// Use of this source code is governed by a MIT license found in the LICENSE file.

package grpc
package internal

import (
"context"
Expand All @@ -19,6 +19,11 @@ type ConfigServiceServer struct {
configs []*konf.Config
}

// NewConfigServiceServer creates a new ConfigServiceServer with the provided configs.
func NewConfigServiceServer(configs []*konf.Config) *ConfigServiceServer {
return &ConfigServiceServer{configs: configs}
}

func (c ConfigServiceServer) Explain(_ context.Context, request *pb.ExplainRequest) (*pb.ExplainResponse, error) {
if len(c.configs) == 0 {
return &pb.ExplainResponse{Explanation: konf.Explain(request.GetPath())}, nil
Expand Down
2 changes: 1 addition & 1 deletion grpc/log/recovery.go → grpc/internal/recovery.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2024 The nilgo authors
// Use of this source code is governed by a MIT license found in the LICENSE file.

package log
package internal

import (
"context"
Expand Down
8 changes: 4 additions & 4 deletions grpc/log/recovery_test.go → grpc/internal/recovery_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2024 The nilgo authors
// Use of this source code is governed by a MIT license found in the LICENSE file.

package log_test
package internal_test

import (
"bytes"
Expand All @@ -15,7 +15,7 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/grpc"

"github.com/nil-go/nilgo/grpc/log"
"github.com/nil-go/nilgo/grpc/internal"
)

func TestRecoveryUnaryInterceptor(t *testing.T) {
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestRecoveryUnaryInterceptor(t *testing.T) {
},
})

resp, err := log.RecoveryUnaryInterceptor(handler)(context.Background(), nil, nil, testcase.handler)
resp, err := internal.RecoveryUnaryInterceptor(handler)(context.Background(), nil, nil, testcase.handler)
if testcase.err == "" {
require.NoError(t, err)
assert.Equal(t, "", resp)
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestRecoveryStreamInterceptor(t *testing.T) {
},
})

err := log.RecoveryStreamInterceptor(handler)(nil, serverStream{}, nil, testcase.handler)
err := internal.RecoveryStreamInterceptor(handler)(nil, serverStream{}, nil, testcase.handler)
if testcase.err == "" {
require.NoError(t, err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion grpc/log/slog.go → grpc/internal/slog.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2024 The nilgo authors
// Use of this source code is governed by a MIT license found in the LICENSE file.

package log
package internal

import (
"context"
Expand Down
6 changes: 3 additions & 3 deletions grpc/log/slog_test.go → grpc/internal/slog_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2024 The nilgo authors
// Use of this source code is governed by a MIT license found in the LICENSE file.

package log_test
package internal_test

import (
"bytes"
Expand All @@ -13,15 +13,15 @@ import (
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/grpclog"

"github.com/nil-go/nilgo/grpc/log"
"github.com/nil-go/nilgo/grpc/internal"
)

func TestSlogger(t *testing.T) {
t.Setenv("GRPC_GO_LOG_SEVERITY_LEVEL", "info")
t.Setenv("GRPC_GO_LOG_VERBOSITY_LEVEL", "1")

buf := new(bytes.Buffer)
logger := log.NewSlogger(slog.NewTextHandler(buf, &slog.HandlerOptions{
logger := internal.NewSlogger(slog.NewTextHandler(buf, &slog.HandlerOptions{
AddSource: true,
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
if len(groups) == 0 && attr.Key == slog.TimeKey {
Expand Down
91 changes: 0 additions & 91 deletions grpc/log/buffer_internal_test.go

This file was deleted.

41 changes: 0 additions & 41 deletions grpc/log/log.go

This file was deleted.

Loading

0 comments on commit 681cd0e

Please sign in to comment.