2021-01-29 13:17:32 +03:00
|
|
|
// Package micro is a pluggable framework for microservices
|
2022-05-03 14:38:44 +03:00
|
|
|
package micro // import "go.unistack.org/micro/v3"
|
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"
|
|
|
|
|
2024-10-06 13:50:59 +03:00
|
|
|
"github.com/KimMachineGun/automemlimit/memlimit"
|
2024-11-20 22:35:36 +03:00
|
|
|
"go.uber.org/automaxprocs/maxprocs"
|
2021-10-02 19:55:07 +03:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2024-10-06 13:50:59 +03:00
|
|
|
func init() {
|
2024-11-20 22:35:36 +03:00
|
|
|
maxprocs.Set()
|
2024-10-06 13:50:59 +03:00
|
|
|
memlimit.SetGoMemLimitWithOpts(
|
|
|
|
memlimit.WithRatio(0.9),
|
|
|
|
memlimit.WithProvider(
|
|
|
|
memlimit.ApplyFallback(
|
|
|
|
memlimit.FromCgroup,
|
|
|
|
memlimit.FromSystem,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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 {
|
2024-10-25 17:20:35 +03:00
|
|
|
done chan struct{}
|
2022-03-30 15:37:02 +03:00
|
|
|
opts Options
|
2024-03-01 21:39:31 +03:00
|
|
|
sync.RWMutex
|
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 {
|
2024-10-25 17:20:35 +03:00
|
|
|
return &service{opts: NewOptions(opts...), done: make(chan struct{})}
|
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-11-13 08:19:44 +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
|
|
|
|
}
|
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-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) 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) {
|
2024-10-12 12:37:43 +03:00
|
|
|
config.Loggers[0].Info(s.opts.Context, fmt.Sprintf("starting [service] %s version %s", s.Options().Name, s.Options().Version))
|
2022-03-30 15:48:43 +03:00
|
|
|
}
|
|
|
|
|
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) {
|
2024-10-12 12:37:43 +03:00
|
|
|
config.Loggers[0].Info(s.opts.Context, fmt.Sprintf("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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-25 17:20:35 +03:00
|
|
|
close(s.done)
|
|
|
|
|
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
|
2024-10-25 17:20:35 +03:00
|
|
|
<-s.done
|
2020-08-28 10:57:42 +03:00
|
|
|
|
|
|
|
return s.Stop()
|
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
|
2024-03-04 01:09:08 +03:00
|
|
|
type Namer interface {
|
|
|
|
Name() string
|
|
|
|
}
|
2024-03-01 21:39:31 +03:00
|
|
|
|
2024-03-04 01:09:08 +03:00
|
|
|
func getNameIndex[T Namer](n string, ifaces []T) int {
|
|
|
|
for idx, iface := range ifaces {
|
|
|
|
if iface.Name() == n {
|
|
|
|
return idx
|
2023-11-13 08:19:44 +03:00
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|