Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
8e3c56f4ed | |||
fcbae6f94a | |||
2f818d389b | |||
d55cb59531 | |||
8c416b10ef | |||
d3c2ae5f54 | |||
3bb8ec0753 | |||
27bca35eb6 | |||
498218912c | |||
bd04f5b9cb | |||
dc976006ad | |||
934ebf6c0a | |||
6d8fce53dd | |||
f12f3fb2c2 | |||
|
5a755437c9 | ||
05db1f3dae | |||
e4ba134fa6 | |||
8a85989b79 |
2
.github/workflows/job_coverage.yml
vendored
2
.github/workflows/job_coverage.yml
vendored
@@ -8,8 +8,6 @@ on:
|
||||
- '.gitea/**'
|
||||
pull_request:
|
||||
branches: [ main, v3, v4 ]
|
||||
# Allows you to run this workflow manually from the Actions tab
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
|
||||
|
53
.github/workflows/job_sync.yml
vendored
53
.github/workflows/job_sync.yml
vendored
@@ -3,11 +3,6 @@ name: sync
|
||||
on:
|
||||
schedule:
|
||||
- cron: '*/5 * * * *'
|
||||
# push:
|
||||
# branches: [ master, v3, v4 ]
|
||||
# paths-ignore:
|
||||
# - '.github/**'
|
||||
# - '.gitea/**'
|
||||
# Allows you to run this workflow manually from the Actions tab
|
||||
workflow_dispatch:
|
||||
|
||||
@@ -23,35 +18,77 @@ jobs:
|
||||
echo "machine git.unistack.org login vtolstov password ${{ secrets.TOKEN_GITEA }}" >> /root/.netrc
|
||||
echo "machine github.com login vtolstov password ${{ secrets.TOKEN_GITHUB }}" >> /root/.netrc
|
||||
|
||||
- name: check master
|
||||
id: check_master
|
||||
run: |
|
||||
src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/master | cut -f1)
|
||||
dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/master | cut -f1)
|
||||
echo "src_hash=$src_hash"
|
||||
echo "dst_hash=$dst_hash"
|
||||
if [ "$src_hash" != "$dst_hash" ]; then
|
||||
echo "sync_needed=true" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "sync_needed=false" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
- name: sync master
|
||||
if: steps.check_master.outputs.sync_needed == 'true'
|
||||
run: |
|
||||
git clone --filter=blob:none --filter=tree:0 --branch master --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
|
||||
cd repo
|
||||
git remote add --no-tags --track master upstream https://github.com/${GITHUB_REPOSITORY}
|
||||
git remote add --no-tags --fetch --track master upstream https://github.com/${GITHUB_REPOSITORY}
|
||||
git pull --rebase upstream master
|
||||
git push upstream master --progress
|
||||
git push origin master --progress
|
||||
cd ../
|
||||
rm -rf repo
|
||||
|
||||
- name: check v3
|
||||
id: check_v3
|
||||
run: |
|
||||
src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/v3 | cut -f1)
|
||||
dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/v3 | cut -f1)
|
||||
echo "src_hash=$src_hash"
|
||||
echo "dst_hash=$dst_hash"
|
||||
if [ "$src_hash" != "$dst_hash" ]; then
|
||||
echo "sync_needed=true" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "sync_needed=false" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
- name: sync v3
|
||||
if: steps.check_v3.outputs.sync_needed == 'true'
|
||||
run: |
|
||||
git clone --filter=blob:none --filter=tree:0 --branch v3 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
|
||||
cd repo
|
||||
git remote add --no-tags --fetch --track v3 upstream https://github.com/${GITHUB_REPOSITORY}
|
||||
git pull --rebase upstream v3
|
||||
git push upstream v3
|
||||
git push upstream v3 --progress
|
||||
git push origin v3 --progress
|
||||
cd ../
|
||||
rm -rf repo
|
||||
|
||||
- name: check v4
|
||||
id: check_v4
|
||||
run: |
|
||||
src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/v4 | cut -f1)
|
||||
dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/v4 | cut -f1)
|
||||
echo "src_hash=$src_hash"
|
||||
echo "dst_hash=$dst_hash"
|
||||
if [ "$src_hash" != "$dst_hash" ]; then
|
||||
echo "sync_needed=true" >> $GITHUB_OUTPUT
|
||||
else
|
||||
echo "sync_needed=false" >> $GITHUB_OUTPUT
|
||||
fi
|
||||
|
||||
- name: sync v4
|
||||
if: steps.check_v4.outputs.sync_needed == 'true'
|
||||
run: |
|
||||
git clone --filter=blob:none --filter=tree:0 --branch v4 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
|
||||
cd repo
|
||||
git remote add --no-tags --fetch --track v4 upstream https://github.com/${GITHUB_REPOSITORY}
|
||||
git pull --rebase upstream v4
|
||||
git push upstream v4
|
||||
git push upstream v4 --progress
|
||||
git push origin v4 --progress
|
||||
cd ../
|
||||
rm -rf repo
|
||||
|
4
README.md
Normal file
4
README.md
Normal file
@@ -0,0 +1,4 @@
|
||||
# GRPC Server
|
||||

