2021-01-29 13:17:32 +03:00
|
|
|
// Package micro is a pluggable framework for microservices
|
2020-08-28 10:57:42 +03:00
|
|
|
package micro
|
|
|
|
|
|
|
|
import (
|
2020-10-28 10:51:09 +03:00
|
|
|
"fmt"
|
2020-08-28 10:57:42 +03:00
|
|
|
"sync"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/auth"
|
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/config"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/micro/v3/meter"
|
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/router"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
|
|
|
"go.unistack.org/micro/v3/store"
|
|
|
|
"go.unistack.org/micro/v3/tracer"
|
2020-08-28 10:57:42 +03:00
|
|
|
)
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// Service is an interface that wraps the lower level components.
|
|
|
|
// Its works as container with building blocks for service.
|
|
|
|
type Service interface {
|
|
|
|
// The service name
|
|
|
|
Name() string
|
|
|
|
// Init initialises options
|
|
|
|
Init(...Option) error
|
|
|
|
// Options returns the current options
|
|
|
|
Options() Options
|
|
|
|
// Auth is for handling auth
|
|
|
|
Auth(...string) auth.Auth
|
|
|
|
// Logger is for logs
|
|
|
|
Logger(...string) logger.Logger
|
|
|
|
// Config if for config
|
|
|
|
Config(...string) config.Config
|
|
|
|
// Client is for calling services
|
|
|
|
Client(...string) client.Client
|
|
|
|
// Broker is for sending and receiving events
|
|
|
|
Broker(...string) broker.Broker
|
|
|
|
// Server is for handling requests and events
|
|
|
|
Server(...string) server.Server
|
|
|
|
// Store is for key/val store
|
|
|
|
Store(...string) store.Store
|
|
|
|
// Register
|
|
|
|
Register(...string) register.Register
|
|
|
|
// Tracer
|
|
|
|
Tracer(...string) tracer.Tracer
|
|
|
|
// Router
|
|
|
|
Router(...string) router.Router
|
|
|
|
// Meter
|
|
|
|
Meter(...string) meter.Meter
|
|
|
|
|
|
|
|
// Runtime
|
|
|
|
// Runtime(string) (runtime.Runtime, bool)
|
|
|
|
// Profile
|
|
|
|
// Profile(string) (profile.Profile, bool)
|
|
|
|
// Run the service
|
|
|
|
Run() error
|
|
|
|
// The service implementation
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterHandler is syntactic sugar for registering a handler
|
|
|
|
func RegisterHandler(s server.Server, h interface{}, opts ...server.HandlerOption) error {
|
|
|
|
return s.Handle(s.NewHandler(h, opts...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterSubscriber is syntactic sugar for registering a subscriber
|
|
|
|
func RegisterSubscriber(topic string, s server.Server, h interface{}, opts ...server.SubscriberOption) error {
|
|
|
|
return s.Subscribe(s.NewSubscriber(topic, h, opts...))
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:57:42 +03:00
|
|
|
type service struct {
|
|
|
|
opts Options
|
2020-11-04 00:38:12 +03:00
|
|
|
sync.RWMutex
|
2020-11-10 10:59:26 +03:00
|
|
|
// once sync.Once
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// NewService creates and returns a new Service based on the packages within.
|
|
|
|
func NewService(opts ...Option) Service {
|
|
|
|
return &service{opts: NewOptions(opts...)}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Name() string {
|
2021-01-29 13:17:32 +03:00
|
|
|
return s.opts.Name
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init initialises options. Additionally it calls cmd.Init
|
|
|
|
// which parses command line flags. cmd.Init is only called
|
|
|
|
// on first Init.
|
2021-02-14 16:16:01 +03:00
|
|
|
//nolint:gocyclo
|
2020-10-16 09:38:57 +03:00
|
|
|
func (s *service) Init(opts ...Option) error {
|
2021-01-29 13:17:32 +03:00
|
|
|
var err error
|
2020-08-28 10:57:42 +03:00
|
|
|
// process options
|
|
|
|
for _, o := range opts {
|
2021-02-13 01:46:16 +03:00
|
|
|
if err = o(&s.opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
2020-12-13 14:36:31 +03:00
|
|
|
for _, cfg := range s.opts.Configs {
|
2020-12-20 01:00:45 +03:00
|
|
|
if cfg.Options().Struct == nil {
|
|
|
|
// skip config as the struct not passed
|
|
|
|
continue
|
|
|
|
}
|
2021-03-28 23:42:02 +03:00
|
|
|
if err = cfg.Init(config.Context(cfg.Options().Context)); err != nil {
|
2020-12-13 14:36:31 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-28 23:42:02 +03:00
|
|
|
if err = cfg.Load(cfg.Options().Context); err != nil {
|
2020-11-13 14:52:19 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
|
2020-12-13 14:36:31 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, log := range s.opts.Loggers {
|
2021-03-28 23:42:02 +03:00
|
|
|
if err = log.Init(logger.WithContext(log.Options().Context)); err != nil {
|
2020-12-13 14:36:31 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-12-04 02:28:45 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, reg := range s.opts.Registers {
|
2021-03-28 23:42:02 +03:00
|
|
|
if err = reg.Init(register.Context(reg.Options().Context)); err != nil {
|
2020-10-16 09:38:57 +03:00
|
|
|
return err
|
2020-09-07 13:38:52 +03:00
|
|
|
}
|
2020-09-07 15:29:43 +03:00
|
|
|
}
|
2020-09-07 13:38:52 +03:00
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, brk := range s.opts.Brokers {
|
2021-03-28 23:42:02 +03:00
|
|
|
if err = brk.Init(broker.Context(brk.Options().Context)); err != nil {
|
2020-10-16 09:38:57 +03:00
|
|
|
return err
|
2020-09-07 13:38:52 +03:00
|
|
|
}
|
2020-09-07 15:29:43 +03:00
|
|
|
}
|
2020-09-07 13:38:52 +03:00
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, str := range s.opts.Stores {
|
2021-03-28 23:42:02 +03:00
|
|
|
if err = str.Init(store.Context(str.Options().Context)); err != nil {
|
2020-10-16 09:38:57 +03:00
|
|
|
return err
|
2020-09-07 13:38:52 +03:00
|
|
|
}
|
2020-09-07 15:29:43 +03:00
|
|
|
}
|
2020-09-07 13:38:52 +03:00
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, srv := range s.opts.Servers {
|
2021-03-28 23:42:02 +03:00
|
|
|
if err = srv.Init(server.Context(srv.Options().Context)); err != nil {
|
2020-10-16 09:38:57 +03:00
|
|
|
return err
|
2020-09-07 13:38:52 +03:00
|
|
|
}
|
2020-09-07 15:29:43 +03:00
|
|
|
}
|
2020-09-07 13:38:52 +03:00
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, cli := range s.opts.Clients {
|
2021-03-28 23:42:02 +03:00
|
|
|
if err = cli.Init(client.Context(cli.Options().Context)); err != nil {
|
2020-10-16 09:38:57 +03:00
|
|
|
return err
|
2020-09-07 13:38:52 +03:00
|
|
|
}
|
2020-09-07 15:29:43 +03:00
|
|
|
}
|
2020-09-07 13:38:52 +03:00
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Options() Options {
|
|
|
|
return s.opts
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func (s *service) Broker(names ...string) broker.Broker {
|
|
|
|
idx := 0
|
|
|
|
if len(names) == 1 {
|
|
|
|
idx = getNameIndex(names[0], s.opts.Brokers)
|
|
|
|
}
|
|
|
|
return s.opts.Brokers[idx]
|
2020-10-16 09:38:57 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func (s *service) Tracer(names ...string) tracer.Tracer {
|
|
|
|
idx := 0
|
|
|
|
if len(names) == 1 {
|
|
|
|
idx = getNameIndex(names[0], s.opts.Tracers)
|
|
|
|
}
|
|
|
|
return s.opts.Tracers[idx]
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func (s *service) Config(names ...string) config.Config {
|
|
|
|
idx := 0
|
|
|
|
if len(names) == 1 {
|
|
|
|
idx = getNameIndex(names[0], s.opts.Configs)
|
|
|
|
}
|
|
|
|
return s.opts.Configs[idx]
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func (s *service) Client(names ...string) client.Client {
|
|
|
|
idx := 0
|
|
|
|
if len(names) == 1 {
|
|
|
|
idx = getNameIndex(names[0], s.opts.Clients)
|
|
|
|
}
|
|
|
|
return s.opts.Clients[idx]
|
2020-11-06 00:04:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func (s *service) Server(names ...string) server.Server {
|
|
|
|
idx := 0
|
|
|
|
if len(names) == 1 {
|
|
|
|
idx = getNameIndex(names[0], s.opts.Servers)
|
|
|
|
}
|
|
|
|
return s.opts.Servers[idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Store(names ...string) store.Store {
|
|
|
|
idx := 0
|
|
|
|
if len(names) == 1 {
|
|
|
|
idx = getNameIndex(names[0], s.opts.Stores)
|
|
|
|
}
|
|
|
|
return s.opts.Stores[idx]
|
2020-11-06 00:04:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func (s *service) Register(names ...string) register.Register {
|
|
|
|
idx := 0
|
|
|
|
if len(names) == 1 {
|
|
|
|
idx = getNameIndex(names[0], s.opts.Registers)
|
|
|
|
}
|
|
|
|
return s.opts.Registers[idx]
|
2020-11-06 00:04:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func (s *service) Logger(names ...string) logger.Logger {
|
|
|
|
idx := 0
|
|
|
|
if len(names) == 1 {
|
|
|
|
idx = getNameIndex(names[0], s.opts.Loggers)
|
|
|
|
}
|
|
|
|
return s.opts.Loggers[idx]
|
2020-11-06 00:04:00 +03:00
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func (s *service) Auth(names ...string) auth.Auth {
|
|
|
|
idx := 0
|
|
|
|
if len(names) == 1 {
|
|
|
|
idx = getNameIndex(names[0], s.opts.Auths)
|
|
|
|
}
|
|
|
|
return s.opts.Auths[idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Router(names ...string) router.Router {
|
|
|
|
idx := 0
|
|
|
|
if len(names) == 1 {
|
|
|
|
idx = getNameIndex(names[0], s.opts.Routers)
|
|
|
|
}
|
|
|
|
return s.opts.Routers[idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Meter(names ...string) meter.Meter {
|
|
|
|
idx := 0
|
|
|
|
if len(names) == 1 {
|
|
|
|
idx = getNameIndex(names[0], s.opts.Meters)
|
|
|
|
}
|
|
|
|
return s.opts.Meters[idx]
|
2020-11-06 00:04:00 +03:00
|
|
|
}
|
|
|
|
|
2020-08-28 10:57:42 +03:00
|
|
|
func (s *service) String() string {
|
|
|
|
return "micro"
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
//nolint:gocyclo
|
2020-08-28 10:57:42 +03:00
|
|
|
func (s *service) Start() error {
|
2020-10-28 10:51:09 +03:00
|
|
|
var err error
|
2020-11-04 00:38:12 +03:00
|
|
|
|
|
|
|
s.RLock()
|
|
|
|
config := s.opts
|
|
|
|
s.RUnlock()
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if config.Loggers[0].V(logger.InfoLevel) {
|
|
|
|
config.Loggers[0].Infof(s.opts.Context, "starting [service] %s", s.Name())
|
2020-10-09 12:15:55 +03:00
|
|
|
}
|
|
|
|
|
2020-08-28 10:57:42 +03:00
|
|
|
for _, fn := range s.opts.BeforeStart {
|
2020-12-04 02:28:45 +03:00
|
|
|
if err = fn(s.opts.Context); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cfg := range s.opts.Configs {
|
2020-12-20 01:00:45 +03:00
|
|
|
if cfg.Options().Struct == nil {
|
|
|
|
// skip config as the struct not passed
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if err = cfg.Load(s.opts.Context); err != nil {
|
2020-08-28 10:57:42 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if len(s.opts.Servers) == 0 {
|
2020-10-28 10:51:09 +03:00
|
|
|
return fmt.Errorf("cant start nil server")
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, reg := range s.opts.Registers {
|
|
|
|
if err = reg.Connect(s.opts.Context); err != nil {
|
2020-10-28 10:51:09 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, brk := range s.opts.Brokers {
|
|
|
|
if err = brk.Connect(s.opts.Context); err != nil {
|
2020-10-28 10:51:09 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, str := range s.opts.Stores {
|
|
|
|
if err = str.Connect(s.opts.Context); err != nil {
|
2020-10-28 10:51:09 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, srv := range s.opts.Servers {
|
|
|
|
if err = srv.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range s.opts.AfterStart {
|
2020-12-04 02:28:45 +03:00
|
|
|
if err = fn(s.opts.Context); err != nil {
|
2020-08-28 10:57:42 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Stop() error {
|
2020-11-04 00:38:12 +03:00
|
|
|
s.RLock()
|
|
|
|
config := s.opts
|
|
|
|
s.RUnlock()
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if config.Loggers[0].V(logger.InfoLevel) {
|
|
|
|
config.Loggers[0].Infof(s.opts.Context, "stoppping [service] %s", s.Name())
|
2020-10-09 12:15:55 +03:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:43:16 +03:00
|
|
|
var err error
|
2020-08-28 10:57:42 +03:00
|
|
|
for _, fn := range s.opts.BeforeStop {
|
2020-12-04 02:28:45 +03:00
|
|
|
if err = fn(s.opts.Context); err != nil {
|
2020-09-05 02:43:16 +03:00
|
|
|
return err
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, srv := range s.opts.Servers {
|
|
|
|
if err = srv.Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range s.opts.AfterStop {
|
2020-12-04 02:28:45 +03:00
|
|
|
if err = fn(s.opts.Context); err != nil {
|
2020-09-05 02:43:16 +03:00
|
|
|
return err
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, reg := range s.opts.Registers {
|
|
|
|
if err = reg.Disconnect(s.opts.Context); err != nil {
|
2020-10-28 12:02:57 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, brk := range s.opts.Brokers {
|
|
|
|
if err = brk.Disconnect(s.opts.Context); err != nil {
|
2020-10-28 12:02:57 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, str := range s.opts.Stores {
|
|
|
|
if err = str.Disconnect(s.opts.Context); err != nil {
|
2020-10-28 12:02:57 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:43:16 +03:00
|
|
|
return nil
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Run() error {
|
|
|
|
// start the profiler
|
2021-01-29 13:17:32 +03:00
|
|
|
/*
|
|
|
|
if s.opts.Profile != nil {
|
|
|
|
// to view mutex contention
|
|
|
|
rtime.SetMutexProfileFraction(5)
|
|
|
|
// to view blocking profile
|
|
|
|
rtime.SetBlockProfileRate(1)
|
|
|
|
|
|
|
|
if err := s.opts.Profile.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.opts.Profile.Stop()
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
*/
|
2020-08-28 10:57:42 +03:00
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait on context cancel
|
2020-11-10 10:59:26 +03:00
|
|
|
<-s.opts.Context.Done()
|
2020-08-28 10:57:42 +03:00
|
|
|
|
|
|
|
return s.Stop()
|
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
|
|
|
|
type nameIface interface {
|
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNameIndex(n string, ifaces ...interface{}) int {
|
|
|
|
for idx, iface := range ifaces {
|
|
|
|
if ifc, ok := iface.(nameIface); ok && ifc.Name() == n {
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|