server

package
v0.0.0-...-10a61ce Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Copyright 2024 Outernet Council Foundation

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package server provides a collection of server implementations for the Cosmic Connector application. It includes three main server types: GrpcServer, ChannelzServer, and PprofServer, all implementing the common Server interface.

Server Types:

  • GrpcServer: Handles the main gRPC communication for the Federation service
  • ChannelzServer: Provides gRPC channelz monitoring capabilities
  • PprofServer: Exposes Go runtime profiling data via HTTP endpoints

Each server implementation provides consistent Start and Shutdown methods for lifecycle management. The servers can be used independently or together as part of a larger application.

Example usage:

import (
 "context"
 "github.com/rs/zerolog"
 "aalyria.com/spacetime/apps/cosmicconnector/server"
 "aalyria.com/spacetime/apps/cosmicconnector/handler"
)

func main() {
 logger := zerolog.New(os.Stdout)

 // Create a new gRPC server
 federationHandler := handler.NewFederationHandler()
 grpcServer := server.NewGrpcServer(8080, federationHandler, logger)

 // Start the server
 ctx := context.Background()
 if err := grpcServer.Start(ctx); err != nil {
  logger.Fatal().Err(err).Msg("Failed to start gRPC server")
 }

 // Graceful shutdown
 if err := grpcServer.Shutdown(ctx); err != nil {
  logger.Error().Err(err).Msg("Error during server shutdown")
 }
}

The Server interface ensures consistent behavior across all server implementations:

type Server interface {
 Start(ctx context.Context) error
 Shutdown(ctx context.Context) error
}

All server implementations use structured logging via zerolog and support graceful shutdown operations. They are designed to be concurrent-safe and suitable for production deployments.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChannelzServer

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

ChannelzServer implements the Server interface for Channelz service.

func NewChannelzServer

func NewChannelzServer(address string, logger zerolog.Logger) *ChannelzServer

NewChannelzServer creates a new ChannelzServer with the given address.

func (*ChannelzServer) Shutdown

func (c *ChannelzServer) Shutdown(ctx context.Context) error

Shutdown gracefully stops the Channelz server.

func (*ChannelzServer) Start

func (c *ChannelzServer) Start(ctx context.Context) error

Start begins serving the Channelz service and blocks until the server is stopped.

type GrpcServer

type GrpcServer struct {
	pb.UnimplementedFederationServer
	// contains filtered or unexported fields
}

GrpcServer implements the Server interface for gRPC services.

func NewGrpcServer

func NewGrpcServer(port int, handler handler.FederationHandler, logger zerolog.Logger) *GrpcServer

NewGrpcServer creates a new GrpcServer with the given port, handler, and logger.

func (*GrpcServer) Shutdown

func (g *GrpcServer) Shutdown(ctx context.Context) error

Shutdown gracefully stops the gRPC server.

func (*GrpcServer) Start

func (g *GrpcServer) Start(ctx context.Context) error

Start begins serving gRPC requests and blocks until the server is stopped.

type PprofServer

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

PprofServer implements the Server interface for pprof HTTP endpoints.

func NewPprofServer

func NewPprofServer(address string, logger zerolog.Logger) *PprofServer

NewPprofServer creates a new PprofServer with the given address and logger.

func (*PprofServer) Shutdown

func (p *PprofServer) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the pprof server.

func (*PprofServer) Start

func (p *PprofServer) Start(ctx context.Context) error

Start begins serving pprof endpoints and blocks until the server is stopped.

type Server

type Server interface {
	Start(ctx context.Context) error
	Shutdown(ctx context.Context) error
}

Jump to

Keyboard shortcuts

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