Connection pool dial and memoize
Review this Go connection pool that lazily dials and memoizes one connection per host.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.
0:00 of about 24 min
Mark a line and say what kind of problem it is.0 findings
1type Pool struct {
2 mu sync.Mutex
3 conns map[string]*Conn
4}
5
6func (p *Pool) Get(host string) *Conn {
7 p.mu.Lock()
8 c, ok := p.conns[host]
9 p.mu.Unlock()
10 if ok {
11 return c
12 }
13 c = dial(host) // slow I/O, outside the lock
14 p.mu.Lock()
15 p.conns[host] = c
16 p.mu.Unlock()
17 return c
18}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.