MCPcopy
hub / github.com/redis/go-redis / Get

Method Get

internal/pool/pool_sticky.go:68–97  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

66}
67
68func (p *StickyConnPool) Get(ctx context.Context) (*Conn, error) {
69 // In worst case this races with Close which is not a very common operation.
70 for i := 0; i < 1000; i++ {
71 switch atomic.LoadUint32(&p.state) {
72 case stateDefault:
73 cn, err := p.pool.Get(ctx)
74 if err != nil {
75 return nil, err
76 }
77 if atomic.CompareAndSwapUint32(&p.state, stateDefault, stateInited) {
78 return cn, nil
79 }
80 p.pool.Remove(ctx, cn, ErrClosed)
81 case stateInited:
82 if err := p.badConnError(); err != nil {
83 return nil, err
84 }
85 cn, ok := <-p.ch
86 if !ok {
87 return nil, ErrClosed
88 }
89 return cn, nil
90 case stateClosed:
91 return nil, ErrClosed
92 default:
93 panic("not reached")
94 }
95 }
96 return nil, fmt.Errorf("redis: StickyConnPool.Get: infinite loop")
97}
98
99func (p *StickyConnPool) Put(ctx context.Context, cn *Conn) {
100 defer func() {

Callers

nothing calls this directly

Calls 3

badConnErrorMethod · 0.95
GetMethod · 0.65
RemoveMethod · 0.65

Tested by

no test coverage detected