-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcaching_client_creator.go
134 lines (115 loc) · 3.98 KB
/
caching_client_creator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2018 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package githubapp
import (
"fmt"
"github.com/google/go-github/v70/github"
lru "github.com/hashicorp/golang-lru"
"github.com/pkg/errors"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
const (
DefaultCachingClientCapacity = 64
)
// NewDefaultCachingClientCreator returns a ClientCreator using values from the
// configuration or other defaults.
func NewDefaultCachingClientCreator(c Config, opts ...ClientOption) (ClientCreator, error) {
delegate := NewClientCreator(
c.V3APIURL,
c.V4APIURL,
c.App.IntegrationID,
[]byte(c.App.PrivateKey),
opts...,
)
return NewCachingClientCreator(delegate, DefaultCachingClientCapacity)
}
// NewCachingClientCreator returns a ClientCreator that creates a GitHub client for installations of the app specified
// by the provided arguments. It uses an LRU cache of the provided capacity to store clients created for installations
// and returns cached clients when a cache hit exists.
func NewCachingClientCreator(delegate ClientCreator, capacity int) (ClientCreator, error) {
cache, err := lru.New(capacity)
if err != nil {
return nil, errors.Wrapf(err, "failed to create cache")
}
return &cachingClientCreator{
cachedClients: cache,
delegate: delegate,
}, nil
}
type cachingClientCreator struct {
cachedClients *lru.Cache
delegate ClientCreator
}
func (c *cachingClientCreator) NewAppClient() (*github.Client, error) {
// app clients are not cached
return c.delegate.NewAppClient()
}
func (c *cachingClientCreator) NewAppV4Client() (*githubv4.Client, error) {
// app clients are not cached
return c.delegate.NewAppV4Client()
}
func (c *cachingClientCreator) NewInstallationClient(installationID int64) (*github.Client, error) {
// if client is in cache, return it
key := c.toCacheKey("v3", installationID)
val, ok := c.cachedClients.Get(key)
if ok {
if client, ok := val.(*github.Client); ok {
return client, nil
}
}
// otherwise, create and return
client, err := c.delegate.NewInstallationClient(installationID)
if err != nil {
return nil, err
}
c.cachedClients.Add(key, client)
return client, nil
}
func (c *cachingClientCreator) NewInstallationV4Client(installationID int64) (*githubv4.Client, error) {
// if client is in cache, return it
key := c.toCacheKey("v4", installationID)
val, ok := c.cachedClients.Get(key)
if ok {
if client, ok := val.(*githubv4.Client); ok {
return client, nil
}
}
// otherwise, create and return
client, err := c.delegate.NewInstallationV4Client(installationID)
if err != nil {
return nil, err
}
c.cachedClients.Add(key, client)
return client, nil
}
func (c *cachingClientCreator) NewTokenSourceClient(ts oauth2.TokenSource) (*github.Client, error) {
// token clients are not cached
return c.delegate.NewTokenSourceClient(ts)
}
func (c *cachingClientCreator) NewTokenClient(token string) (*github.Client, error) {
// token clients are not cached
return c.delegate.NewTokenClient(token)
}
func (c *cachingClientCreator) NewTokenV4Client(token string) (*githubv4.Client, error) {
// token clients are not cached
return c.delegate.NewTokenV4Client(token)
}
func (c *cachingClientCreator) NewTokenSourceV4Client(ts oauth2.TokenSource) (*githubv4.Client, error) {
// token clients are not cached
return c.delegate.NewTokenSourceV4Client(ts)
}
func (c *cachingClientCreator) toCacheKey(apiVersion string, installationID int64) string {
return fmt.Sprintf("%s:%d", apiVersion, installationID)
}