-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Will put message into buffer if client timeout
This should help prevent client message lost when client conn accidentally drop off. Signed-off-by: Yilun <[email protected]>
- Loading branch information
1 parent
a473ad1
commit a7ee196
Showing
5 changed files
with
131 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package server | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type DelayedChan struct { | ||
buffer chan *delayedValue | ||
delay time.Duration | ||
} | ||
|
||
type delayedValue struct { | ||
value interface{} | ||
releaseTime time.Time | ||
} | ||
|
||
func NewDelayedChan(size int, delay time.Duration) *DelayedChan { | ||
buffer := make(chan *delayedValue, size) | ||
return &DelayedChan{ | ||
buffer: buffer, | ||
delay: delay, | ||
} | ||
} | ||
|
||
func (dc *DelayedChan) Push(v interface{}) bool { | ||
dv := &delayedValue{ | ||
value: v, | ||
releaseTime: time.Now().Add(dc.delay), | ||
} | ||
select { | ||
case dc.buffer <- dv: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func (dc *DelayedChan) Pop() (interface{}, bool) { | ||
dv, ok := <-dc.buffer | ||
if !ok { | ||
return nil, false | ||
} | ||
if dv.releaseTime.After(time.Now()) { | ||
time.Sleep(time.Until(dv.releaseTime)) | ||
} | ||
return dv.value, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters