Chromium Code Reviews| Index: milo/common/pubsub.go |
| diff --git a/milo/common/pubsub.go b/milo/common/pubsub.go |
| index 56b79c2b05902c61395af9853b59294f0865550e..814340d15b8f3309cf893f7e868974f2fda6271e 100644 |
| --- a/milo/common/pubsub.go |
| +++ b/milo/common/pubsub.go |
| @@ -6,12 +6,14 @@ import ( |
| "fmt" |
| "net/url" |
| "strings" |
| + "sync" |
| "time" |
| "cloud.google.com/go/pubsub" |
| "golang.org/x/net/context" |
| "github.com/luci/gae/service/info" |
| + luciErrors "github.com/luci/luci-go/common/errors" |
| "github.com/luci/luci-go/common/logging" |
| "github.com/luci/luci-go/milo/api/config" |
| ) |
| @@ -52,6 +54,18 @@ type pubsubClient interface { |
| *pubsub.Subscription, error) |
| } |
| +type pubsubClients struct { |
| + clients map[string]pubsubClient |
| + lock sync.RWMutex |
| +} |
| + |
| +func newClientsBundle() *pubsubClients { |
| + return &pubsubClients{ |
| + clients: map[string]pubsubClient{}, |
| + lock: sync.RWMutex{}, |
|
nodir
2017/07/12 23:06:58
this line is noop, unnecessary
Ryan Tseng
2017/07/13 01:08:26
Oh you're right, removed.
|
| + } |
| +} |
| + |
| // prodPubSubClient is a wrapper around the production pubsub client. |
| type prodPubSubClient struct { |
| ctx context.Context |
| @@ -89,9 +103,16 @@ func (pc *prodPubSubClient) createSubscription(id string, cfg pubsub.Subscriptio |
| } |
| // getPubSubClient extracts a debug PubSub client out of the context. |
| -func getPubSubClient(c context.Context) (pubsubClient, error) { |
| - if client, ok := c.Value(&pubSubClientKey).(pubsubClient); ok { |
| - return client, nil |
| +func getPubSubClient(c context.Context, projectID string) (pubsubClient, error) { |
| + v := c.Value(&pubSubClientKey) |
| + if clients, ok := v.(*pubsubClients); ok { |
| + clients.lock.RLock() |
| + client, ok := clients.clients[projectID] |
| + clients.lock.RUnlock() |
| + if ok { |
| + return client, nil |
| + } |
| + return nil, fmt.Errorf("no pubsub clients installed for " + projectID) |
| } |
| return nil, errors.New("no pubsub clients installed") |
| } |
| @@ -100,35 +121,44 @@ func getPubSubClient(c context.Context) (pubsubClient, error) { |
| // given project ID |
| func withClient(c context.Context, projectID string) (context.Context, error) { |
| if projectID == "" { |
| - return nil, errors.New("missing buildbucket project") |
| + return nil, errors.New("missing project id") |
| } |
| client, err := pubsub.NewClient(c, projectID) |
| if err != nil { |
| return nil, err |
| } |
| - return context.WithValue(c, &pubSubClientKey, &prodPubSubClient{c, client}), nil |
| + var clients *pubsubClients |
| + var ok bool |
| + if clients, ok = c.Value(&pubSubClientKey).(*pubsubClients); !ok { |
| + clients = newClientsBundle() |
| + c = context.WithValue(c, &pubSubClientKey, clients) |
| + } |
| + clients.lock.Lock() |
| + clients.clients[projectID] = &prodPubSubClient{c, client} |
|
nodir
2017/07/12 23:06:58
this may cause a memory leak. Consider:
- there i
Ryan Tseng
2017/07/13 01:08:26
I see, you're right. I used this pattern only bec
Ryan Tseng
2017/07/13 02:42:57
Actually on second thought, withClient() is called
nodir
2017/07/13 05:58:46
indeed, pubsub client is not global.
my concern w
Ryan Tseng
2017/07/13 18:02:30
I think it is very confusing to have multiple ways
nodir
2017/07/13 19:38:39
I understand that you prefer services be in contex
nodir
2017/07/13 21:55:00
another issue with the current patchset is:
c1 :=
|
| + clients.lock.Unlock() |
| + return c, nil |
| } |
| -func getTopic(c context.Context, id string) (*pubsub.Topic, error) { |
| - client, err := getPubSubClient(c) |
| +func getTopic(c context.Context, project, id string) (*pubsub.Topic, error) { |
| + client, err := getPubSubClient(c, project) |
| if err != nil { |
| return nil, err |
| } |
| return client.getTopic(id) |
| } |
| -func getSubscription(c context.Context, id string) (*pubsub.Subscription, error) { |
| - client, err := getPubSubClient(c) |
| +func getSubscription(c context.Context, project, id string) (*pubsub.Subscription, error) { |
| + client, err := getPubSubClient(c, project) |
| if err != nil { |
| return nil, err |
| } |
| return client.getSubscription(id) |
| } |
| -func createSubscription(c context.Context, id string, cfg pubsub.SubscriptionConfig) ( |
| +func createSubscription(c context.Context, project, id string, cfg pubsub.SubscriptionConfig) ( |
| *pubsub.Subscription, error) { |
| - client, err := getPubSubClient(c) |
| + client, err := getPubSubClient(c, project) |
| if err != nil { |
| return nil, err |
| } |
| @@ -140,11 +170,16 @@ func createSubscription(c context.Context, id string, cfg pubsub.SubscriptionCon |
| func EnsurePubSubSubscribed(c context.Context, settings *config.Settings) error { |
| if settings.Buildbucket != nil { |
| // Install the production pubsub client pointing to the buildbucket project |
| - // into the context. |
| + // into the context, so that we can get a reference to the topic. |
| c, err := withClient(c, settings.Buildbucket.Project) |
| if err != nil { |
| return err |
| } |
| + // We also need a client for this project for the subscription. |
| + c, err = withClient(c, info.AppID(c)) |
| + if err != nil { |
| + return err |
| + } |
| return ensureBuildbucketSubscribed(c, settings.Buildbucket.Project) |
| } |
| // TODO(hinoka): Ensure buildbot subscribed. |
| @@ -156,10 +191,10 @@ func EnsurePubSubSubscribed(c context.Context, settings *config.Settings) error |
| func ensureBuildbucketSubscribed(c context.Context, projectID string) error { |
| topicID := "builds" |
| // Check to see if the topic exists first. |
| - topic, err := getTopic(c, topicID) |
| + topic, err := getTopic(c, projectID, topicID) |
| switch err { |
| case errNotExist: |
| - logging.WithError(err).Errorf(c, "%s does not exist", topicID) |
| + luciErrors.Annotate(err, "%s does not exist", topicID).Err() |
|
nodir
2017/07/12 23:06:58
this is noop because the return value of Err() is
Ryan Tseng
2017/07/13 01:08:26
my bad, thanks.
|
| return err |
| case nil: |
| // continue |
| @@ -181,10 +216,8 @@ func ensureBuildbucketSubscribed(c context.Context, projectID string) error { |
| return err |
| } |
| // Now check to see if the subscription already exists. |
| - subID := info.AppID(c) |
| - // Get the pubsub module of our app. We do not want to use info.ModuleHostname() |
| - // because it returns a version pinned hostname instead of the default route. |
| - sub, err := getSubscription(c, subID) |
| + appID := info.AppID(c) |
| + sub, err := getSubscription(c, appID, "buildbucket") |
| switch err { |
| case errNotExist: |
| // continue |
| @@ -195,6 +228,8 @@ func ensureBuildbucketSubscribed(c context.Context, projectID string) error { |
| logging.WithError(err).Errorf(c, "could not check subscription %#v", sub) |
| return err |
| } |
| + // Get the pubsub module of our app. We do not want to use info.ModuleHostname() |
| + // because it returns a version pinned hostname instead of the default route. |
| pubsubModuleHost := "pubsub." + info.DefaultVersionHostname(c) |
| // No subscription exists, attach a new subscription to the existing topic. |
| @@ -208,22 +243,9 @@ func ensureBuildbucketSubscribed(c context.Context, projectID string) error { |
| PushConfig: pubsub.PushConfig{Endpoint: endpointURL.String()}, |
| AckDeadline: time.Minute * 10, |
| } |
| - newSub, err := createSubscription(c, subID, subConfig) |
| + newSub, err := createSubscription(c, appID, "buildbucket", subConfig) |
| if err != nil { |
| - if strings.Contains(err.Error(), "The supplied HTTP URL is not registered") { |
| - registerURL := "https://console.cloud.google.com/apis/credentials/domainverification?project=" + projectID |
| - verifyURL := "https://www.google.com/webmasters/verification/verification?hl=en-GB&siteUrl=http://" + pubsubModuleHost |
| - logging.WithError(err).Errorf( |
| - c, "The domain has to be verified and added.\n\n"+ |
| - "1. Go to %s\n"+ |
| - "2. Verify the domain\n"+ |
| - "3. Go to %s\n"+ |
| - "4. Add %s to allowed domains\n\n", |
| - verifyURL, registerURL, pubsubModuleHost) |
| - } else { |
| - logging.WithError(err).Errorf(c, "could not create subscription %#v", sub) |
| - } |
| - return err |
| + return luciErrors.Annotate(err, "could not create subscription %#v", sub).Err() |
| } |
| // Success! |
| logging.Infof(c, "successfully created subscription %#v", newSub) |