Skip to content
This repository was archived by the owner on Jun 18, 2022. It is now read-only.

Commit cb0d31f

Browse files
Daishan PengCraig Jellick
authored andcommitted
reuse transport connection
1 parent 7feaf17 commit cb0d31f

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

core/storage/plugin_unix.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,49 @@ import (
88
"fmt"
99
"io/ioutil"
1010
"net/http"
11+
"path/filepath"
1112
"strings"
13+
"sync"
1214

1315
"github.com/docker/go-connections/sockets"
1416
"github.com/pkg/errors"
1517
"github.com/rancher/agent/model"
1618
"github.com/rancher/log"
1719
)
1820

21+
var transportMap = &transportStore{
22+
clientMap: make(map[string]*http.Client),
23+
lock: sync.RWMutex{},
24+
}
25+
26+
type transportStore struct {
27+
clientMap map[string]*http.Client
28+
lock sync.RWMutex
29+
}
30+
31+
func (t *transportStore) add(driver string) {
32+
t.lock.Lock()
33+
defer t.lock.Unlock()
34+
if _, ok := t.clientMap[driver]; !ok {
35+
transport := new(http.Transport)
36+
sockets.ConfigureTransport(transport, "unix", filepath.Join(rancherSockDir, driver+".sock"))
37+
t.clientMap[driver] = &http.Client{
38+
Transport: transport,
39+
}
40+
}
41+
}
42+
43+
func (t *transportStore) get(driver string) *http.Client {
44+
t.lock.RLock()
45+
defer t.lock.RUnlock()
46+
return t.clientMap[driver]
47+
}
48+
1949
func CallRancherStorageVolumePlugin(volume model.Volume, action string, payload interface{}) (Response, error) {
20-
transport := new(http.Transport)
21-
sockets.ConfigureTransport(transport, "unix", rancherStorageSockPath(volume))
22-
client := &http.Client{
23-
Transport: transport,
50+
if transportMap.get(volume.Data.Fields.Driver) == nil {
51+
transportMap.add(volume.Data.Fields.Driver)
2452
}
53+
client := transportMap.get(volume.Data.Fields.Driver)
2554
url := fmt.Sprintf("http://volume-plugin/VolumeDriver.%v", action)
2655

2756
bs, err := json.Marshal(payload)

0 commit comments

Comments
 (0)