File: server.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (84 lines) | stat: -rw-r--r-- 2,676 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package testapp

import (
	"context"
	"encoding/json"
	"io"

	newrelic "github.com/newrelic/go-agent"
	codes "google.golang.org/grpc/codes"
	"google.golang.org/grpc/metadata"
	status "google.golang.org/grpc/status"
)

// Server is a gRPC server.
type Server struct{}

// DoUnaryUnary is a unary request, unary response method.
func (s *Server) DoUnaryUnary(ctx context.Context, msg *Message) (*Message, error) {
	defer newrelic.StartSegment(newrelic.FromContext(ctx), "DoUnaryUnary").End()
	md, _ := metadata.FromIncomingContext(ctx)
	js, _ := json.Marshal(md)
	return &Message{Text: string(js)}, nil
}

// DoUnaryStream is a unary request, stream response method.
func (s *Server) DoUnaryStream(msg *Message, stream TestApplication_DoUnaryStreamServer) error {
	defer newrelic.StartSegment(newrelic.FromContext(stream.Context()), "DoUnaryStream").End()
	md, _ := metadata.FromIncomingContext(stream.Context())
	js, _ := json.Marshal(md)
	for i := 0; i < 3; i++ {
		if err := stream.Send(&Message{Text: string(js)}); nil != err {
			return err
		}
	}
	return nil
}

// DoStreamUnary is a stream request, unary response method.
func (s *Server) DoStreamUnary(stream TestApplication_DoStreamUnaryServer) error {
	defer newrelic.StartSegment(newrelic.FromContext(stream.Context()), "DoStreamUnary").End()
	md, _ := metadata.FromIncomingContext(stream.Context())
	js, _ := json.Marshal(md)
	for {
		_, err := stream.Recv()
		if err == io.EOF {
			return stream.SendAndClose(&Message{Text: string(js)})
		} else if nil != err {
			return err
		}
	}
}

// DoStreamStream is a stream request, stream response method.
func (s *Server) DoStreamStream(stream TestApplication_DoStreamStreamServer) error {
	defer newrelic.StartSegment(newrelic.FromContext(stream.Context()), "DoStreamStream").End()
	md, _ := metadata.FromIncomingContext(stream.Context())
	js, _ := json.Marshal(md)
	for {
		_, err := stream.Recv()
		if err == io.EOF {
			return nil
		} else if nil != err {
			return err
		}
		if err := stream.Send(&Message{Text: string(js)}); nil != err {
			return err
		}
	}
}

// DoUnaryUnaryError is a unary request, unary response method that returns an
// error.
func (s *Server) DoUnaryUnaryError(ctx context.Context, msg *Message) (*Message, error) {
	return &Message{}, status.New(codes.DataLoss, "oooooops!").Err()
}

// DoUnaryStreamError is a unary request, unary response method that returns an
// error.
func (s *Server) DoUnaryStreamError(msg *Message, stream TestApplication_DoUnaryStreamErrorServer) error {
	return status.New(codes.DataLoss, "oooooops!").Err()
}