Skip to content

Commit 901c079

Browse files
committed
global http client
1 parent 3f14f6e commit 901c079

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

devices.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ func (d device) deviceSend(interval int, ch chan<- string) {
7777
}
7878

7979
func (d device) createDevice() (*http.Response, error) {
80-
client := &http.Client{
81-
Timeout: time.Second * 10,
82-
}
8380

8481
reqBody, _ := json.Marshal(devicebody{DeviceID: d.Name})
8582

@@ -98,9 +95,6 @@ func (d device) createDevice() (*http.Response, error) {
9895
// get device in IoT Hub
9996
// return 200 if device is found, 404 if not
10097
func (d device) getDevice() (*http.Response, error) {
101-
client := &http.Client{
102-
Timeout: time.Second * 10,
103-
}
10498

10599
req, _ := http.NewRequest("GET", "https://"+Conf.IoTHubs[d.IoTHub]+"/devices/"+d.Name+"?api-version=2016-02-03", nil)
106100

@@ -115,9 +109,6 @@ func (d device) getDevice() (*http.Response, error) {
115109
}
116110

117111
func (d device) sendData(message devicemessage) (*http.Response, error) {
118-
client := &http.Client{
119-
Timeout: time.Second * 10,
120-
}
121112

122113
reqBody, _ := json.Marshal(message)
123114

@@ -135,9 +126,6 @@ func (d device) sendData(message devicemessage) (*http.Response, error) {
135126
}
136127

137128
func (d device) deleteDevice() (*http.Response, error) {
138-
client := &http.Client{
139-
Timeout: time.Second * 10,
140-
}
141129

142130
//fmt.Println("https://" + Conf.IoTHubs[d.IoTHub] + "/devices/" + d.Name + "?api-version=2016-11-14")
143131

main.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package main
22

3-
import "flag"
4-
import "fmt"
5-
import "os"
6-
import "runtime"
3+
import (
4+
"flag"
5+
"fmt"
6+
"net/http"
7+
"os"
8+
"runtime"
9+
"time"
10+
)
711

812
// define a flag -r to remove all devices
913
var removeDevices = flag.Bool("r", false, "Remove devices from IoT Hub")
1014

15+
// http client
16+
var client *http.Client
17+
1118
func main() {
1219
// logical processors for goroutines
1320
runtime.GOMAXPROCS(runtime.NumCPU())
@@ -16,6 +23,11 @@ func main() {
1623
// in this case goroutines will communicate with main() which is also a goroutine
1724
ch := make(chan string)
1825

26+
// http client
27+
client = &http.Client{
28+
Timeout: time.Second * 10,
29+
}
30+
1931
// parse flags
2032
flag.Parse()
2133

0 commit comments

Comments
 (0)