Skip to content

Commit

Permalink
リトライ処理をまとめる
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexa committed Mar 7, 2024
1 parent 8c1d7d8 commit 414728b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 49 deletions.
18 changes: 5 additions & 13 deletions amazon_transcribe_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"io"
"sync"

"github.com/aws/aws-sdk-go/service/transcribestreamingservice"
zlog "github.com/rs/zerolog/log"
Expand All @@ -23,8 +22,7 @@ type AmazonTranscribeHandler struct {
SampleRate uint32
ChannelCount uint16
LanguageCode string
RetryCount int
mu sync.Mutex
RetryCounter RetryCounter

OnResultFunc func(context.Context, io.WriteCloser, string, string, string, any) error
}
Expand All @@ -39,7 +37,7 @@ func (h *AmazonTranscribeHandlerMaker) New(config Config, channelID, connectionI
SampleRate: sampleRate,
ChannelCount: channelCount,
LanguageCode: languageCode,
RetryCount: 0,
RetryCounter: NewRetryCounter(),
OnResultFunc: onResultFunc.(func(context.Context, io.WriteCloser, string, string, string, any) error),
}
}
Expand Down Expand Up @@ -74,21 +72,15 @@ func (ar *AwsResult) SetMessage(message string) *AwsResult {
}

func (h *AmazonTranscribeHandler) UpdateRetryCount() int {
defer h.mu.Unlock()
h.mu.Lock()
h.RetryCount++
return h.RetryCount
return h.RetryCounter.Update()
}

func (h *AmazonTranscribeHandler) GetRetryCount() int {
return h.RetryCount
return h.RetryCounter.Get()
}

func (h *AmazonTranscribeHandler) ResetRetryCount() int {
defer h.mu.Unlock()
h.mu.Lock()
h.RetryCount = 0
return h.RetryCount
return h.RetryCounter.Reset()
}

func (h *AmazonTranscribeHandler) Handle(ctx context.Context, reader io.Reader) (*io.PipeReader, error) {
Expand Down
17 changes: 5 additions & 12 deletions packet_dump_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"io"
"os"
"sync"
"time"
)

Expand All @@ -21,8 +20,7 @@ type PacketDumpHandler struct {
SampleRate uint32
ChannelCount uint16
LanguageCode string
RetryCount int
mu sync.Mutex
RetryCounter RetryCounter

OnResultFunc func(context.Context, io.WriteCloser, string, string, string, any) error
}
Expand All @@ -37,6 +35,7 @@ func (h *PacketDumpHandlerMaker) New(config Config, channelID, connectionID stri
SampleRate: sampleRate,
ChannelCount: channelCount,
LanguageCode: languageCode,
RetryCounter: NewRetryCounter(),
OnResultFunc: onResultFunc.(func(context.Context, io.WriteCloser, string, string, string, any) error),
}
}
Expand All @@ -52,21 +51,15 @@ type PacketDumpResult struct {
}

func (h *PacketDumpHandler) UpdateRetryCount() int {
defer h.mu.Unlock()
h.mu.Lock()
h.RetryCount++
return h.RetryCount
return h.RetryCounter.Update()
}

func (h *PacketDumpHandler) GetRetryCount() int {
return h.RetryCount
return h.RetryCounter.Get()
}

func (h *PacketDumpHandler) ResetRetryCount() int {
defer h.mu.Unlock()
h.mu.Lock()
h.RetryCount = 0
return h.RetryCount
return h.RetryCounter.Reset()
}

func (h *PacketDumpHandler) Handle(ctx context.Context, reader io.Reader) (*io.PipeReader, error) {
Expand Down
30 changes: 30 additions & 0 deletions service_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"sync"

"golang.org/x/exp/slices"
)
Expand Down Expand Up @@ -59,3 +60,32 @@ func (h *serviceHandlerMakers) GetNames(exclude []string) []string {

return names
}

type RetryCounter struct {
Count int
mu sync.Mutex
}

func NewRetryCounter() RetryCounter {
return RetryCounter{
Count: 0,
}
}

func (r *RetryCounter) Update() int {
defer r.mu.Unlock()
r.mu.Lock()
r.Count++
return r.Count
}

func (r *RetryCounter) Get() int {
return r.Count
}

func (r *RetryCounter) Reset() int {
defer r.mu.Unlock()
r.mu.Lock()
r.Count = 0
return r.Count
}
17 changes: 5 additions & 12 deletions speech_to_text_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"
"strings"
"sync"

zlog "github.com/rs/zerolog/log"

Expand All @@ -26,8 +25,7 @@ type SpeechToTextHandler struct {
SampleRate uint32
ChannelCount uint16
LanguageCode string
RetryCount int
mu sync.Mutex
RetryCounter RetryCounter

OnResultFunc func(context.Context, io.WriteCloser, string, string, string, any) error
}
Expand All @@ -42,6 +40,7 @@ func (h *SpeechToTextHandlerMaker) New(config Config, channelID, connectionID st
SampleRate: sampleRate,
ChannelCount: channelCount,
LanguageCode: languageCode,
RetryCounter: NewRetryCounter(),
OnResultFunc: onResultFunc.(func(context.Context, io.WriteCloser, string, string, string, any) error),
}
}
Expand Down Expand Up @@ -76,21 +75,15 @@ func (gr *GcpResult) SetMessage(message string) *GcpResult {
}

func (h *SpeechToTextHandler) UpdateRetryCount() int {
defer h.mu.Unlock()
h.mu.Lock()
h.RetryCount++
return h.RetryCount
return h.RetryCounter.Update()
}

func (h *SpeechToTextHandler) GetRetryCount() int {
return h.RetryCount
return h.RetryCounter.Get()
}

func (h *SpeechToTextHandler) ResetRetryCount() int {
defer h.mu.Unlock()
h.mu.Lock()
h.RetryCount = 0
return h.RetryCount
return h.RetryCounter.Reset()
}

func (h *SpeechToTextHandler) Handle(ctx context.Context, reader io.Reader) (*io.PipeReader, error) {
Expand Down
17 changes: 5 additions & 12 deletions test_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"sync"

zlog "github.com/rs/zerolog/log"
)
Expand All @@ -22,8 +21,7 @@ type TestHandler struct {
SampleRate uint32
ChannelCount uint16
LanguageCode string
RetryCount int
mu sync.Mutex
RetryCounter RetryCounter

OnResultFunc func(context.Context, io.WriteCloser, string, string, string, any) error
}
Expand All @@ -38,6 +36,7 @@ func (h *TestHandlerMaker) New(config Config, channelID, connectionID string, sa
SampleRate: sampleRate,
ChannelCount: channelCount,
LanguageCode: languageCode,
RetryCounter: NewRetryCounter(),
OnResultFunc: onResultFunc.(func(context.Context, io.WriteCloser, string, string, string, any) error),
}
}
Expand All @@ -58,21 +57,15 @@ func NewTestResult(channelID, message string) TestResult {
}

func (h *TestHandler) UpdateRetryCount() int {
defer h.mu.Unlock()
h.mu.Lock()
h.RetryCount++
return h.RetryCount
return h.RetryCounter.Update()
}

func (h *TestHandler) GetRetryCount() int {
return h.RetryCount
return h.RetryCounter.Get()
}

func (h *TestHandler) ResetRetryCount() int {
defer h.mu.Unlock()
h.mu.Lock()
h.RetryCount = 0
return h.RetryCount
return h.RetryCounter.Reset()
}

func (h *TestHandler) Handle(ctx context.Context, reader io.Reader) (*io.PipeReader, error) {
Expand Down

0 comments on commit 414728b

Please sign in to comment.