websocket-based lrcproto server
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

add WithResolver option, send resolvID on init chans

+79 -30
+35 -11
options.go
··· 1 1 package lrcd 2 2 3 3 import ( 4 + "context" 4 5 "errors" 5 6 "io" 6 7 ··· 8 9 ) 9 10 10 11 type options struct { 11 - uri string 12 - secret string 13 - welcome *string 14 - writer *io.Writer 15 - verbose bool 16 - pubChan chan PubEvent 17 - initChan chan lrcpb.Event_Init 18 - mediainitChan chan lrcpb.Event_Mediainit 19 - initialID *uint32 12 + uri string 13 + secret string 14 + welcome *string 15 + writer *io.Writer 16 + verbose bool 17 + pubChan chan PubEvent 18 + initChan chan struct { 19 + lrcpb.Event_Init 20 + *string 21 + } 22 + mediainitChan chan struct { 23 + lrcpb.Event_Mediainit 24 + *string 25 + } 26 + initialID *uint32 27 + resolver func(externalID string, ctx context.Context) *string 20 28 } 21 29 22 30 type Option func(option *options) error 23 31 32 + func WithResolver(f func(externalID string, ctx context.Context) *string) Option { 33 + return func(options *options) error { 34 + if f == nil { 35 + return errors.New("resolver must be non nil") 36 + } 37 + options.resolver = f 38 + return nil 39 + } 40 + } 41 + 24 42 func WithWelcome(welcome string) Option { 25 43 return func(options *options) error { 26 44 options.welcome = &welcome ··· 46 64 } 47 65 } 48 66 49 - func WithInitChannel(initChan chan lrcpb.Event_Init) Option { 67 + func WithInitChannel(initChan chan struct { 68 + lrcpb.Event_Init 69 + *string 70 + }) Option { 50 71 return func(options *options) error { 51 72 if initChan == nil { 52 73 return errors.New("must provide a channel") ··· 56 77 } 57 78 } 58 79 59 - func WithMediainitChannel(mediainitChan chan lrcpb.Event_Mediainit) Option { 80 + func WithMediainitChannel(mediainitChan chan struct { 81 + lrcpb.Event_Mediainit 82 + *string 83 + }) Option { 60 84 return func(options *options) error { 61 85 if mediainitChan == nil { 62 86 return errors.New("must provide a channel")
+44 -19
server.go
··· 16 16 ) 17 17 18 18 type Server struct { 19 - secret string 20 - uri string 21 - eventBus chan clientEvent 22 - ctx context.Context 23 - cancel context.CancelFunc 24 - clients map[*client]bool 25 - clientsMu sync.Mutex 26 - idmapsMu sync.Mutex 27 - idToClient map[uint32]*client 28 - lastID uint32 29 - logger *log.Logger 30 - debugLogger *log.Logger 31 - welcomeEvt []byte 32 - pongEvt []byte 33 - initChan chan lrcpb.Event_Init 34 - mediainitChan chan lrcpb.Event_Mediainit 35 - pubChan chan PubEvent 19 + secret string 20 + uri string 21 + eventBus chan clientEvent 22 + ctx context.Context 23 + cancel context.CancelFunc 24 + clients map[*client]bool 25 + clientsMu sync.Mutex 26 + idmapsMu sync.Mutex 27 + idToClient map[uint32]*client 28 + lastID uint32 29 + logger *log.Logger 30 + debugLogger *log.Logger 31 + welcomeEvt []byte 32 + pongEvt []byte 33 + initChan chan struct { 34 + lrcpb.Event_Init 35 + *string 36 + } 37 + mediainitChan chan struct { 38 + lrcpb.Event_Mediainit 39 + *string 40 + } 41 + resolver func(externalID string, ctx context.Context) *string 42 + pubChan chan PubEvent 36 43 } 37 44 38 45 type PubEvent struct { ··· 53 60 post *string 54 61 nick *string 55 62 externID *string 63 + resolvID *string 64 + rcancel context.CancelFunc 56 65 color *uint32 57 66 } 58 67 ··· 99 108 } 100 109 s.uri = options.uri 101 110 s.secret = options.secret 111 + s.resolver = options.resolver 102 112 103 113 s.clients = make(map[*client]bool) 104 114 s.clientsMu = sync.Mutex{} ··· 360 370 msg.Init.Nonce = nil 361 371 if s.initChan != nil { 362 372 select { 363 - case s.initChan <- *msg: 373 + case s.initChan <- struct { 374 + lrcpb.Event_Init 375 + *string 376 + }{*msg, client.resolvID}: 364 377 default: 365 378 s.log("initchan blocked, closing channel") 366 379 close(s.initChan) ··· 423 436 msg.Mediainit.Nonce = nil 424 437 if s.mediainitChan != nil { 425 438 select { 426 - case s.mediainitChan <- *msg: 439 + case s.mediainitChan <- struct { 440 + lrcpb.Event_Mediainit 441 + *string 442 + }{*msg, client.resolvID}: 427 443 default: 428 444 s.log("initchan blocked, closing channel") 429 445 close(s.mediainitChan) ··· 674 690 if externalId != nil { 675 691 externid := *externalId 676 692 client.externID = &externid 693 + client.rcancel() 694 + if s.resolver != nil { 695 + go func() { 696 + ctx, cancel := context.WithCancel(client.ctx) 697 + client.rcancel = cancel 698 + resolvid := s.resolver(externid, ctx) 699 + client.resolvID = resolvid 700 + }() 701 + } 677 702 } 678 703 color := msg.Set.Color 679 704 if color != nil {