File tree Expand file tree Collapse file tree 1 file changed +22
-2
lines changed Expand file tree Collapse file tree 1 file changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,10 @@ import (
12
12
"gopkg.in/edn.v1"
13
13
)
14
14
15
+ const (
16
+ closeDelay = 30 * time .Second
17
+ )
18
+
15
19
func init () {
16
20
rand .Seed (time .Now ().UTC ().UnixNano ())
17
21
}
@@ -59,9 +63,25 @@ func (rs *Rooms) release(game, room string) {
59
63
}
60
64
rm .count -= 1
61
65
if rm .count <= 0 {
62
- delete (rs .rooms , key )
63
- rm .close ()
66
+ go rs .maybeClose (key )
67
+ }
68
+ }
69
+
70
+ // maybeClose closes an empty room after a delay,
71
+ // assuming it is still empty (or accidentally again empty).
72
+ func (rs * Rooms ) maybeClose (key [2 ]string ) {
73
+ time .Sleep (closeDelay )
74
+ rs .mu .Lock ()
75
+ defer rs .mu .Unlock ()
76
+ rm := rs .rooms [key ]
77
+ if rm == nil {
78
+ return
79
+ }
80
+ if rm .count > 0 {
81
+ return
64
82
}
83
+ delete (rs .rooms , key )
84
+ rm .close ()
65
85
}
66
86
67
87
type Room struct {
You can’t perform that action at this time.
0 commit comments