CloseIfEmpty closes the room if all participants had left, or it's still empty past timeout
()
| 770 | |
| 771 | // CloseIfEmpty closes the room if all participants had left, or it's still empty past timeout |
| 772 | func (r *Room) CloseIfEmpty() { |
| 773 | r.lock.Lock() |
| 774 | |
| 775 | if r.IsClosed() || r.holds.Load() > 0 { |
| 776 | r.lock.Unlock() |
| 777 | return |
| 778 | } |
| 779 | |
| 780 | for _, p := range r.participants { |
| 781 | if !p.IsDependent() { |
| 782 | r.lock.Unlock() |
| 783 | return |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | var timeout uint32 |
| 788 | var elapsed int64 |
| 789 | var reason string |
| 790 | if r.FirstJoinedAt() > 0 && r.LastLeftAt() > 0 { |
| 791 | elapsed = time.Now().Unix() - r.LastLeftAt() |
| 792 | // need to give time in case participant is reconnecting |
| 793 | timeout = r.protoRoom.DepartureTimeout |
| 794 | reason = "departure timeout" |
| 795 | } else { |
| 796 | elapsed = time.Now().Unix() - r.protoRoom.CreationTime |
| 797 | timeout = r.protoRoom.EmptyTimeout |
| 798 | reason = "empty timeout" |
| 799 | } |
| 800 | r.lock.Unlock() |
| 801 | |
| 802 | if elapsed >= int64(timeout) { |
| 803 | r.Close(types.ParticipantCloseReasonRoomClosed) |
| 804 | r.logger.Infow("closing idle room", "reason", reason) |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | func (r *Room) Close(reason types.ParticipantCloseReason) { |
| 809 | r.lock.Lock() |