drpc

package module
v0.0.0-...-07a72b3 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2024 License: Apache-2.0 Imports: 14 Imported by: 10

README

hertz server for go-orb

This package contains a hertz http/h2c server for go-orb. It's a WIP and has known bugs.

Known bugs

  • Doesn't exit gracefully.
  • Slow.

Documentation

Overview

Package drpc provides the drpc server for go-orb.

Index

Constants

View Source
const (
	// DefaultAddress to use for new dRPC servers.
	// If set to "random", the default, a random address will be selected,
	// preferably on a private interface (XX subet). TODO: implement.
	DefaultAddress = "[::]:8381"

	// DefaultMaxConcurrentStreams for HTTP2.
	DefaultMaxConcurrentStreams = 512

	// DefaultReadTimeout see net/http pkg for more details.
	DefaultReadTimeout = 5 * time.Second

	// DefaultWriteTimeout see net/http pkg for more details.
	DefaultWriteTimeout = 5 * time.Second

	// DefaultIdleTimeout see net/http pkg for more details.
	DefaultIdleTimeout = 5 * time.Second

	// DefaultStopTimeout sets the timeout for ServerHertz.Stop().
	DefaultStopTimeout = time.Second

	// DefaultConfigSection is the section key used in config files used to
	// configure the server options.
	DefaultConfigSection = Plugin
)
View Source
const Plugin = "drpc"

Plugin is the plugin name.

Variables

View Source
var (
	// ErrInvalidConfigType is returned when you provided an invalid config type.
	ErrInvalidConfigType = errors.New("drpc server: invalid config type provided, not of type drpc.Config")
)

Errors.

Functions

func DefaultCodecWhitelist

func DefaultCodecWhitelist() []string

DefaultCodecWhitelist is the default allowed list of codecs to be used for HTTP request encoding/decoding. This means that if any of these plugins are registered, they will be included in the server's available codecs. If they are not registered, the server will not be able to handle these formats.

func WithDefaults

func WithDefaults(options ...Option) server.Option

WithDefaults sets default options to use on the creation of new HTTP entrypoints.

func WithEntrypoint

func WithEntrypoint(options ...Option) server.Option

WithEntrypoint adds an HTTP entrypoint with the provided options.

Types

type Config

type Config struct {
	// Name is the entrypoint name.
	//
	// The default name is 'http-<random uuid>'
	Name string `json:"name" yaml:"name"`

	// Listener can be used to provide your own Listener, when in use `Address` is obsolete.
	Listener net.Listener `json:"-" yaml:"-"`

	// Address to listen on.
	// TODO(davincible): implement this, and the address method.
	// If no IP is provided, an interface will be selected automatically. Private
	// interfaces are preferred, if none are found a public interface will be used.
	//
	// If no port is provided, a random port will be selected. To listen on a
	// specific interface, but with a random port, you can use '<IP>:0'.
	Address string `json:"address" yaml:"address"`

	// HandlerRegistrations are all handler registration functions that will be
	// registered to the server upon startup.
	//
	// You can statically add handlers by using the fuctional server options.
	// Optionally, you can dynamically add handlers by registering them to the
	// Handlers global, and setting them explicitly in the config.
	HandlerRegistrations server.HandlerRegistrations `json:"handlers" yaml:"handlers"`

	// Logger allows you to dynamically change the log level and plugin for a
	// specific entrypoint.
	Logger struct {
		Level  slog.Level `json:"level,omitempty"  yaml:"level,omitempty"` // TODO(davincible): change with custom level
		Plugin string     `json:"plugin,omitempty" yaml:"plugin,omitempty"`
	} `json:"logger" yaml:"logger"`
}

Config provides options to the entrypoint.

func NewConfig

func NewConfig(options ...Option) *Config

NewConfig will create a new default config for the entrypoint.

func (*Config) ApplyOptions

func (c *Config) ApplyOptions(options ...Option)

ApplyOptions applies a set of options to the config.

func (Config) Copy

func (c Config) Copy() server.EntrypointConfig

Copy creates a copy of the entrypoint config.

func (Config) GetAddress

func (c Config) GetAddress() string

GetAddress returns the entrypoint address.

type Option

type Option func(*Config)

Option is a functional option to provide custom values to the config.

func WithAddress

func WithAddress(address string) Option

WithAddress specifies the address to listen on. If you want to listen on all interfaces use the format ":8080" If you want to listen on a specific interface/address use the full IP.

func WithConfig

func WithConfig(config Config) Option

WithConfig will set replace the server config with config provided as argument. Warning: any options applied previous to this option will be overwritten by the contents of the config provided here.

func WithListener

func WithListener(l net.Listener) Option

WithListener sets the entrypoints listener. This overwrites `Address`.

func WithLogLevel

func WithLogLevel(level slog.Level) Option

WithLogLevel changes the log level from the inherited logger.

func WithLogPlugin

func WithLogPlugin(plugin string) Option

WithLogPlugin changes the log level from the inherited logger.

func WithName

func WithName(name string) Option

WithName sets the entrypoint name. The default name is in the format of 'http-<uuid>'.

Setting a custom name allows you to dynamically reference the entrypoint in the file config, and makes it easier to attribute the logs.

func WithRegistration

func WithRegistration(name string, registration server.RegistrationFunc) Option

WithRegistration adds a named registration function to the config. The name set here allows you to dynamically add this handler to entrypoints through a config.

Registration functions are used to register handlers to a server.

type Server

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

Server is the drpc Server for go-orb.

func ProvideServer

func ProvideServer(
	_ types.ServiceName,
	logger log.Logger,
	reg registry.Type,
	cfg Config,
	options ...Option,
) (*Server, error)

ProvideServer creates a new entrypoint for a single address. You can create multiple entrypoints for multiple addresses and ports.

func (*Server) AddEndpoint

func (s *Server) AddEndpoint(name string)

AddEndpoint add's an endpoint to the internal list. This is used by the Register() callback function.

func (*Server) Address

func (s *Server) Address() string

Address returns the address the entrypoint is listening on, for example: [::]:8381.

func (*Server) EntrypointID

func (s *Server) EntrypointID() string

EntrypointID returns the id (uuid) of this entrypoint in the registry.

func (*Server) Name

func (s *Server) Name() string

Name returns the entrypoint name.

func (*Server) Register

func (s *Server) Register(register orbserver.RegistrationFunc)

Register executes a registration function on the entrypoint.

func (*Server) Router

func (s *Server) Router() *drpcmux.Mux

Router returns the drpc mux.

func (*Server) Start

func (s *Server) Start() error

Start will create the listeners and start the server on the entrypoint.

func (*Server) Stop

func (s *Server) Stop(_ context.Context) error

Stop will stop the Hertz server(s).

func (*Server) String

func (s *Server) String() string

String returns the entrypoint type; http.

func (*Server) Transport

func (s *Server) Transport() string

Transport returns the client transport to use: "drpc".

func (*Server) Type

func (s *Server) Type() string

Type returns the component type.

Jump to

Keyboard shortcuts

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