diff --git a/revid/revid.go b/revid/revid.go index acdcbbba..0f21044f 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -212,7 +212,7 @@ func (r *Revid) reset(config Config) error { // encoder to revid's encoder slice, and give this encoder the mtsSenders // as a destination. if len(mtsSenders) != 0 { - ms := newMultiSender(mtsSenders) + ms := multiSender(mtsSenders) if err != nil { return err } @@ -224,7 +224,7 @@ func (r *Revid) reset(config Config) error { // encoder to revid's encoder slice, and give this encoder the flvSenders // as a destination. if len(flvSenders) != 0 { - ms := newMultiSender(flvSenders) + ms := multiSender(flvSenders) e, err := flv.NewEncoder(ms, true, true, int(r.config.FrameRate)) if err != nil { return err diff --git a/revid/revid_test.go b/revid/revid_test.go index e81bdcf6..d7c7dfd9 100644 --- a/revid/revid_test.go +++ b/revid/revid_test.go @@ -229,7 +229,7 @@ func TestResetEncoderSenderSetup(t *testing.T) { // Now check that this encoder has correct number of destinations (senders). ms := e.GetDst() - senders := ms.(*multiSender).senders + senders := []loadSender(ms.(multiSender)) got = len(senders) want = len(test.encoders[idx].destinations) if got != want { diff --git a/revid/senders.go b/revid/senders.go index 76f22556..5f88c345 100644 --- a/revid/senders.go +++ b/revid/senders.go @@ -52,38 +52,23 @@ type Sender interface { send(d []byte) error } -// multiSender allows for the sending through multi loadSenders using a single -// call to multiSender.Write. -type multiSender struct { - senders []loadSender -} +// multiSender implements io.Writer. It provides the capacity to send to multiple +// senders from a single Write call. +type multiSender []loadSender -// newMultiSender returns a pointer to a new multiSender. active is a function -// to indicate the state of the multiSenders owner i.e. whether it is running -// or not. -func newMultiSender(senders []loadSender) *multiSender { - s := &multiSender{ - senders: senders, - } - return s -} - -// Write implements io.Writer. The written slice will be sent to each loadSender -// in multiSender.senders as long as s.active() is true. If a send fails, and -// s.retry is true, the send will be tried again. -func (s *multiSender) Write(d []byte) (int, error) { - for _, sender := range s.senders { +// Write implements io.Writer. This will call load (with the passed slice), send +// and release on all senders of multiSender. +func (s multiSender) Write(d []byte) (int, error) { + for _, sender := range s { sender.load(d) err := sender.send() if err != nil { sender.handleSendFail(err) } } - - for _, sender := range s.senders { + for _, sender := range s { sender.release() } - return len(d), nil } diff --git a/revid/senders_test.go b/revid/senders_test.go index 0ae222d5..c3432975 100644 --- a/revid/senders_test.go +++ b/revid/senders_test.go @@ -321,7 +321,7 @@ func TestMultiSenderWrite(t *testing.T) { newDummyLoadSender(false, false), newDummyLoadSender(false, false), } - ms := newMultiSender(senders) + ms := multiSender(senders) // Perform some multiSender writes. const noOfWrites = 5 @@ -331,7 +331,7 @@ func TestMultiSenderWrite(t *testing.T) { // Check that the senders got the data correctly from the writes. for i := byte(0); i < noOfWrites; i++ { - for j, dest := range ms.senders { + for j, dest := range []loadSender(ms) { got := dest.(*dummyLoadSender).buf[i][0] if got != i { t.Errorf("Did not get expected result for sender: %v. \nGot: %v\nWant: %v\n", j, got, i) @@ -349,7 +349,7 @@ func TestMultiSenderFailNoRetry(t *testing.T) { newDummyLoadSender(false, false), } - ms := newMultiSender(senders) + ms := multiSender(senders) // We will perform two writes. We expect the second write not to be complete, // i.e. the senders should not send anything on this write. @@ -357,7 +357,7 @@ func TestMultiSenderFailNoRetry(t *testing.T) { // Make second sender fail a send. const failedSenderIdx = 1 - failedSender := ms.senders[failedSenderIdx].(*dummyLoadSender) + failedSender := []loadSender(ms)[failedSenderIdx].(*dummyLoadSender) failedSender.failOnSend = true ms.Write([]byte{0x01}) @@ -371,7 +371,7 @@ func TestMultiSenderFailNoRetry(t *testing.T) { ms.Write([]byte{0x02}) // Check number of slices sent for each sender and also check data. - for i, sender := range ms.senders { + for i, sender := range []loadSender(ms) { // First check number of slices sent for each sender. wantLen := 3 if i == failedSenderIdx {