@@ -14,6 +14,7 @@ import (
14
14
"github.com/gitpod-io/gitpod/common-go/log"
15
15
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
16
16
"github.com/gitpod-io/gitpod/public-api-server/pkg/auth"
17
+ "github.com/gitpod-io/gitpod/public-api-server/pkg/origin"
17
18
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
18
19
19
20
lru "github.com/hashicorp/golang-lru"
@@ -41,14 +42,14 @@ func (p *NoConnectionPool) Get(ctx context.Context, token auth.Token) (gitpod.AP
41
42
opts := gitpod.ConnectToServerOpts {
42
43
Context : ctx ,
43
44
Log : logger ,
45
+ Origin : origin .FromContext (ctx ),
44
46
}
45
47
46
48
switch token .Type {
47
49
case auth .AccessTokenType :
48
50
opts .Token = token .Value
49
51
case auth .CookieTokenType :
50
52
opts .Cookie = token .Value
51
- opts .Origin = token .OriginHeader
52
53
default :
53
54
return nil , errors .New ("unknown token type" )
54
55
}
@@ -83,11 +84,12 @@ func NewConnectionPool(address *url.URL, poolSize int) (*ConnectionPool, error)
83
84
84
85
return & ConnectionPool {
85
86
cache : cache ,
86
- connConstructor : func (token auth.Token ) (gitpod.APIInterface , error ) {
87
+ connConstructor : func (ctx context. Context , token auth.Token ) (gitpod.APIInterface , error ) {
87
88
opts := gitpod.ConnectToServerOpts {
88
89
// We're using Background context as we want the connection to persist beyond the lifecycle of a single request
89
90
Context : context .Background (),
90
91
Log : log .Log ,
92
+ Origin : origin .FromContext (ctx ),
91
93
CloseHandler : func (_ error ) {
92
94
cache .Remove (token )
93
95
connectionPoolSize .Dec ()
@@ -99,7 +101,6 @@ func NewConnectionPool(address *url.URL, poolSize int) (*ConnectionPool, error)
99
101
opts .Token = token .Value
100
102
case auth .CookieTokenType :
101
103
opts .Cookie = token .Value
102
- opts .Origin = token .OriginHeader
103
104
default :
104
105
return nil , errors .New ("unknown token type" )
105
106
}
@@ -120,15 +121,23 @@ func NewConnectionPool(address *url.URL, poolSize int) (*ConnectionPool, error)
120
121
121
122
}
122
123
124
+ type conenctionPoolCacheKey struct {
125
+ token auth.Token
126
+ origin string
127
+ }
128
+
123
129
type ConnectionPool struct {
124
- connConstructor func (token auth.Token ) (gitpod.APIInterface , error )
130
+ connConstructor func (context. Context , auth.Token ) (gitpod.APIInterface , error )
125
131
126
132
// cache stores token to connection mapping
127
133
cache * lru.Cache
128
134
}
129
135
130
136
func (p * ConnectionPool ) Get (ctx context.Context , token auth.Token ) (gitpod.APIInterface , error ) {
131
- cached , found := p .cache .Get (token )
137
+ origin := origin .FromContext (ctx )
138
+
139
+ cacheKey := p .cacheKey (token , origin )
140
+ cached , found := p .cache .Get (cacheKey )
132
141
reportCacheOutcome (found )
133
142
if found {
134
143
conn , ok := cached .(* gitpod.APIoverJSONRPC )
@@ -137,17 +146,24 @@ func (p *ConnectionPool) Get(ctx context.Context, token auth.Token) (gitpod.APII
137
146
}
138
147
}
139
148
140
- conn , err := p .connConstructor (token )
149
+ conn , err := p .connConstructor (ctx , token )
141
150
if err != nil {
142
151
return nil , fmt .Errorf ("failed to create new connection to server: %w" , err )
143
152
}
144
153
145
- p .cache .Add (token , conn )
154
+ p .cache .Add (cacheKey , conn )
146
155
connectionPoolSize .Inc ()
147
156
148
157
return conn , nil
149
158
}
150
159
160
+ func (p * ConnectionPool ) cacheKey (token auth.Token , origin string ) conenctionPoolCacheKey {
161
+ return conenctionPoolCacheKey {
162
+ token : token ,
163
+ origin : origin ,
164
+ }
165
+ }
166
+
151
167
func getEndpointBasedOnToken (t auth.Token , u * url.URL ) (string , error ) {
152
168
switch t .Type {
153
169
case auth .AccessTokenType :
0 commit comments