memcache的客户端实现文件是memcache.go,实现了memcached的协议客户端,对于学习golang的客户端网络编程,
memcache.go还是非常值得一看的。奇怪的是vitess的安装文档没有提到要求安装memcached,至于为什么使用memcache,
而不是进程内的cache以节省开销呢?vitess的文档是这样描述的:
Go’s existing mark-and-sweep garbage collector is sub-optimal for systems that use large amounts of
static memory (like caches). In the case of vtocc, this would be the row cache. To alleviate this,
we intend to use memcache for the time being. If the gc ends up addressing this, it should be
fairly trivial to switch to an in-memory row cache
就不翻译了。简单来讲,vitess的作者也认为进程内的cache是更好的方案,由于当前golang的gc实现还不够理想,
所以选择了memcache。另外这里有篇文章分享了从ruby到golang到c的历程也说明go的gc还不够成熟。
豆瓣的一哥们也分享了golang手动管理内存的心得,见这里
希望go team接下来的工作重点之一是修正gc的缺陷(似乎短期内不太现实),对现有gc的问题的解决办法几个:
1. 尽量选择64位操作系统,不要使用32位的(不是治本之道),
2. 大量的对象尽量使用池来管理,redis和memcache是不错的选择,因为这部分内存管理的事情交给它们了
vitess就是这么干的
3. 手动管理也是个不错的选择,参考豆瓣的哥们的文章
gc的问题可以丢一边去了,来看看memcache客户端,还是先把最重要的贴出来,那就是
memcache在vitess里面扮演的什么角色?
答案是Row Cache里面的cache_pool里面的主要组件,见架构图的底部
memcache中有哪些亮点呢?老规矩,先上数据结构
type Connection struct {
conn net.Conn
buffered bufio.ReadWriter
}
既然net.Conn已经实现了Read和Write接口,那么为什么要使用bufio.ReadWriter呢,猜想有2点原因
1. bufio本身的性能可能更高,因为bufferd(未实际测试)
2. 更方便,bufio.ReadWriter还实现了ReadString,ReadLine等一系列接口,可以更便捷的实现memcache的协议
这点在接下来的源码中可以看到,不再详述
重点看几个函数就行,其余的大家看代码即可
//
学习构造Reader和Writer即可,这中方式在golang中非常普遍
func newConnection(nc net.Conn) *Connection {
return &Connection{
conn: nc,
buffered: bufio.ReadWriter{
bufio.NewReader(nc), // 只要实现了Read接口就可以用来构造Reader
bufio.NewWriter(nc), // 只要实现了Write接口就可以用来构造Writer
},
}
}
func (self *Connection) Close() {
self.conn.Close()
self.conn = nil // 用于判断一个connection是否关闭了,见IsClosed函数
}
func (self *Connection) IsClosed() bool {
return self.conn == nil
func newConnection(nc net.Conn) *Connection {
return &Connection{
conn: nc,
buffered: bufio.ReadWriter{
bufio.NewReader(nc), // 只要实现了Read接口就可以用来构造Reader
bufio.NewWriter(nc), // 只要实现了Write接口就可以用来构造Writer
},
}
}
func (self *Connection) Close() {
self.conn.Close()
self.conn = nil // 用于判断一个connection是否关闭了,见IsClosed函数
}
func (self *Connection) IsClosed() bool {
return self.conn == nil
}
接下来欣赏golang漂亮的错误处理机制
以Get函数为例:
func (self *Connection) Get(key
string) (value []
byte, flags uint16, err error) {
//
有名字的函数返回值,以后写个return就自动返回这些啦
defer handleError(&err)
value, flags, _ = self. get( " get ", key)
return
defer handleError(&err)
value, flags, _ = self. get( " get ", key)
return
}
没有看到长长的try和cache了,代码又少了5行左右。有人说了,不就是几个大括号嘛,亲,再给你几个函数看看
func (self *Connection) Gets(key
string
) (value []
byte
, flags uint16, cas uint64, err error) {
defer handleError(&err)
value, flags, cas = self. get( " gets ", key)
return
}
func (self *Connection) Set(key string, flags uint16, timeout uint64, value [] byte) (stored bool, err error) {
defer handleError(&err)
return self.store( " set ", key, flags, timeout, value, 0), nil
}
func (self *Connection) Add(key string, flags uint16, timeout uint64, value [] byte) (stored bool, err error) {
defer handleError(&err)
return self.store( " add ", key, flags, timeout, value, 0), nil
}
func (self *Connection) Replace(key string, flags uint16, timeout uint64, value [] byte) (stored bool, err error) {
defer handleError(&err)
return self.store( " replace ", key, flags, timeout, value, 0), nil
}
func (self *Connection) Append(key string, flags uint16, timeout uint64, value [] byte) (stored bool, err error) {
defer handleError(&err)
return self.store( " append ", key, flags, timeout, value, 0), nil
}
func (self *Connection) Prepend(key string, flags uint16, timeout uint64, value [] byte) (stored bool, err error) {
defer handleError(&err)
return self.store( " prepend ", key, flags, timeout, value, 0), nil
}
func (self *Connection) Cas(key string, flags uint16, timeout uint64, value [] byte, cas uint64) (stored bool, err error) {
defer handleError(&err)
return self.store( " cas ", key, flags, timeout, value, cas), nil
}
func (self *Connection) Delete(key string) (deleted bool, err error) {
defer handleError(&err)
// delete <key> [<time>] [noreply]\r\n
self.writestrings( " delete ", key, " \r\n ")
reply := self.readline()
if strings.Contains(reply, " ERROR ") {
panic(NewMemcacheError( " Server error "))
}
return strings.HasPrefix(reply, " DELETED "), nil
}
defer handleError(&err)
value, flags, cas = self. get( " gets ", key)
return
}
func (self *Connection) Set(key string, flags uint16, timeout uint64, value [] byte) (stored bool, err error) {
defer handleError(&err)
return self.store( " set ", key, flags, timeout, value, 0), nil
}
func (self *Connection) Add(key string, flags uint16, timeout uint64, value [] byte) (stored bool, err error) {
defer handleError(&err)
return self.store( " add ", key, flags, timeout, value, 0), nil
}
func (self *Connection) Replace(key string, flags uint16, timeout uint64, value [] byte) (stored bool, err error) {
defer handleError(&err)
return self.store( " replace ", key, flags, timeout, value, 0), nil
}
func (self *Connection) Append(key string, flags uint16, timeout uint64, value [] byte) (stored bool, err error) {
defer handleError(&err)
return self.store( " append ", key, flags, timeout, value, 0), nil
}
func (self *Connection) Prepend(key string, flags uint16, timeout uint64, value [] byte) (stored bool, err error) {
defer handleError(&err)
return self.store( " prepend ", key, flags, timeout, value, 0), nil
}
func (self *Connection) Cas(key string, flags uint16, timeout uint64, value [] byte, cas uint64) (stored bool, err error) {
defer handleError(&err)
return self.store( " cas ", key, flags, timeout, value, cas), nil
}
func (self *Connection) Delete(key string) (deleted bool, err error) {
defer handleError(&err)
// delete <key> [<time>] [noreply]\r\n
self.writestrings( " delete ", key, " \r\n ")
reply := self.readline()
if strings.Contains(reply, " ERROR ") {
panic(NewMemcacheError( " Server error "))
}
return strings.HasPrefix(reply, " DELETED "), nil
}
乖乖,这下可少了不少代码行,少了50行左右,不能不说这个defer漂亮。
说了半天还没给出handleError的实现呢,赶紧上菜
type MemcacheError
struct {
Message string
}
// 可变参数,这个在golang里面也是个常见的模式
func NewMemcacheError(format string, args ... interface{}) MemcacheError {
return MemcacheError{fmt.Sprintf(format, args...)}
}
func (self MemcacheError) Error() string {
return self.Message
}
func handleError(err *error) {
if x := recover(); x != nil {
*err = x.(MemcacheError) // golang是强类型的,interface需要强制要转化一下,这也是golang的常见模式
}
}
Message string
}
// 可变参数,这个在golang里面也是个常见的模式
func NewMemcacheError(format string, args ... interface{}) MemcacheError {
return MemcacheError{fmt.Sprintf(format, args...)}
}
func (self MemcacheError) Error() string {
return self.Message
}
func handleError(err *error) {
if x := recover(); x != nil {
*err = x.(MemcacheError) // golang是强类型的,interface需要强制要转化一下,这也是golang的常见模式
}