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

Commit

Permalink
reuse transport connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Daishan Peng authored and Craig Jellick committed Jul 9, 2018
1 parent 7feaf17 commit cb0d31f
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions core/storage/plugin_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,49 @@ import (
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"sync"

"github.com/docker/go-connections/sockets"
"github.com/pkg/errors"
"github.com/rancher/agent/model"
"github.com/rancher/log"
)

var transportMap = &transportStore{
clientMap: make(map[string]*http.Client),
lock: sync.RWMutex{},
}

type transportStore struct {
clientMap map[string]*http.Client
lock sync.RWMutex
}

func (t *transportStore) add(driver string) {
t.lock.Lock()
defer t.lock.Unlock()
if _, ok := t.clientMap[driver]; !ok {
transport := new(http.Transport)
sockets.ConfigureTransport(transport, "unix", filepath.Join(rancherSockDir, driver+".sock"))
t.clientMap[driver] = &http.Client{
Transport: transport,
}
}
}

func (t *transportStore) get(driver string) *http.Client {
t.lock.RLock()
defer t.lock.RUnlock()
return t.clientMap[driver]
}

func CallRancherStorageVolumePlugin(volume model.Volume, action string, payload interface{}) (Response, error) {
transport := new(http.Transport)
sockets.ConfigureTransport(transport, "unix", rancherStorageSockPath(volume))
client := &http.Client{
Transport: transport,
if transportMap.get(volume.Data.Fields.Driver) == nil {
transportMap.add(volume.Data.Fields.Driver)
}
client := transportMap.get(volume.Data.Fields.Driver)
url := fmt.Sprintf("http://volume-plugin/VolumeDriver.%v", action)

bs, err := json.Marshal(payload)
Expand Down

0 comments on commit cb0d31f

Please sign in to comment.