Successfully reported this slideshow.

GoでKVSを書けるのか

2,084 views

Published on

Shibuya.pm #12 発表資料です

Published in: Technology
  • Be the first to comment

GoでKVSを書けるのか

  1. 1. Moriyoshi Koizumi
  2. 2. net.Listen("tcp", "127.0.0.1:11211") net.Accept()
  3. 3. go handleConn(sock);
  4. 4. func main() {     if len(os.Args) < 2 {         fmt.Fprintf(os.Stderr, "usage: %s addr:portn", os.Args[0]);         os.Exit(255);     }       l, e := net.Listen("tcp", os.Args[1]);     if e != nil {         error("An error occurred (%s)", e.String());         os.Exit(1);     }       // blocking channel     backend_ch := make(chan *kvsBackendConn);       go backend(backend_ch);       for {         ch, e := l.Accept();         if e != nil {             error("An error occurred (%s)", e.String());             os.Exit(1);         }         go handleConn(ch, backend_ch, false);     }     os.Exit(0) }  
  5. 5. opcode := int(header[1]); key_length := (int(header[2]) << 8) | int(header[3]); extra_length := int(header[4]); data_type := int(header[5]); body_length := (int(header[8]) << 24)                 | (int(header[9]) << 16)                 | (int(header[10]) << 8)                 | int(header[11]);
  6. 6. func backend(ch <-chan *kvsBackendConn) {     kvs := make(map[string] []byte);     for req := range ch {         switch (req.command) {         case 0: // get             value, e := kvs[string(req.key)];             if e {                 req.resp_ch<- (&kvsBackendConn{command:0, value:value})             } else {                 req.resp_ch<- (&kvsBackendConn{command:0, value:nil})             }             break;         case 1: // set             kvs[string(req.key)] = req.value;             break; backend_ch<-         } (&kvsBackendConn{     } command: 0, } key: key, value: nil, resp_ch: recv_ch }); result := <-recv_ch; resp_extra := make([]byte, 4); if result != nil {     resp = buildResponse(opcode, 0, ...) } else {     resp = buildResponse(opcode, 1, ...) }
  7. 7. memslap --test=get --concurrency=4 --binary --verbose --servers=127.0.0.1 --execute-number=100 --initial-load=10000 --tcp-nodelay GOMAXPROCS=1 GOMAXPROCS=2 GOMAXPROCS=3 GOMAXPROCS=4
  8. 8. epoll(2)/ Poll server kqueue(2) Write() FD FD FD FD once.Do(startServer) FD FD FD FD Read() FD FD Read() Write() channel communication
  9. 9. pipe(2) eventfd(2)
  10. 10. moriyoshi

×