Skip to content

Commit

Permalink
update go
Browse files Browse the repository at this point in the history
  • Loading branch information
szhaomsft committed Dec 28, 2019
1 parent ea4c25c commit 7a43a88
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions Samples-Http/Go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func GetToken(auth string, key string) string {
if err != nil {
log.Printf("Do request failed with error %s\n", err)
} else {
defer response.Body.Close()
data, _ := ioutil.ReadAll(response.Body)
token = string(data)
log.Println("token is", token)
Expand All @@ -35,7 +36,7 @@ func GetToken(auth string, key string) string {
return token
}

func GetAudioBytes(endpoint string, authToken string, ssml string) {
func GetAudioBytes(client *http.Client, endpoint string, authToken string, ssml string) {
start := time.Now()
postData := []byte(ssml)
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(postData))
Expand All @@ -48,10 +49,11 @@ func GetAudioBytes(endpoint string, authToken string, ssml string) {
req.Header.Set("X-Microsoft-OutputFormat", "riff-24khz-16bit-mono-pcm")
req.Header.Set("Authorization", "Bearer "+authToken)
req.Header.Set("User-Agent", "GoClient")
response, err := http.DefaultClient.Do(req)
response, err := client.Do(req)
if err != nil {
log.Printf("Do request failed with error %s\n", err)
} else {
defer response.Body.Close()
if response.StatusCode == 200 {
data, _ := ioutil.ReadAll(response.Body)
log.Println("audio byte len is", len(data))
Expand Down Expand Up @@ -93,8 +95,18 @@ func main() {
}
}()

tr := &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 20,
}

for {
time.Sleep(100 * time.Millisecond)
GetAudioBytes(endpoint, token, "<speak version='1.0' xml:lang='en-us'><voice name='en-US-JessaNeural'>Hello world!</voice></speak>")

client := &http.Client{
Transport: tr,
}

GetAudioBytes(client, endpoint, token, "<speak version='1.0' xml:lang='en-us'><voice name='en-US-JessaNeural'>Hello world!</voice></speak>")
}
}

0 comments on commit 7a43a88

Please sign in to comment.