2021-01-29 13:17:32 +03:00
|
|
|
// Package micro is a pluggable framework for microservices
|
2023-04-11 22:20:37 +03:00
|
|
|
package micro // import "go.unistack.org/micro/v4"
|
2020-08-28 10:57:42 +03:00
|
|
|
|
|
|
|
import (
|
2020-10-28 10:51:09 +03:00
|
|
|
"fmt"
|
2020-08-28 10:57:42 +03:00
|
|
|
"sync"
|
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/broker"
|
|
|
|
"go.unistack.org/micro/v4/client"
|
|
|
|
"go.unistack.org/micro/v4/config"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
|
|
|
"go.unistack.org/micro/v4/meter"
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/register"
|
|
|
|
"go.unistack.org/micro/v4/router"
|
|
|
|
"go.unistack.org/micro/v4/server"
|
|
|
|
"go.unistack.org/micro/v4/store"
|
|
|
|
"go.unistack.org/micro/v4/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
|
2022-05-03 14:38:44 +03:00
|
|
|
// Logger is for output log from service components
|
2021-01-29 13:17:32 +03:00
|
|
|
Logger(...string) logger.Logger
|
2022-05-03 14:38:44 +03:00
|
|
|
// Config if for config handling via load/save methods and also with ability to watch changes
|
2021-01-29 13:17:32 +03:00
|
|
|
Config(...string) config.Config
|
2022-05-03 14:38:44 +03:00
|
|
|
// Client is for sync calling services via RPC
|
2021-01-29 13:17:32 +03:00
|
|
|
Client(...string) client.Client
|
2022-05-03 14:38:44 +03:00
|
|
|
// Broker is for sending and receiving async events
|
2021-01-29 13:17:32 +03:00
|
|
|
Broker(...string) broker.Broker
|
2022-05-03 14:38:44 +03:00
|
|
|
// Server is for handling requests and broker unmarshaled events
|
2021-01-29 13:17:32 +03:00
|
|
|
Server(...string) server.Server
|
|
|
|
// Store is for key/val store
|
|
|
|
Store(...string) store.Store
|
2022-05-03 14:38:44 +03:00
|
|
|
// Register used by client to lookup other services and server registers on it
|
2021-01-29 13:17:32 +03:00
|
|
|
Register(...string) register.Register
|
|
|
|
// Tracer
|
|
|
|
Tracer(...string) tracer.Tracer
|
|
|
|
// Router
|
|
|
|
Router(...string) router.Router
|
2022-05-03 14:38:44 +03:00
|
|
|
// Meter may be used internally by other component to export metrics
|
2021-01-29 13:17:32 +03:00
|
|
|
Meter(...string) meter.Meter
|
|
|
|
|
|
|
|
// Runtime
|
|
|
|
// Runtime(string) (runtime.Runtime, bool)
|
|
|
|
// Profile
|
|
|
|
// Profile(string) (profile.Profile, bool)
|
2022-05-03 14:38:44 +03:00
|
|
|
// Run the service and wait for stop
|
2021-01-29 13:17:32 +03:00
|
|
|
Run() error
|
2022-05-03 14:38:44 +03:00
|
|
|
// Start the service and not wait
|
2022-03-25 14:24:20 +03:00
|
|
|
Start() error
|
2022-05-03 00:23:43 +03:00
|
|
|
// Stop the service
|
|
|
|
Stop() error
|
2021-01-29 13:17:32 +03:00
|
|
|
// The service implementation
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterHandler is syntactic sugar for registering a handler
|
2023-07-29 00:40:58 +03:00
|
|
|
func RegisterHandler(s server.Server, h interface{}, opts ...options.Option) error {
|
|
|
|
return s.Handle(h, opts...)
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
|
|
|
|
2020-08-28 10:57:42 +03:00
|
|
|
type service struct {
|
2020-11-04 00:38:12 +03:00
|
|
|
sync.RWMutex
|
2022-03-30 15:37:02 +03:00
|
|
|
opts Options
|
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.
|
2023-04-11 23:32:58 +03:00
|
|
|
//
|
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
|
|
|
|
}
|
2023-07-29 00:40:58 +03:00
|
|
|
if err = cfg.Init(options.Context(cfg.Options().Context)); err != nil {
|
2020-12-13 14:36:31 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
for _, log := range s.opts.Loggers {
|
2023-07-29 00:40:58 +03:00
|
|
|
if err = log.Init(options.Context(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 {
|
2023-07-29 00:40:58 +03:00
|
|
|
if err = brk.Init(options.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 {
|
2023-07-29 00:40:58 +03:00
|
|
|
if err = str.Init(options.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 {
|
2023-07-29 00:40:58 +03:00
|
|
|
if err = srv.Init(options.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 {
|
2023-07-29 00:40:58 +03:00
|
|
|
if err = cli.Init(options.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) 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 {
|
2022-04-24 11:08:38 +03:00
|
|
|
return s.opts.Name
|
2020-08-28 10:57:42 +03:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2020-12-04 02:28:45 +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-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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 15:48:43 +03:00
|
|
|
for _, fn := range s.opts.BeforeStart {
|
|
|
|
if err = fn(s.opts.Context); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Loggers[0].V(logger.InfoLevel) {
|
|
|
|
config.Loggers[0].Infof(s.opts.Context, "starting [service] %s version %s", s.Options().Name, s.Options().Version)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-07 20:59:27 +03:00
|
|
|
func getNameIndex(n string, ifaces interface{}) int {
|
|
|
|
values, ok := ifaces.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
for idx, iface := range values {
|
2021-01-29 13:17:32 +03:00
|
|
|
if ifc, ok := iface.(nameIface); ok && ifc.Name() == n {
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|