@@ -14,6 +14,7 @@ import (
1414 "github.com/gitpod-io/gitpod/common-go/log"
1515 gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
1616 "github.com/gitpod-io/gitpod/public-api-server/pkg/auth"
17+ "github.com/gitpod-io/gitpod/public-api-server/pkg/origin"
1718 "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
1819
1920 lru "github.com/hashicorp/golang-lru"
@@ -41,14 +42,14 @@ func (p *NoConnectionPool) Get(ctx context.Context, token auth.Token) (gitpod.AP
4142 opts := gitpod.ConnectToServerOpts {
4243 Context : ctx ,
4344 Log : logger ,
45+ Origin : origin .FromContext (ctx ),
4446 }
4547
4648 switch token .Type {
4749 case auth .AccessTokenType :
4850 opts .Token = token .Value
4951 case auth .CookieTokenType :
5052 opts .Cookie = token .Value
51- opts .Origin = token .OriginHeader
5253 default :
5354 return nil , errors .New ("unknown token type" )
5455 }
@@ -83,7 +84,7 @@ func NewConnectionPool(address *url.URL, poolSize int) (*ConnectionPool, error)
8384
8485 return & ConnectionPool {
8586 cache : cache ,
86- connConstructor : func (token auth.Token ) (gitpod.APIInterface , error ) {
87+ connConstructor : func (ctx context. Context , token auth.Token ) (gitpod.APIInterface , error ) {
8788 opts := gitpod.ConnectToServerOpts {
8889 // We're using Background context as we want the connection to persist beyond the lifecycle of a single request
8990 Context : context .Background (),
@@ -99,7 +100,6 @@ func NewConnectionPool(address *url.URL, poolSize int) (*ConnectionPool, error)
99100 opts .Token = token .Value
100101 case auth .CookieTokenType :
101102 opts .Cookie = token .Value
102- opts .Origin = token .OriginHeader
103103 default :
104104 return nil , errors .New ("unknown token type" )
105105 }
@@ -120,15 +120,23 @@ func NewConnectionPool(address *url.URL, poolSize int) (*ConnectionPool, error)
120120
121121}
122122
123+ type conenctionPoolCacheKey struct {
124+ token auth.Token
125+ origin string
126+ }
127+
123128type ConnectionPool struct {
124- connConstructor func (token auth.Token ) (gitpod.APIInterface , error )
129+ connConstructor func (context. Context , auth.Token ) (gitpod.APIInterface , error )
125130
126131 // cache stores token to connection mapping
127132 cache * lru.Cache
128133}
129134
130135func (p * ConnectionPool ) Get (ctx context.Context , token auth.Token ) (gitpod.APIInterface , error ) {
131- cached , found := p .cache .Get (token )
136+ origin := origin .FromContext (ctx )
137+
138+ cacheKey := p .cacheKey (token , origin )
139+ cached , found := p .cache .Get (cacheKey )
132140 reportCacheOutcome (found )
133141 if found {
134142 conn , ok := cached .(* gitpod.APIoverJSONRPC )
@@ -137,17 +145,24 @@ func (p *ConnectionPool) Get(ctx context.Context, token auth.Token) (gitpod.APII
137145 }
138146 }
139147
140- conn , err := p .connConstructor (token )
148+ conn , err := p .connConstructor (ctx , token )
141149 if err != nil {
142150 return nil , fmt .Errorf ("failed to create new connection to server: %w" , err )
143151 }
144152
145- p .cache .Add (token , conn )
153+ p .cache .Add (cacheKey , conn )
146154 connectionPoolSize .Inc ()
147155
148156 return conn , nil
149157}
150158
159+ func (p * ConnectionPool ) cacheKey (token auth.Token , origin string ) conenctionPoolCacheKey {
160+ return conenctionPoolCacheKey {
161+ token : token ,
162+ origin : origin ,
163+ }
164+ }
165+
151166func getEndpointBasedOnToken (t auth.Token , u * url.URL ) (string , error ) {
152167 switch t .Type {
153168 case auth .AccessTokenType :
0 commit comments