getClient is used to get a cached or new client
()
| 51 | |
| 52 | // getClient is used to get a cached or new client |
| 53 | func (c *conn) getClient() (*streamClient, error) { |
| 54 | // Check for cached client |
| 55 | c.clientLock.Lock() |
| 56 | front := c.clients.Front() |
| 57 | if front != nil { |
| 58 | c.clients.Remove(front) |
| 59 | } |
| 60 | c.clientLock.Unlock() |
| 61 | if front != nil { |
| 62 | return front.Value.(*streamClient), nil |
| 63 | } |
| 64 | |
| 65 | // Open a new session |
| 66 | stream, err := c.session.Open() |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | |
| 71 | // Create a client codec |
| 72 | codec := msgpackrpc.NewCodec(false, false, stream) |
| 73 | |
| 74 | // Return a new stream client |
| 75 | sc := &streamClient{ |
| 76 | stream: stream, |
| 77 | codec: codec, |
| 78 | } |
| 79 | return sc, nil |
| 80 | } |
| 81 | |
| 82 | // returnStream is used when done with a stream |
| 83 | // to allow re-use by a future RPC |