mirror of
https://github.com/Monibuca/engine.git
synced 2025-10-05 08:36:56 +08:00
👌 IMPROVE: 打印全局API,发布首次超时时间随PublishTimeout配置
This commit is contained in:
29
io.go
29
io.go
@@ -60,6 +60,25 @@ func (i *IO[C]) SetParentCtx(parent context.Context) {
|
|||||||
i.Context, i.CancelFunc = context.WithCancel(parent)
|
i.Context, i.CancelFunc = context.WithCancel(parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *IO[C]) SetStuff(stuffs ...any) {
|
||||||
|
for _, stuff := range stuffs {
|
||||||
|
switch v := stuff.(type) {
|
||||||
|
case context.Context:
|
||||||
|
i.Context, i.CancelFunc = context.WithCancel(v)
|
||||||
|
default:
|
||||||
|
if v, ok := v.(io.Closer); ok {
|
||||||
|
i.Closer = v
|
||||||
|
}
|
||||||
|
if v, ok := v.(io.Reader); ok {
|
||||||
|
i.Reader = v
|
||||||
|
}
|
||||||
|
if v, ok := v.(io.Writer); ok {
|
||||||
|
i.Writer = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (i *IO[C]) OnEvent(event any) {
|
func (i *IO[C]) OnEvent(event any) {
|
||||||
switch event.(type) {
|
switch event.(type) {
|
||||||
case SEclose, SEKick:
|
case SEclose, SEKick:
|
||||||
@@ -98,8 +117,8 @@ func (io *IO[C]) Stop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var BadNameErr = errors.New("Bad Name")
|
var ErrBadName = errors.New("Stream Already Exist")
|
||||||
var StreamIsClosedErr = errors.New("Stream Is Closed")
|
var ErrStreamIsClosed = errors.New("Stream Is Closed")
|
||||||
|
|
||||||
// receive 用于接收发布或者订阅
|
// receive 用于接收发布或者订阅
|
||||||
func (io *IO[C]) receive(streamPath string, specific IIO, conf *C) error {
|
func (io *IO[C]) receive(streamPath string, specific IIO, conf *C) error {
|
||||||
@@ -122,7 +141,7 @@ func (io *IO[C]) receive(streamPath string, specific IIO, conf *C) error {
|
|||||||
s, create := findOrCreateStream(u.Path, wt)
|
s, create := findOrCreateStream(u.Path, wt)
|
||||||
Streams.Unlock()
|
Streams.Unlock()
|
||||||
if s == nil {
|
if s == nil {
|
||||||
return BadNameErr
|
return ErrBadName
|
||||||
}
|
}
|
||||||
io.Config = conf
|
io.Config = conf
|
||||||
if io.Type == "" {
|
if io.Type == "" {
|
||||||
@@ -137,7 +156,7 @@ func (io *IO[C]) receive(streamPath string, specific IIO, conf *C) error {
|
|||||||
s.Warn("kick", zap.String("type", oldPublisher.GetIO().Type))
|
s.Warn("kick", zap.String("type", oldPublisher.GetIO().Type))
|
||||||
oldPublisher.OnEvent(SEKick{})
|
oldPublisher.OnEvent(SEKick{})
|
||||||
} else {
|
} else {
|
||||||
return BadNameErr
|
return ErrBadName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.PublishTimeout = util.Second2Duration(v.PublishTimeout)
|
s.PublishTimeout = util.Second2Duration(v.PublishTimeout)
|
||||||
@@ -169,7 +188,7 @@ func (io *IO[C]) receive(streamPath string, specific IIO, conf *C) error {
|
|||||||
return promise.Catch()
|
return promise.Catch()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return StreamIsClosedErr
|
return ErrStreamIsClosed
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClientIO 作为Client角色(Puller,Pusher)的公共结构体
|
// ClientIO 作为Client角色(Puller,Pusher)的公共结构体
|
||||||
|
14
plugin.go
14
plugin.go
@@ -85,9 +85,9 @@ func (opt *Plugin) handleFunc(pattern string, handler func(http.ResponseWriter,
|
|||||||
if opt != Engine {
|
if opt != Engine {
|
||||||
pattern = "/" + strings.ToLower(opt.Name) + pattern
|
pattern = "/" + strings.ToLower(opt.Name) + pattern
|
||||||
opt.Debug("http handle added to engine:" + pattern)
|
opt.Debug("http handle added to engine:" + pattern)
|
||||||
apiList = append(apiList, pattern)
|
|
||||||
EngineConfig.HandleFunc(pattern, opt.logHandler(pattern, handler))
|
EngineConfig.HandleFunc(pattern, opt.logHandler(pattern, handler))
|
||||||
}
|
}
|
||||||
|
apiList = append(apiList, pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取独立配置合并入总配置中
|
// 读取独立配置合并入总配置中
|
||||||
@@ -229,7 +229,7 @@ func (opt *Plugin) SubscribeBlock(streamPath string, sub ISubscriber, t byte) (e
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var NoPullConfigErr = errors.New("no pull config")
|
var ErrNoPullConfig = errors.New("no pull config")
|
||||||
var Pullers sync.Map
|
var Pullers sync.Map
|
||||||
|
|
||||||
func (opt *Plugin) Pull(streamPath string, url string, puller IPuller, save bool) (err error) {
|
func (opt *Plugin) Pull(streamPath string, url string, puller IPuller, save bool) (err error) {
|
||||||
@@ -241,7 +241,7 @@ func (opt *Plugin) Pull(streamPath string, url string, puller IPuller, save bool
|
|||||||
}()
|
}()
|
||||||
conf, ok := opt.Config.(config.PullConfig)
|
conf, ok := opt.Config.(config.PullConfig)
|
||||||
if !ok {
|
if !ok {
|
||||||
return NoPullConfigErr
|
return ErrNoPullConfig
|
||||||
}
|
}
|
||||||
pullConf := conf.GetPullConfig()
|
pullConf := conf.GetPullConfig()
|
||||||
|
|
||||||
@@ -266,7 +266,9 @@ func (opt *Plugin) Pull(streamPath string, url string, puller IPuller, save bool
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = opt.Publish(streamPath, puller); err != nil {
|
if err = opt.Publish(streamPath, puller); err != nil {
|
||||||
if Streams.Get(streamPath).Publisher != puller {
|
if puber := Streams.Get(streamPath).Publisher; puber != puller {
|
||||||
|
io := puber.GetIO()
|
||||||
|
opt.Warn("puller is not publisher", zap.String("ID", io.ID), zap.String("Type", io.Type), zap.Error(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -289,7 +291,7 @@ func (opt *Plugin) Pull(streamPath string, url string, puller IPuller, save bool
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var NoPushConfigErr = errors.New("no push config")
|
var ErrNoPushConfig = errors.New("no push config")
|
||||||
var Pushers sync.Map
|
var Pushers sync.Map
|
||||||
|
|
||||||
func (opt *Plugin) Push(streamPath string, url string, pusher IPusher, save bool) (err error) {
|
func (opt *Plugin) Push(streamPath string, url string, pusher IPusher, save bool) (err error) {
|
||||||
@@ -302,7 +304,7 @@ func (opt *Plugin) Push(streamPath string, url string, pusher IPusher, save bool
|
|||||||
}()
|
}()
|
||||||
conf, ok := opt.Config.(config.PushConfig)
|
conf, ok := opt.Config.(config.PushConfig)
|
||||||
if !ok {
|
if !ok {
|
||||||
return NoPushConfigErr
|
return ErrNoPushConfig
|
||||||
}
|
}
|
||||||
pushConfig := conf.GetPushConfig()
|
pushConfig := conf.GetPushConfig()
|
||||||
|
|
||||||
|
32
stream.go
32
stream.go
@@ -265,7 +265,7 @@ func (r *Stream) action(action StreamAction) (ok bool) {
|
|||||||
case STATE_PUBLISHING:
|
case STATE_PUBLISHING:
|
||||||
stateEvent = SEpublish{event}
|
stateEvent = SEpublish{event}
|
||||||
r.broadcast(stateEvent)
|
r.broadcast(stateEvent)
|
||||||
r.timeout.Reset(time.Second * 10) // 5秒心跳,检测track的存活度
|
r.timeout.Reset(r.PublishTimeout) // 5秒心跳,检测track的存活度
|
||||||
case STATE_WAITCLOSE:
|
case STATE_WAITCLOSE:
|
||||||
stateEvent = SEwaitClose{event}
|
stateEvent = SEwaitClose{event}
|
||||||
r.timeout.Reset(r.DelayCloseTimeout)
|
r.timeout.Reset(r.DelayCloseTimeout)
|
||||||
@@ -456,11 +456,11 @@ func (s *Stream) run() {
|
|||||||
if s.Publisher == v.Value {
|
if s.Publisher == v.Value {
|
||||||
s.Publisher = nil
|
s.Publisher = nil
|
||||||
}
|
}
|
||||||
v.Reject(BadNameErr)
|
v.Reject(ErrBadName)
|
||||||
}
|
}
|
||||||
case *util.Promise[ISubscriber, struct{}]:
|
case *util.Promise[ISubscriber, struct{}]:
|
||||||
if s.IsClosed() {
|
if s.IsClosed() {
|
||||||
v.Reject(StreamIsClosedErr)
|
v.Reject(ErrStreamIsClosed)
|
||||||
}
|
}
|
||||||
suber := v.Value
|
suber := v.Value
|
||||||
io := suber.GetIO()
|
io := suber.GetIO()
|
||||||
@@ -512,6 +512,18 @@ func (s *Stream) run() {
|
|||||||
if len(s.Subscribers) == 1 {
|
if len(s.Subscribers) == 1 {
|
||||||
s.action(ACTION_FIRSTENTER)
|
s.action(ACTION_FIRSTENTER)
|
||||||
}
|
}
|
||||||
|
case TrackRemoved:
|
||||||
|
name := v.GetBase().Name
|
||||||
|
if t, ok := s.Tracks.Delete(name); ok {
|
||||||
|
s.Info("track -1", zap.String("name", name))
|
||||||
|
s.broadcast(v)
|
||||||
|
if s.Tracks.Len() == 0 {
|
||||||
|
s.action(ACTION_PUBLISHLOST)
|
||||||
|
}
|
||||||
|
if dt, ok := t.(*track.Data); ok {
|
||||||
|
dt.Dispose()
|
||||||
|
}
|
||||||
|
}
|
||||||
case Track:
|
case Track:
|
||||||
name := v.GetBase().Name
|
name := v.GetBase().Name
|
||||||
if s.Tracks.Add(name, v) {
|
if s.Tracks.Add(name, v) {
|
||||||
@@ -526,18 +538,6 @@ func (s *Stream) run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case TrackRemoved:
|
|
||||||
name := v.GetBase().Name
|
|
||||||
if t, ok := s.Tracks.Delete(name); ok {
|
|
||||||
s.Info("track -1", zap.String("name", name))
|
|
||||||
s.broadcast(v)
|
|
||||||
if s.Tracks.Len() == 0 {
|
|
||||||
s.action(ACTION_PUBLISHLOST)
|
|
||||||
}
|
|
||||||
if dt, ok := t.(*track.Data); ok {
|
|
||||||
dt.Dispose()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case StreamAction:
|
case StreamAction:
|
||||||
s.action(v)
|
s.action(v)
|
||||||
default:
|
default:
|
||||||
@@ -545,7 +545,7 @@ func (s *Stream) run() {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for sub, w := range waitP {
|
for sub, w := range waitP {
|
||||||
w.Reject(StreamIsClosedErr)
|
w.Reject(ErrStreamIsClosed)
|
||||||
delete(waitP, sub)
|
delete(waitP, sub)
|
||||||
}
|
}
|
||||||
s.Tracks.Range(func(_ string, t Track) {
|
s.Tracks.Range(func(_ string, t Track) {
|
||||||
|
Reference in New Issue
Block a user