Increase default channel capacity to 100, and allow it to be set in RunOptions

This commit is contained in:
Sam
2019-09-17 22:37:13 -04:00
parent a11d16faa0
commit 500a965df2
2 changed files with 7 additions and 1 deletions

View File

@ -52,8 +52,13 @@ func getNewMessages(k *Keybase, c chan<- ChatAPI, execOptions []string) {
// Run runs `keybase chat api-listen`, and passes incoming messages to the message handler func // Run runs `keybase chat api-listen`, and passes incoming messages to the message handler func
func (k *Keybase) Run(handler func(ChatAPI), options ...RunOptions) { func (k *Keybase) Run(handler func(ChatAPI), options ...RunOptions) {
var heartbeatFreq int64 var heartbeatFreq int64
var channelCapacity = 100
runOptions := make([]string, 0) runOptions := make([]string, 0)
if len(options) > 0 { if len(options) > 0 {
if options[0].Capacity > 0 {
channelCapacity = options[0].Capacity
}
if options[0].Heartbeat > 0 { if options[0].Heartbeat > 0 {
heartbeatFreq = options[0].Heartbeat heartbeatFreq = options[0].Heartbeat
} }
@ -76,7 +81,7 @@ func (k *Keybase) Run(handler func(ChatAPI), options ...RunOptions) {
runOptions = append(runOptions, createFilterString(options[0].FilterChannel)) runOptions = append(runOptions, createFilterString(options[0].FilterChannel))
} }
} }
c := make(chan ChatAPI, 50) c := make(chan ChatAPI, channelCapacity)
defer close(c) defer close(c)
if heartbeatFreq > 0 { if heartbeatFreq > 0 {
go heartbeat(c, time.Duration(heartbeatFreq)*time.Minute) go heartbeat(c, time.Duration(heartbeatFreq)*time.Minute)

View File

@ -2,6 +2,7 @@ package keybase
// RunOptions holds a set of options to be passed to Run // RunOptions holds a set of options to be passed to Run
type RunOptions struct { type RunOptions struct {
Capacity int // Channel capacity for the buffered channel that holds messages. Defaults to 100 if not set
Heartbeat int64 // Send a heartbeat through the channel every X minutes (0 = off) Heartbeat int64 // Send a heartbeat through the channel every X minutes (0 = off)
Local bool // Subscribe to local messages Local bool // Subscribe to local messages
HideExploding bool // Ignore exploding messages HideExploding bool // Ignore exploding messages