memcached-server.scm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ;;; Simple memcached server implementation
  2. ;; Copyright (C) 2012 Free Software Foundation, Inc.
  3. ;; This library is free software; you can redistribute it and/or
  4. ;; modify it under the terms of the GNU Lesser General Public
  5. ;; License as published by the Free Software Foundation; either
  6. ;; version 3 of the License, or (at your option) any later version.
  7. ;;
  8. ;; This library is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;; Lesser General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU Lesser General Public
  14. ;; License along with this library; if not, write to the Free Software
  15. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. ;; 02110-1301 USA
  17. (use-modules (rnrs bytevectors)
  18. (fibers)
  19. (ice-9 binary-ports)
  20. (ice-9 textual-ports)
  21. (ice-9 rdelim)
  22. (ice-9 match))
  23. (define (make-default-socket family addr port)
  24. (let ((sock (socket PF_INET SOCK_STREAM 0)))
  25. (setsockopt sock SOL_SOCKET SO_REUSEADDR 1)
  26. (fcntl sock F_SETFD FD_CLOEXEC)
  27. (bind sock family addr port)
  28. (fcntl sock F_SETFL (logior O_NONBLOCK (fcntl sock F_GETFL)))
  29. sock))
  30. (define (client-error port msg . args)
  31. (close-port port)
  32. (apply error msg args))
  33. (define (parse-int port val)
  34. (let ((num (string->number val)))
  35. (unless (and num (integer? num) (exact? num) (>= num 0))
  36. (client-error port "Expected a non-negative integer: ~s" val))
  37. num))
  38. (define (make-item flags bv)
  39. (vector flags bv))
  40. (define (item-flags item)
  41. (vector-ref item 0))
  42. (define (item-bv item)
  43. (vector-ref item 1))
  44. (define *commands* (make-hash-table))
  45. (define-syntax-rule (define-command (name port store . pat)
  46. body body* ...)
  47. (begin
  48. (define (name port store args)
  49. (match args
  50. (pat body body* ...)
  51. (else
  52. (client-error port "Bad line: ~A ~S" 'name (string-join args " ")))))
  53. (hashq-set! *commands* 'name name)))
  54. (define-command (get port store key* ...)
  55. (let lp ((key* key*))
  56. (match key*
  57. ((key key* ...)
  58. (let ((item (hash-ref store key)))
  59. (when item
  60. (put-string port "VALUE ")
  61. (put-string port key)
  62. (put-char port #\space)
  63. (put-string port (number->string (item-flags item)))
  64. (put-char port #\space)
  65. (put-string port (number->string
  66. (bytevector-length (item-bv item))))
  67. (put-char port #\return)
  68. (put-char port #\newline)
  69. (put-bytevector port (item-bv item))
  70. (put-string port "\r\n"))
  71. (lp key*)))
  72. (()
  73. (put-string port "END\r\n")))))
  74. (define-command (set port store key flags exptime bytes
  75. . (and noreply (or ("noreply") ())))
  76. (let* ((flags (parse-int port flags))
  77. (exptime (parse-int port exptime))
  78. (bytes (parse-int port bytes)))
  79. (let ((bv (get-bytevector-n port bytes)))
  80. (unless (= (bytevector-length bv) bytes)
  81. (client-error port "Tried to read ~A bytes, only read ~A"
  82. bytes (bytevector-length bv)))
  83. (hash-set! store key (make-item flags bv))
  84. (when (eqv? (peek-char port) #\return)
  85. (read-char port))
  86. (when (eqv? (peek-char port) #\newline)
  87. (read-char port)))
  88. (put-string port "STORED\r\n")))
  89. (define (client-loop port addr store)
  90. ;; Disable Nagle's algorithm. We buffer ourselves.
  91. (setsockopt port IPPROTO_TCP TCP_NODELAY 1)
  92. (setvbuf port 'block 1024)
  93. (let loop ()
  94. ;; TODO: Restrict read-line to 512 chars.
  95. (let ((line (read-line port)))
  96. (cond
  97. ((eof-object? line)
  98. (close-port port))
  99. (else
  100. (match (string-split (string-trim-right line) #\space)
  101. ((verb . args)
  102. (let ((proc (hashq-ref *commands* (string->symbol verb))))
  103. (unless proc
  104. (client-error port "Bad command: ~a" verb))
  105. (proc port store args)
  106. (force-output port)
  107. (loop)))
  108. (else (client-error port "Bad command line" line))))))))
  109. (define (socket-loop socket store)
  110. (let loop ()
  111. (match (accept socket SOCK_NONBLOCK)
  112. ((client . addr)
  113. (spawn-fiber
  114. (lambda () (client-loop client addr store))
  115. #:parallel? #t)
  116. (loop)))))
  117. (define* (run-memcached #:key
  118. (host #f)
  119. (family AF_INET)
  120. (addr (if host
  121. (inet-pton family host)
  122. INADDR_LOOPBACK))
  123. (port 11211)
  124. (socket (make-default-socket family addr port)))
  125. (listen socket 1024)
  126. (sigaction SIGPIPE SIG_IGN)
  127. (socket-loop socket (make-hash-table)))
  128. ;; Have to restrict parallelism as long as we are using a
  129. ;; thread-unsafe hash table as the store!
  130. (run-fibers run-memcached #:parallelism 1)