Skip to content

Commit

Permalink
refactor: handle numbers of channel
Browse files Browse the repository at this point in the history
  • Loading branch information
totanvix committed Jun 28, 2023
1 parent 0b7330f commit 470c5b5
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions utils/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ type rChannel struct {
err error
}

func NewBot(hookData structs.HookData) *Bot {
ch := make(chan rChannel, 2)
const numberOfCh = 2

func NewBot(hookData structs.HookData) *Bot {
ch := make(chan rChannel, numberOfCh)
return &Bot{HookData: hookData, rCh: ch}
}

Expand All @@ -44,16 +45,20 @@ func (b Bot) ResolveHook() error {
go b.resolveCallbackCommand()
}

for {
for i := 0; i < numberOfCh; i++ {
select {
case r, ok := <-b.rCh:
if !ok || r.err != nil {
case r := <-b.rCh:
if r.err != nil {
return r.err
}
case <-time.After(10 * time.Second):
return errors.New("Timeout")
}
}

close(b.rCh)

return nil
}

func (b Bot) setTypingAction() {
Expand Down Expand Up @@ -87,7 +92,6 @@ func (b Bot) resolveCommand() error {

defer func() {
b.rCh <- rChannel{err: err}
close(b.rCh)
}()

data := b.getTelegramData()
Expand Down

0 comments on commit 470c5b5

Please sign in to comment.