From 414728bd2d88436efd58ceeb253074c303c84bea Mon Sep 17 00:00:00 2001 From: Yoshida Hiroshi Date: Thu, 7 Mar 2024 17:12:56 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=AA=E3=83=88=E3=83=A9=E3=82=A4=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=82=92=E3=81=BE=E3=81=A8=E3=82=81=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- amazon_transcribe_handler.go | 18 +++++------------- packet_dump_handler.go | 17 +++++------------ service_handler.go | 30 ++++++++++++++++++++++++++++++ speech_to_text_handler.go | 17 +++++------------ test_handler.go | 17 +++++------------ 5 files changed, 50 insertions(+), 49 deletions(-) diff --git a/amazon_transcribe_handler.go b/amazon_transcribe_handler.go index 2c9b27e..789ef3c 100644 --- a/amazon_transcribe_handler.go +++ b/amazon_transcribe_handler.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "io" - "sync" "github.com/aws/aws-sdk-go/service/transcribestreamingservice" zlog "github.com/rs/zerolog/log" @@ -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 } @@ -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), } } @@ -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) { diff --git a/packet_dump_handler.go b/packet_dump_handler.go index dfa315f..6bc8294 100644 --- a/packet_dump_handler.go +++ b/packet_dump_handler.go @@ -5,7 +5,6 @@ import ( "encoding/json" "io" "os" - "sync" "time" ) @@ -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 } @@ -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), } } @@ -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) { diff --git a/service_handler.go b/service_handler.go index 1c198b6..7fedfc7 100644 --- a/service_handler.go +++ b/service_handler.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "sync" "golang.org/x/exp/slices" ) @@ -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 +} diff --git a/speech_to_text_handler.go b/speech_to_text_handler.go index 52d62a9..dce417c 100644 --- a/speech_to_text_handler.go +++ b/speech_to_text_handler.go @@ -7,7 +7,6 @@ import ( "fmt" "io" "strings" - "sync" zlog "github.com/rs/zerolog/log" @@ -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 } @@ -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), } } @@ -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) { diff --git a/test_handler.go b/test_handler.go index 7807665..e4e499a 100644 --- a/test_handler.go +++ b/test_handler.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "sync" zlog "github.com/rs/zerolog/log" ) @@ -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 } @@ -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), } } @@ -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) {