|
||||
|
||||
This plugin is a grpc server for micro.
|
3
go.mod
3
go.mod
@@ -5,6 +5,7 @@ go 1.23.0
|
||||
toolchain go1.24.2
|
||||
|
||||
require (
|
||||
github.com/stretchr/testify v1.10.0
|
||||
go.unistack.org/micro/v4 v4.1.8
|
||||
golang.org/x/net v0.39.0
|
||||
google.golang.org/grpc v1.72.0
|
||||
@@ -13,8 +14,10 @@ require (
|
||||
|
||||
require (
|
||||
github.com/ash3in/uuidv8 v1.2.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/matoous/go-nanoid v1.5.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/spf13/cast v1.7.1 // indirect
|
||||
go.unistack.org/micro-proto/v4 v4.1.0 // indirect
|
||||
golang.org/x/sys v0.32.0 // indirect
|
||||
|
9
grpc.go
9
grpc.go
@@ -263,6 +263,7 @@ func (g *Server) handler(srv interface{}, stream grpc.ServerStream) error {
|
||||
// create new context
|
||||
ctx = metadata.NewIncomingContext(ctx, md)
|
||||
ctx = metadata.NewOutgoingContext(ctx, metadata.New(0))
|
||||
ctx = context.WithValue(ctx, rspMetadataKey{}, &rspMetadataVal{m: metadata.New(0)})
|
||||
|
||||
stream = &streamWrapper{ctx, stream}
|
||||
|
||||
@@ -397,8 +398,8 @@ func (g *Server) processRequest(ctx context.Context, stream grpc.ServerStream, s
|
||||
statusDesc := ""
|
||||
// execute the handler
|
||||
appErr := fn(ctx, r, replyv.Interface())
|
||||
if outmd, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||
if err = stream.SendHeader(gmetadata.MD(outmd.Copy())); err != nil {
|
||||
if md := getResponseMetadata(ctx); len(md) > 0 {
|
||||
if err = stream.SendHeader(md.AsHTTP2()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -481,8 +482,8 @@ func (g *Server) processStream(ctx context.Context, stream grpc.ServerStream, se
|
||||
statusDesc := ""
|
||||
|
||||
appErr := fn(ctx, r, ss)
|
||||
if outmd, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||
if err := stream.SendHeader(gmetadata.MD(outmd.Copy())); err != nil {
|
||||
if md := getResponseMetadata(ctx); len(md) > 0 {
|
||||
if err := stream.SendHeader(md.AsHTTP2()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
47
metadata.go
Normal file
47
metadata.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.unistack.org/micro/v4/metadata"
|
||||
)
|
||||
|
||||
type (
|
||||
rspMetadataKey struct{}
|
||||
rspMetadataVal struct {
|
||||
m metadata.Metadata
|
||||
}
|
||||
)
|
||||
|
||||
// AppendResponseMetadata adds metadata entries to metadata.Metadata stored in the context.
|
||||
// It expects the context to contain a *rspMetadataVal value under the rspMetadataKey{} key.
|
||||
// If the value is missing or invalid, the function does nothing.
|
||||
//
|
||||
// Note: this function is not thread-safe. Synchronization is required if used from multiple goroutines.
|
||||
func AppendResponseMetadata(ctx context.Context, md metadata.Metadata) {
|
||||
if md == nil {
|
||||
return
|
||||
}
|
||||
|
||||
val, ok := ctx.Value(rspMetadataKey{}).(*rspMetadataVal)
|
||||
if !ok || val == nil || val.m == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for key, values := range md {
|
||||
val.m.Append(key, values...)
|
||||
}
|
||||
}
|
||||
|
||||
// getResponseMetadata retrieves the metadata.Metadata stored in the context.
|
||||
//
|
||||
// Note: this function is not thread-safe. Synchronization is required if used from multiple goroutines.
|
||||
// If you plan to modify the returned metadata, make a full copy to avoid affecting shared state.
|
||||
func getResponseMetadata(ctx context.Context) metadata.Metadata {
|
||||
val, ok := ctx.Value(rspMetadataKey{}).(*rspMetadataVal)
|
||||
if !ok || val == nil || val.m == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return val.m
|
||||
}
|
136
metadata_test.go
Normal file
136
metadata_test.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.unistack.org/micro/v4/metadata"
|
||||
)
|
||||
|
||||
func TestAppendResponseMetadata(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ctx context.Context
|
||||
md metadata.Metadata
|
||||
expected context.Context
|
||||
}{
|
||||
{
|
||||
name: "nil metadata",
|
||||
ctx: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{m: metadata.Metadata{}}),
|
||||
md: nil,
|
||||
expected: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{m: metadata.Metadata{}}),
|
||||
},
|
||||
{
|
||||
name: "empty metadata",
|
||||
ctx: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{m: metadata.Metadata{}}),
|
||||
md: metadata.Metadata{},
|
||||
expected: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{m: metadata.Metadata{}}),
|
||||
},
|
||||
{
|
||||
name: "context without response metadata key",
|
||||
ctx: context.Background(),
|
||||
md: metadata.Pairs("key1", "val1"),
|
||||
expected: context.Background(),
|
||||
},
|
||||
{
|
||||
name: "context with nil response metadata value",
|
||||
ctx: context.WithValue(context.Background(), rspMetadataKey{}, nil),
|
||||
md: metadata.Pairs("key1", "val1"),
|
||||
expected: context.WithValue(context.Background(), rspMetadataKey{}, nil),
|
||||
},
|
||||
{
|
||||
name: "context with incorrect type in response metadata value",
|
||||
ctx: context.WithValue(context.Background(), rspMetadataKey{}, struct{}{}),
|
||||
md: metadata.Pairs("key1", "val1"),
|
||||
expected: context.WithValue(context.Background(), rspMetadataKey{}, struct{}{}),
|
||||
},
|
||||
{
|
||||
name: "context with response metadata value, but nil metadata",
|
||||
ctx: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{m: nil}),
|
||||
md: metadata.Pairs("key1", "val1"),
|
||||
expected: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{m: nil}),
|
||||
},
|
||||
{
|
||||
name: "basic metadata append",
|
||||
ctx: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{m: metadata.Metadata{}}),
|
||||
md: metadata.Pairs("key1", "val1"),
|
||||
expected: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{
|
||||
m: metadata.Metadata{
|
||||
"key1": []string{"val1"},
|
||||
},
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "multiple values for same key",
|
||||
ctx: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{m: metadata.Metadata{}}),
|
||||
md: metadata.Pairs("key1", "val1", "key1", "val2"),
|
||||
expected: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{
|
||||
m: metadata.Metadata{
|
||||
"key1": []string{"val1", "val2"},
|
||||
},
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "multiple values for different keys",
|
||||
ctx: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{m: metadata.Metadata{}}),
|
||||
md: metadata.Pairs("key1", "val1", "key1", "val2", "key2", "val3", "key2", "val4", "key3", "val5"),
|
||||
expected: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{
|
||||
m: metadata.Metadata{
|
||||
"key1": []string{"val1", "val2"},
|
||||
"key2": []string{"val3", "val4"},
|
||||
"key3": []string{"val5"},
|
||||
},
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
AppendResponseMetadata(tt.ctx, tt.md)
|
||||
require.Equal(t, tt.expected, tt.ctx)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetResponseMetadata(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ctx context.Context
|
||||
expected metadata.Metadata
|
||||
}{
|
||||
{
|
||||
name: "context without response metadata key",
|
||||
ctx: context.Background(),
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "context with nil response metadata value",
|
||||
ctx: context.WithValue(context.Background(), rspMetadataKey{}, nil),
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "context with incorrect type in response metadata value",
|
||||
ctx: context.WithValue(context.Background(), rspMetadataKey{}, &struct{}{}),
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "context with response metadata value, but nil metadata",
|
||||
ctx: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{m: nil}),
|
||||
expected: nil,
|
||||
},
|
||||
{
|
||||
name: "valid metadata",
|
||||
ctx: context.WithValue(context.Background(), rspMetadataKey{}, &rspMetadataVal{
|
||||
m: metadata.Pairs("key1", "value1"),
|
||||
}),
|
||||
expected: metadata.Metadata{"key1": {"value1"}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
require.Equal(t, tt.expected, getResponseMetadata(tt.ctx))
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user