88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
package uniss
|
|
|
|
// TODO
|
|
// 1.data center object 2.swarm stop after scheduler
|
|
|
|
import (
|
|
"context"
|
|
"dashboard/logger"
|
|
"dashboard/models"
|
|
"dashboard/settings"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
var log *logger.Logger
|
|
|
|
type UnisStation struct {
|
|
cancelFunc context.CancelFunc
|
|
commonService *commonService
|
|
communicateService *communicateService
|
|
dataCenterService *dataCenterService
|
|
}
|
|
|
|
func NewUnis(_log *logger.Logger, conf *settings.UnisConfig) (res *UnisStation, err error) {
|
|
res = new(UnisStation)
|
|
log = _log
|
|
|
|
res.dataCenterService = newDataCenterService(conf)
|
|
res.communicateService, err = newcommunicateService(conf)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
res.commonService = newcommonService(res.communicateService)
|
|
|
|
return
|
|
}
|
|
|
|
func (u *UnisStation) GetHttpServerChannel() chan *models.UnisHttpRequest {
|
|
return u.communicateService.httpServerHandler.httpC
|
|
}
|
|
|
|
func (u *UnisStation) stop() {
|
|
if u.cancelFunc != nil {
|
|
u.cancelFunc()
|
|
}
|
|
}
|
|
|
|
// High performance threads do not allow any blocking
|
|
// If there is blocking work, asynchronous methods need to be used
|
|
// Ensure the last exit
|
|
func (u *UnisStation) mainthread() error {
|
|
var ctx context.Context
|
|
ctx, u.cancelFunc = context.WithCancel(context.TODO())
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
log.Error("Unis mesageHandle cancel by ctx")
|
|
return ctx.Err()
|
|
case httpMsg := <-u.communicateService.httpServerHandler.httpC:
|
|
u.communicateService.httpServerHandler.httpServerHandle(httpMsg)
|
|
case httpMsg := <-u.communicateService.httpClientHandler.httpC:
|
|
u.communicateService.httpClientHandler.httpClientHandle(httpMsg)
|
|
// default:
|
|
}
|
|
}
|
|
}
|
|
|
|
func (u *UnisStation) Run(ctx context.Context) error {
|
|
ewgs, nctx := errgroup.WithContext(ctx)
|
|
|
|
ewgs.Go(func() error {
|
|
defer u.stop()
|
|
return u.communicateService.Run(nctx)
|
|
})
|
|
|
|
ewgs.Go(func() error {
|
|
return u.mainthread()
|
|
})
|
|
|
|
if err := ewgs.Wait(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|