12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package proxy
- import (
- "go.uber.org/zap"
- "runtime/debug"
- "sync"
- )
- var (
- servicesMap = new(sync.Map)
- logger *zap.Logger
- )
- type Service struct {
- TCPConn TCP
- Name string
- }
- func (s *Service) Stop() {
- servicesMap.Delete(s.Name)
- s.TCPConn.Close()
- }
- func Run(name string, args TCPArgs, zapLogger *zap.Logger) *Service {
- logger = zapLogger
- service := &Service{
- TCPConn: &tcp{cfg: args},
- Name: name,
- }
- store, loaded := servicesMap.LoadOrStore(name, service)
- if loaded {
- service = store.(*Service)
- }
- go func() {
- defer func() {
- recoverErr := recover()
- if recoverErr != nil {
- logger.Sugar().Errorf("%s servcie crashed, ERR: %s\ntrace:%s", name, recoverErr, string(debug.Stack()))
- }
- }()
- startErr := service.TCPConn.Start()
- if startErr != nil {
- logger.Sugar().Errorf("%s servcie fail, ERR: %s", name, startErr)
- }
- }()
- return service
- }
|