79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
package unisc
|
||
|
||
import (
|
||
"dashboard/models"
|
||
"dashboard/utils"
|
||
"net/http"
|
||
"time"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
type httpHandle struct {
|
||
httpC []chan *models.UnisHttpRequest
|
||
}
|
||
|
||
func newhttpHandle(httpc []chan *models.UnisHttpRequest) *httpHandle {
|
||
res := new(httpHandle)
|
||
|
||
res.httpC = httpc
|
||
|
||
return res
|
||
}
|
||
|
||
func (u *httpHandle) httpSendWithExpire(channel int, req *models.UnisHttpRequest) (*models.UnisHttpResponse, error) {
|
||
select {
|
||
case u.httpC[channel] <- req:
|
||
case <-time.After(2 * time.Second):
|
||
return nil, models.ErrorSendTimeOut
|
||
}
|
||
|
||
select {
|
||
case resp := <-req.ResC:
|
||
return resp, nil
|
||
case <-time.After(10 * time.Second): // 这里的消息必须要等到,如果等不到业务进程可能会出现空指针操作
|
||
return nil, models.ErrorReciveTimeOut
|
||
}
|
||
}
|
||
|
||
func (u *httpHandle) stationConfig(c *gin.Context) error {
|
||
log, _ := utils.GetLogFromContext(c)
|
||
|
||
config := new(models.StationConfigParams)
|
||
if err := c.ShouldBindJSON(config); err != nil {
|
||
log.Sugar().Errorf("stationConfig bind json err: %v", err)
|
||
return err
|
||
}
|
||
|
||
// 不再使用channel的方式,而是使用配置文件启动不同进程的方式
|
||
// 当前先兼容使用channel的方式
|
||
channel := c.GetInt(models.GinContextChannel)
|
||
if channel > len(u.httpC)-1 {
|
||
log.Sugar().Errorf("stationConfig channel err: %d", channel)
|
||
return models.ErrorParamsErr
|
||
}
|
||
|
||
req, err := getUnisHttpContextFromContext(c)
|
||
if err != nil {
|
||
log.Sugar().Errorf("stationConfig get http req from context err: %v", err)
|
||
return models.ErrorParamsErr
|
||
}
|
||
|
||
req.SetReqParam(models.UnisHttpUrl(c.Request.URL.String()).GetMsgId(), config)
|
||
|
||
// req := &models.UnisHttpRequest{
|
||
// Id: models.UnisHttpUrl(c.Request.URL.String()).GetMsgId(),
|
||
// Msg: config,
|
||
// }
|
||
|
||
resp, err := u.httpSendWithExpire(channel, req)
|
||
if err != nil {
|
||
log.Sugar().Errorf("stationConfig send or recive err: %v", err)
|
||
return err
|
||
}
|
||
|
||
c.JSON(http.StatusOK, resp)
|
||
|
||
return nil
|
||
}
|