Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix load source plugins #892

Merged
merged 5 commits into from
Dec 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 23 additions & 41 deletions pkg/source/source_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -164,10 +163,30 @@ func (m *clientManager) UnRegister(scheme string) {
}

func (m *clientManager) GetClient(scheme string) (ResourceClient, bool) {
logger.Debugf("current clients: %#v", m.clients)
m.mu.RLock()
defer m.mu.RUnlock()
client, ok := m.clients[strings.ToLower(scheme)]
return client, ok
scheme = strings.ToLower(scheme)
client, ok := m.clients[scheme]
if ok {
m.mu.RUnlock()
return client, true
}
m.mu.RUnlock()
m.mu.Lock()
client, ok = m.clients[scheme]
if ok {
m.mu.Unlock()
return client, true
}
client, err := LoadPlugin(scheme)
if err != nil {
logger.Errorf("failed to load source plugin for scheme %s: %v", scheme, err)
m.mu.Unlock()
return nil, false
}
m.clients[scheme] = client
m.mu.Unlock()
return client, true
}

func Register(scheme string, resourceClient ResourceClient, adaptor requestAdapter, hooks ...Hook) error {
Expand Down Expand Up @@ -285,40 +304,3 @@ func DownloadWithExpireInfo(request *Request) (io.ReadCloser, *ExpireInfo, error
}
return client.DownloadWithExpireInfo(request)
}

// getSourceClient get a source client from source manager with specified schema.
func (m *clientManager) getSourceClient(rawURL string) (ResourceClient, error) {
logger.Debugf("current clients: %#v", m.clients)
parsedURL, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
m.mu.RLock()
client, ok := m.clients[strings.ToLower(parsedURL.Scheme)]
m.mu.RUnlock()
if !ok || client == nil {
client, err = m.loadSourcePlugin(strings.ToLower(parsedURL.Scheme))
if err == nil && client != nil {
return client, nil
}
return nil, errors.Errorf("can not find client for supporting url %s, clients:%v", rawURL, m.clients)
}
return client, nil
}

func (m *clientManager) loadSourcePlugin(scheme string) (ResourceClient, error) {
m.mu.Lock()
defer m.mu.Unlock()
// double check
client, ok := m.clients[scheme]
if ok {
return client, nil
}

client, err := LoadPlugin(scheme)
if err != nil {
return nil, err
}
m.clients[scheme] = client
return client, nil
}