2016-12-14 15:41:48 +00:00
|
|
|
// Package server is an interface for a micro server
|
2015-01-13 23:31:27 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2018-03-03 11:53:52 +00:00
|
|
|
"context"
|
2019-08-21 15:43:46 +01:00
|
|
|
"time"
|
2015-01-13 23:31:27 +00:00
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/codec"
|
2020-11-18 16:50:41 +03:00
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
2021-01-29 13:17:32 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2021-08-20 22:40:48 +03:00
|
|
|
"github.com/unistack-org/micro/v3/util/id"
|
2015-01-13 23:31:27 +00:00
|
|
|
)
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
// DefaultServer default server
|
|
|
|
var DefaultServer Server = NewServer()
|
2020-08-28 11:52:51 +03:00
|
|
|
|
2020-11-26 01:13:05 +03:00
|
|
|
var (
|
2021-07-17 01:57:39 +06: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"
|
2021-04-27 08:32:47 +03:00
|
|
|
// DefaultID will be used if no id passed
|
2021-08-20 22:40:48 +03:00
|
|
|
DefaultID = id.Must()
|
2020-12-08 00:38:37 +03:00
|
|
|
// 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
|
|
|
|
// DefaultNamespace will be used if no namespace passed
|
|
|
|
DefaultNamespace = "micro"
|
|
|
|
// DefaultMaxMsgSize holds default max msg ssize
|
|
|
|
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 14:51:42 +00:00
|
|
|
// Server is a simple micro server abstraction
|
2015-01-13 23:31:27 +00:00
|
|
|
type Server interface {
|
2021-01-29 13:17:32 +03:00
|
|
|
// Name returns server name
|
|
|
|
Name() string
|
2020-05-12 11:32:01 +01:00
|
|
|
// Initialise options
|
2016-01-02 19:12:17 +00:00
|
|
|
Init(...Option) error
|
2020-05-12 11:32:01 +01:00
|
|
|
// Retrieve the options
|
|
|
|
Options() Options
|
|
|
|
// Register a handler
|
2021-02-14 11:28:50 +03:00
|
|
|
Handle(h Handler) error
|
2020-05-12 11:32:01 +01:00
|
|
|
// Create a new handler
|
2021-02-14 11:28:50 +03:00
|
|
|
NewHandler(h interface{}, opts ...HandlerOption) Handler
|
2020-05-12 11:32:01 +01:00
|
|
|
// Create a new subscriber
|
2021-02-14 11:28:50 +03:00
|
|
|
NewSubscriber(topic string, h interface{}, opts ...SubscriberOption) Subscriber
|
2020-05-12 11:32:01 +01:00
|
|
|
// Register a subscriber
|
2021-02-14 11:28:50 +03:00
|
|
|
Subscribe(s Subscriber) error
|
2020-05-12 11:32:01 +01:00
|
|
|
// Start the server
|
2015-01-13 23:31:27 +00:00
|
|
|
Start() error
|
2020-05-12 11:32:01 +01:00
|
|
|
// Stop the server
|
2015-01-13 23:31:27 +00:00
|
|
|
Stop() error
|
2020-05-12 11:32:01 +01:00
|
|
|
// Server implementation
|
2015-12-19 21:56:14 +00:00
|
|
|
String() string
|
2015-01-13 23:31:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 13:26:36 +03:00
|
|
|
/*
|
2019-01-07 14:44:40 +00:00
|
|
|
// Router handle serving messages
|
|
|
|
type Router interface {
|
2019-11-25 16:31:43 +00:00
|
|
|
// ProcessMessage processes a message
|
2021-02-14 11:28:50 +03:00
|
|
|
ProcessMessage(ctx context.Context, msg Message) error
|
2019-01-09 16:20:57 +00:00
|
|
|
// ServeRequest processes a request to completion
|
2021-02-14 11:28:50 +03:00
|
|
|
ServeRequest(ctx context.Context, req Request, rsp Response) error
|
2019-01-07 14:44:40 +00:00
|
|
|
}
|
2021-03-24 13:26:36 +03:00
|
|
|
*/
|
2019-01-07 14:44:40 +00:00
|
|
|
|
2018-11-26 14:51:42 +00:00
|
|
|
// Message is an async message interface
|
2018-04-14 18:21:02 +01:00
|
|
|
type Message interface {
|
2019-11-25 16:31:43 +00:00
|
|
|
// Topic of the message
|
2015-12-02 20:56:50 +00:00
|
|
|
Topic() string
|
2019-11-25 16:31:43 +00:00
|
|
|
// The decoded payload value
|
2018-04-14 18:21:02 +01:00
|
|
|
Payload() interface{}
|
2019-11-25 16:31:43 +00:00
|
|
|
// The content type of the payload
|
2015-12-02 20:56:50 +00:00
|
|
|
ContentType() string
|
2019-11-25 16:31:43 +00:00
|
|
|
// The raw headers of the message
|
2020-11-18 16:50:41 +03:00
|
|
|
Header() metadata.Metadata
|
2019-11-25 16:31:43 +00:00
|
|
|
// The raw body of the message
|
|
|
|
Body() []byte
|
|
|
|
// Codec used to decode the message
|
2020-11-23 16:18:47 +03:00
|
|
|
Codec() codec.Codec
|
2015-12-02 20:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 14:51:42 +00:00
|
|
|
// Request is a synchronous request interface
|
2015-12-02 20:56:50 +00:00
|
|
|
type Request interface {
|
2019-01-09 16:20:57 +00:00
|
|
|
// Service name requested
|
2015-12-02 20:56:50 +00:00
|
|
|
Service() string
|
2019-01-18 10:12:57 +00:00
|
|
|
// The action requested
|
|
|
|
Method() string
|
2019-01-10 21:25:31 +00:00
|
|
|
// Endpoint name requested
|
|
|
|
Endpoint() string
|
2019-01-09 16:20:57 +00:00
|
|
|
// Content type provided
|
2015-12-02 20:56:50 +00:00
|
|
|
ContentType() string
|
2019-01-09 19:11:47 +00:00
|
|
|
// Header of the request
|
2020-11-18 16:50:41 +03:00
|
|
|
Header() metadata.Metadata
|
2019-02-04 13:13:03 +00:00
|
|
|
// Body is the initial decoded value
|
|
|
|
Body() interface{}
|
2019-01-09 19:11:47 +00: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 16:20:57 +00:00
|
|
|
// Indicates whether its a stream
|
2015-12-02 20:56:50 +00:00
|
|
|
Stream() bool
|
|
|
|
}
|
|
|
|
|
2019-01-09 19:11:47 +00:00
|
|
|
// Response is the response writer for unencoded messages
|
|
|
|
type Response interface {
|
2019-01-09 19:28:13 +00:00
|
|
|
// Encoded writer
|
2020-11-23 16:18:47 +03:00
|
|
|
Codec() codec.Codec
|
2019-01-09 19:11:47 +00:00
|
|
|
// Write the header
|
2021-02-14 11:28:50 +03:00
|
|
|
WriteHeader(md metadata.Metadata)
|
2019-01-09 19:11:47 +00:00
|
|
|
// write a response directly to the client
|
|
|
|
Write([]byte) error
|
|
|
|
}
|
|
|
|
|
2018-04-14 18:15:09 +01:00
|
|
|
// Stream represents a stream established with a client.
|
2015-12-17 20:37:35 +00:00
|
|
|
// A stream can be bidirectional which is indicated by the request.
|
|
|
|
// The last error will be left in Error().
|
2018-11-26 14:51:42 +00:00
|
|
|
// EOF indicates end of the stream.
|
2018-04-14 18:15:09 +01:00
|
|
|
type Stream interface {
|
2015-12-17 20:37:35 +00:00
|
|
|
Context() context.Context
|
|
|
|
Request() Request
|
2021-02-14 11:28:50 +03:00
|
|
|
Send(msg interface{}) error
|
|
|
|
Recv(msg interface{}) error
|
2015-12-17 20:37:35 +00:00
|
|
|
Error() error
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2018-11-26 14:51:42 +00:00
|
|
|
// Handler interface represents a request handler. It's generated
|
2019-01-10 21:25:31 +00:00
|
|
|
// by passing any type of public concrete object with endpoints into server.NewHandler.
|
2018-11-26 14:51:42 +00:00
|
|
|
// Most will pass in a struct.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// type Greeter struct {}
|
|
|
|
//
|
|
|
|
// func (g *Greeter) Hello(context, request, response) error {
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
type Handler interface {
|
|
|
|
Name() string
|
|
|
|
Handler() interface{}
|
2021-01-29 13:17:32 +03:00
|
|
|
Endpoints() []*register.Endpoint
|
2018-11-26 14:51:42 +00:00
|
|
|
Options() HandlerOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscriber interface represents a subscription to a given topic using
|
2020-05-20 11:49:09 +01:00
|
|
|
// a specific subscriber function or object with endpoints. It mirrors
|
2020-05-12 11:32:01 +01:00
|
|
|
// the handler in its behaviour.
|
2018-11-26 14:51:42 +00:00
|
|
|
type Subscriber interface {
|
|
|
|
Topic() string
|
|
|
|
Subscriber() interface{}
|
2021-01-29 13:17:32 +03:00
|
|
|
Endpoints() []*register.Endpoint
|
2018-11-26 14:51:42 +00:00
|
|
|
Options() SubscriberOptions
|
|
|
|
}
|