123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package proxy
- import (
- "log"
- "runtime/debug"
- "sync"
- )
- var servicesMap = new(sync.Map)
- type Service struct {
- TCPConn TCP
- Name string
- }
- func (s *Service) Stop() {
- servicesMap.Delete(s.Name)
- s.TCPConn.Close()
- }
- func Run(name string, args TCPArgs) *Service {
- 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 {
- log.Fatalf("%s servcie crashed, ERR: %s\ntrace:%s", name, recoverErr, string(debug.Stack()))
- }
- }()
- startErr := service.TCPConn.Start()
- if startErr != nil {
- log.Fatalf("%s servcie fail, ERR: %s", name, startErr)
- }
- }()
- return service
- }
|