grpc

package module
v0.0.0-...-c0075a2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2023 License: MIT Imports: 3 Imported by: 0

README

grpc server

grpc server implemented by grpc

Usage

package grpc

import (
	"context"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func TestNewServer(t *testing.T) {
	addr := "0.0.0.0:9090"
	grpcServer := grpc.NewServer()
	server := NewServer(grpcServer, addr)

	go func() {
		ctx, cancelFunc := context.WithTimeout(context.TODO(), time.Second*3)
		defer cancelFunc()
		_, err := grpc.DialContext(
			ctx,
			"127.0.0.1:9090",
			grpc.WithTransportCredentials(insecure.NewCredentials()),
			grpc.WithBlock(),
		)
		assert.NoError(t, err)

		time.Sleep(1 * time.Second)
		err = server.Shutdown()
		assert.NoError(t, err)
		t.Log("shutdown completed")
	}()

	_ = server.Start()
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options func(*Server)

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server grpc server

func NewServer

func NewServer(grpcServer *grpc.Server, addr string, options ...Options) *Server

NewServer creates a new server instance with default settings

func (*Server) Shutdown

func (h *Server) Shutdown() error

Shutdown shuts down the server

func (*Server) Start

func (h *Server) Start() (err error)

Start to start the server and listen on the given address

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL