@@ -7,14 +7,16 @@ import (
77 "time"
88
99 "google.golang.org/protobuf/types/known/emptypb"
10+ "gorm.io/gorm"
1011 "m7s.live/v5/pb"
1112)
1213
1314type AliasStream struct {
1415 * Publisher `gorm:"-:all"`
1516 AutoRemove bool
1617 StreamPath string
17- Alias string `gorm:"primarykey"`
18+ Alias string `gorm:"primarykey"`
19+ Args url.Values `gorm:"-"`
1820}
1921
2022func (a * AliasStream ) GetKey () string {
@@ -24,14 +26,49 @@ func (a *AliasStream) GetKey() string {
2426// StreamAliasDB 用于存储流别名的数据库模型
2527type StreamAliasDB struct {
2628 AliasStream
27- CreatedAt time.Time `yaml:"-"`
28- UpdatedAt time.Time `yaml:"-"`
29+ ArgsString string `gorm:"column:args;type:text"`
30+ CreatedAt time.Time `yaml:"-"`
31+ UpdatedAt time.Time `yaml:"-"`
2932}
3033
3134func (StreamAliasDB ) TableName () string {
3235 return "stream_alias"
3336}
3437
38+ // BeforeSave 保存前序列化查询参数
39+ func (db * StreamAliasDB ) BeforeSave (tx * gorm.DB ) error {
40+ if len (db .Args ) > 0 {
41+ db .ArgsString = db .Args .Encode ()
42+ } else {
43+ db .ArgsString = ""
44+ }
45+ return nil
46+ }
47+
48+ // BeforeCreate 创建前序列化查询参数
49+ func (db * StreamAliasDB ) BeforeCreate (tx * gorm.DB ) error {
50+ return db .BeforeSave (tx )
51+ }
52+
53+ // BeforeUpdate 更新前序列化查询参数
54+ func (db * StreamAliasDB ) BeforeUpdate (tx * gorm.DB ) error {
55+ return db .BeforeSave (tx )
56+ }
57+
58+ // AfterFind 查询后反序列化查询参数
59+ func (db * StreamAliasDB ) AfterFind (tx * gorm.DB ) error {
60+ if db .ArgsString != "" {
61+ var err error
62+ db .Args , err = url .ParseQuery (db .ArgsString )
63+ if err != nil {
64+ db .Args = nil
65+ }
66+ } else {
67+ db .Args = nil
68+ }
69+ return nil
70+ }
71+
3572func (s * Server ) initStreamAlias () {
3673 if s .DB == nil {
3774 return
@@ -75,13 +112,15 @@ func (s *Server) SetStreamAlias(ctx context.Context, req *pb.SetStreamAliasReque
75112 return
76113 }
77114 req .StreamPath = strings .TrimPrefix (u .Path , "/" )
115+ queryParams := u .Query ()
78116 publisher , canReplace := s .Streams .Get (req .StreamPath )
79117 if ! canReplace {
80- defer s .OnSubscribe (req .StreamPath , u . Query () )
118+ defer s .OnSubscribe (req .StreamPath , queryParams )
81119 }
82120 if aliasInfo , ok := s .AliasStreams .Get (req .Alias ); ok { //modify alias
83121 oldStreamPath := aliasInfo .StreamPath
84122 aliasInfo .AutoRemove = req .AutoRemove
123+ aliasInfo .Args = queryParams
85124 if aliasInfo .StreamPath != req .StreamPath {
86125 aliasInfo .StreamPath = req .StreamPath
87126 if canReplace {
@@ -96,16 +135,18 @@ func (s *Server) SetStreamAlias(ctx context.Context, req *pb.SetStreamAliasReque
96135 }
97136 // 更新数据库中的别名
98137 if s .DB != nil {
99- s . DB . Where ( "alias = ?" , req . Alias ). Assign ( aliasInfo ). FirstOrCreate ( & StreamAliasDB {
138+ dbAlias := & StreamAliasDB {
100139 AliasStream : * aliasInfo ,
101- })
140+ }
141+ s .DB .Where ("alias = ?" , req .Alias ).Save (dbAlias )
102142 }
103143 s .Info ("modify alias" , "alias" , req .Alias , "oldStreamPath" , oldStreamPath , "streamPath" , req .StreamPath , "replace" , ok && canReplace )
104144 } else { // create alias
105145 aliasInfo := AliasStream {
106146 AutoRemove : req .AutoRemove ,
107147 StreamPath : req .StreamPath ,
108148 Alias : req .Alias ,
149+ Args : queryParams ,
109150 }
110151 var pubId uint32
111152 s .AliasStreams .Add (& aliasInfo )
@@ -125,9 +166,10 @@ func (s *Server) SetStreamAlias(ctx context.Context, req *pb.SetStreamAliasReque
125166 }
126167 // 保存到数据库
127168 if s .DB != nil {
128- s . DB . Create ( & StreamAliasDB {
169+ dbAlias := & StreamAliasDB {
129170 AliasStream : aliasInfo ,
130- })
171+ }
172+ s .DB .Create (dbAlias )
131173 }
132174 s .Info ("add alias" , "alias" , req .Alias , "streamPath" , req .StreamPath , "replace" , ok && canReplace , "pub" , pubId )
133175 }
@@ -143,15 +185,20 @@ func (s *Server) SetStreamAlias(ctx context.Context, req *pb.SetStreamAliasReque
143185 if publisher , hasTarget := s .Streams .Get (req .Alias ); hasTarget { // restore stream
144186 aliasStream .TransferSubscribers (publisher )
145187 } else {
146- var args url.Values
147- for sub := range aliasStream .Publisher .SubscriberRange {
148- if sub .StreamPath == req .Alias {
149- aliasStream .Publisher .RemoveSubscriber (sub )
150- s .Waiting .Wait (sub )
151- args = sub .Args
188+ // 优先使用别名保存的查询参数
189+ args := aliasStream .Args
190+ if len (args ) == 0 {
191+ // 如果没有保存的查询参数,则从订阅者中获取
192+ for sub := range aliasStream .Publisher .SubscriberRange {
193+ if sub .StreamPath == req .Alias {
194+ aliasStream .Publisher .RemoveSubscriber (sub )
195+ s .Waiting .Wait (sub )
196+ args = sub .Args
197+ break
198+ }
152199 }
153200 }
154- if args != nil {
201+ if len ( args ) > 0 {
155202 s .OnSubscribe (req .Alias , args )
156203 }
157204 }
@@ -218,7 +265,21 @@ func (s *Subscriber) processAliasOnStart() (hasInvited bool, done bool) {
218265 done = true
219266 return
220267 } else {
221- server .OnSubscribe (alias .StreamPath , s .Args )
268+ // 合并参数:先使用别名保存的参数,然后用订阅者传入的参数覆盖同名参数
269+ args := make (url.Values )
270+ // 先复制别名保存的参数
271+ if alias .Args != nil {
272+ for k , v := range alias .Args {
273+ args [k ] = append ([]string (nil ), v ... )
274+ }
275+ }
276+ // 用订阅者传入的参数覆盖同名参数
277+ if s .Args != nil {
278+ for k , v := range s .Args {
279+ args [k ] = append ([]string (nil ), v ... )
280+ }
281+ }
282+ server .OnSubscribe (alias .StreamPath , args )
222283 hasInvited = true
223284 }
224285 } else {
@@ -227,12 +288,14 @@ func (s *Subscriber) processAliasOnStart() (hasInvited bool, done bool) {
227288 as := AliasStream {
228289 StreamPath : streamPath ,
229290 Alias : s .StreamPath ,
291+ Args : s .Args ,
230292 }
231293 server .AliasStreams .Set (& as )
232294 if server .DB != nil {
233- server . DB . Where ( "alias = ?" , s . StreamPath ). Assign ( as ). FirstOrCreate ( & StreamAliasDB {
295+ dbAlias := & StreamAliasDB {
234296 AliasStream : as ,
235- })
297+ }
298+ server .DB .Where ("alias = ?" , s .StreamPath ).Save (dbAlias )
236299 }
237300 if publisher , ok := server .Streams .Get (streamPath ); ok {
238301 publisher .AddSubscriber (s )
0 commit comments