2016-12-14 18:41:48 +03:00
|
|
|
// Package server is an interface for a micro server
|
2023-04-11 22:20:37 +03:00
|
|
|
package server // import "go.unistack.org/micro/v4/server"
|
2015-01-14 02:31:27 +03:00
|
|
|
|
|
|
|
import (
|
2018-03-03 14:53:52 +03:00
|
|
|
"context"
|
2019-08-21 17:43:46 +03:00
|
|
|
"time"
|
2015-01-14 02:31:27 +03:00
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/codec"
|
|
|
|
"go.unistack.org/micro/v4/metadata"
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2015-01-14 02:31:27 +03:00
|
|
|
)
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
// DefaultServer default server
|
2022-04-17 11:41:49 +03:00
|
|
|
var DefaultServer = NewServer()
|
2020-08-28 11:52:51 +03:00
|
|
|
|
2020-11-26 01:13:05 +03:00
|
|
|
var (
|
2021-07-16 22:57:39 +03:00
|
|
|
// DefaultAddress will be used if no address passed, use secure localhost
|
|
|
|
DefaultAddress = "127.0.0.1:0"
|
2020-12-08 00:38:37 +03:00
|
|
|
// DefaultName will be used if no name passed
|
|
|
|
DefaultName = "server"
|
|
|
|
// DefaultVersion will be used if no version passed
|
|
|
|
DefaultVersion = "latest"
|
|
|
|
// DefaultRegisterCheck holds func that run before register server
|
|
|
|
DefaultRegisterCheck = func(context.Context) error { return nil }
|
|
|
|
// DefaultRegisterInterval holds interval for register
|
2020-11-26 01:13:05 +03:00
|
|
|
DefaultRegisterInterval = time.Second * 30
|
2021-01-29 13:17:32 +03:00
|
|
|
// DefaultRegisterTTL holds register record ttl, must be multiple of DefaultRegisterInterval
|
2020-12-08 00:38:37 +03:00
|
|
|
DefaultRegisterTTL = time.Second * 90
|
2023-07-29 00:40:58 +03:00
|
|
|
// DefaultMaxMsgSize holds default max msg size
|
2020-12-08 00:38:37 +03:00
|
|
|
DefaultMaxMsgSize = 1024 * 1024 * 4 // 4Mb
|
|
|
|
// DefaultMaxMsgRecvSize holds default max recv size
|
|
|
|
DefaultMaxMsgRecvSize = 1024 * 1024 * 4 // 4Mb
|
|
|
|
// DefaultMaxMsgSendSize holds default max send size
|
|
|
|
DefaultMaxMsgSendSize = 1024 * 1024 * 4 // 4Mb
|
2020-11-26 01:13:05 +03:00
|
|
|
)
|
|
|
|
|
2018-11-26 17:51:42 +03:00
|
|
|
// Server is a simple micro server abstraction
|
2015-01-14 02:31:27 +03:00
|
|
|
type Server interface {
|
2021-01-29 13:17:32 +03:00
|
|
|
// Name returns server name
|
|
|
|
Name() string
|
2020-05-12 13:32:01 +03:00
|
|
|
// Initialise options
|
2023-07-29 00:40:58 +03:00
|
|
|
Init(...options.Option) error
|
2020-05-12 13:32:01 +03:00
|
|
|
// Retrieve the options
|
|
|
|
Options() Options
|
2023-07-29 00:40:58 +03:00
|
|
|
// Create and register new handler
|
|
|
|
Handle(h interface{}, opts ...options.Option) error
|
2020-05-12 13:32:01 +03:00
|
|
|
// Start the server
|
2015-01-14 02:31:27 +03:00
|
|
|
Start() error
|
2020-05-12 13:32:01 +03:00
|
|
|
// Stop the server
|
2015-01-14 02:31:27 +03:00
|
|
|
Stop() error
|
2020-05-12 13:32:01 +03:00
|
|
|
// Server implementation
|
2015-12-20 00:56:14 +03:00
|
|
|
String() string
|
2015-01-14 02:31:27 +03:00
|
|
|
}
|
|
|
|
|
2018-11-26 17:51:42 +03:00
|
|
|
// Request is a synchronous request interface
|
2015-12-02 23:56:50 +03:00
|
|
|
type Request interface {
|
2019-01-09 19:20:57 +03:00
|
|
|
// Service name requested
|
2015-12-02 23:56:50 +03:00
|
|
|
Service() string
|
2019-01-18 13:12:57 +03:00
|
|
|
// The action requested
|
|
|
|
Method() string
|
2019-01-11 00:25:31 +03:00
|
|
|
// Endpoint name requested
|
|
|
|
Endpoint() string
|
2019-01-09 19:20:57 +03:00
|
|
|
// Content type provided
|
2015-12-02 23:56:50 +03:00
|
|
|
ContentType() string
|
2019-01-09 22:11:47 +03:00
|
|
|
// Header of the request
|
2020-11-18 16:50:41 +03:00
|
|
|
Header() metadata.Metadata
|
2019-02-04 16:13:03 +03:00
|
|
|
// Body is the initial decoded value
|
|
|
|
Body() interface{}
|
2019-01-09 22:11:47 +03:00
|
|
|
// Read the undecoded request body
|
|
|
|
Read() ([]byte, error)
|
|
|
|
// The encoded message stream
|
2020-11-23 16:18:47 +03:00
|
|
|
Codec() codec.Codec
|
2019-01-09 19:20:57 +03:00
|
|
|
// Indicates whether its a stream
|
2015-12-02 23:56:50 +03:00
|
|
|
Stream() bool
|
|
|
|
}
|
|
|
|
|
2019-01-09 22:11:47 +03:00
|
|
|
// Response is the response writer for unencoded messages
|
|
|
|
type Response interface {
|
2019-01-09 22:28:13 +03:00
|
|
|
// Encoded writer
|
2020-11-23 16:18:47 +03:00
|
|
|
Codec() codec.Codec
|
2019-01-09 22:11:47 +03:00
|
|
|
// Write the header
|
2021-02-14 11:28:50 +03:00
|
|
|
WriteHeader(md metadata.Metadata)
|
2019-01-09 22:11:47 +03:00
|
|
|
// write a response directly to the client
|
|
|
|
Write([]byte) error
|
|
|
|
}
|
|
|
|
|
2018-04-14 20:15:09 +03:00
|
|
|
// Stream represents a stream established with a client.
|
2015-12-17 23:37:35 +03:00
|
|
|
// A stream can be bidirectional which is indicated by the request.
|
|
|
|
// The last error will be left in Error().
|
2018-11-26 17:51:42 +03:00
|
|
|
// EOF indicates end of the stream.
|
2018-04-14 20:15:09 +03:00
|
|
|
type Stream interface {
|
2022-01-10 16:47:37 +03:00
|
|
|
// Context for the stream
|
2015-12-17 23:37:35 +03:00
|
|
|
Context() context.Context
|
2022-01-10 16:47:37 +03:00
|
|
|
// Request returns request
|
2015-12-17 23:37:35 +03:00
|
|
|
Request() Request
|
2022-01-10 16:47:37 +03:00
|
|
|
// Send will encode and send a request
|
2021-02-14 11:28:50 +03:00
|
|
|
Send(msg interface{}) error
|
2022-01-10 16:47:37 +03:00
|
|
|
// Recv will decode and read a response
|
2021-02-14 11:28:50 +03:00
|
|
|
Recv(msg interface{}) error
|
2022-01-10 16:47:37 +03:00
|
|
|
// SendMsg will encode and send a request
|
|
|
|
SendMsg(msg interface{}) error
|
|
|
|
// RecvMsg will decode and read a response
|
|
|
|
RecvMsg(msg interface{}) error
|
|
|
|
// Error returns stream error
|
2015-12-17 23:37:35 +03:00
|
|
|
Error() error
|
2022-01-10 16:47:37 +03:00
|
|
|
// Close closes the stream
|
2015-12-17 23:37:35 +03:00
|
|
|
Close() error
|
|
|
|
}
|