File: pool_self.go

package info (click to toggle)
gitlab-agent 16.11.5-1
  • links: PTS, VCS
  • area: contrib
  • in suites: experimental
  • size: 7,072 kB
  • sloc: makefile: 193; sh: 55; ruby: 3
file content (43 lines) | stat: -rw-r--r-- 841 bytes parent folder | download
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
package grpctool

import (
	"context"

	"google.golang.org/grpc"
)

// PoolSelf is a decorator that uses an in-memory connection to dial self rather than going over network.
type PoolSelf struct {
	delegate PoolInterface
	selfURL  string
	conn     selfPoolConn
}

func NewPoolSelf(delegate PoolInterface, selfURL string, selfConn grpc.ClientConnInterface) *PoolSelf {
	return &PoolSelf{
		delegate: delegate,
		selfURL:  selfURL,
		conn: selfPoolConn{
			ClientConnInterface: selfConn,
		},
	}
}

func (p *PoolSelf) Dial(ctx context.Context, targetURL string) (PoolConn, error) {
	if targetURL == p.selfURL {
		return &p.conn, nil
	}
	return p.delegate.Dial(ctx, targetURL)
}

func (p *PoolSelf) Close() error {
	return p.delegate.Close()
}

type selfPoolConn struct {
	grpc.ClientConnInterface
}

func (s *selfPoolConn) Done() {
	// noop
}