├── .gitignore ├── go.mod ├── NOTICE ├── swisstable ├── match_amd64.go ├── match.s ├── bitset.go └── map.go ├── README.md ├── goredis └── main.go ├── buff_writer.go ├── resp.go ├── server.go ├── server_test.go └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *.out 2 | *.test 3 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/jauhararifin/goredis 2 | 3 | go 1.21.0 4 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | This product includes software developed at [dolthub swiss table](https://github.com/dolthub/swiss) under Apache License 2.0. 2 | 3 | -------------------------------------------------------------------------------- /swisstable/match_amd64.go: -------------------------------------------------------------------------------- 1 | // Code generated by command: go run asm.go -out match.s -stubs match_amd64.go. DO NOT EDIT. 2 | 3 | //go:build amd64 4 | 5 | package swisstable 6 | 7 | // MatchMetadata performs a 16-way probe of |metadata| using SSE instructions 8 | // nb: |metadata| must be an aligned pointer 9 | func MatchMetadata(metadata *[16]int8, hash int8) uint16 10 | 11 | 12 | -------------------------------------------------------------------------------- /swisstable/match.s: -------------------------------------------------------------------------------- 1 | // Code generated by command: go run asm.go -out match.s -stubs match_amd64.go. DO NOT EDIT. 2 | 3 | //go:build amd64 4 | 5 | #include "textflag.h" 6 | 7 | // func MatchMetadata(metadata *[16]int8, hash int8) uint16 8 | // Requires: SSE2, SSSE3 9 | TEXT ·MatchMetadata(SB), NOSPLIT, $0-18 10 | MOVQ metadata+0(FP), AX 11 | MOVBLSX hash+8(FP), CX 12 | MOVD CX, X0 13 | PXOR X1, X1 14 | PSHUFB X1, X0 15 | MOVOU (AX), X1 16 | PCMPEQB X1, X0 17 | PMOVMSKB X0, AX 18 | MOVW AX, ret+16(FP) 19 | RET 20 | 21 | 22 | -------------------------------------------------------------------------------- /swisstable/bitset.go: -------------------------------------------------------------------------------- 1 | package swisstable 2 | 3 | import ( 4 | "math/bits" 5 | _ "unsafe" 6 | ) 7 | 8 | const ( 9 | groupSize = 16 10 | maxAvgGroupLoad = 14 11 | ) 12 | 13 | type bitset uint16 14 | 15 | func metaMatchH2(m *metadata, h h2) bitset { 16 | b := MatchMetadata((*[16]int8)(m), int8(h)) 17 | return bitset(b) 18 | } 19 | 20 | func metaMatchEmpty(m *metadata) bitset { 21 | b := MatchMetadata((*[16]int8)(m), empty) 22 | return bitset(b) 23 | } 24 | 25 | func nextMatch(b *bitset) (s uint32) { 26 | s = uint32(bits.TrailingZeros16(uint16(*b))) 27 | *b &= ^(1 << s) // clear bit |s| 28 | return 29 | } 30 | 31 | //go:linkname fastrand runtime.fastrand 32 | func fastrand() uint32 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Golang Tests 3 | 4 | ```bash 5 | # run the tests 6 | go test . -v -coverprofile=cover.out 7 | 8 | # show the coverage 9 | go tool cover -html cover.out 10 | 11 | # run the benchmark tests 12 | go test . -v -bench=BenchmarkRedisSet -benchmem -benchtime=10s -memprofile=mem.out -cpuprofile=cpu.out -run="^$" 13 | 14 | # using count instead of duration: 15 | go test . -v -bench=BenchmarkRedisSet -benchmem -benchtime=20000000x -memprofile=mem.out -cpuprofile=cpu.out -run="^$" 16 | ``` 17 | 18 | ## Golang Escape Analysis 19 | 20 | ```bash 21 | go build -gcflags "-m -l" *.go 22 | ``` 23 | 24 | ## Benchmark 25 | 26 | ```bash 27 | redis-benchmark -h localhost -p 3100 -r 100000000000 -P 1000 -c 50 -t SET,GET 28 | 29 | # with bigger n 30 | redis-benchmark -h localhost -p 3100 -r 100000000000 -P 10000 -c 24 -t SET,GET -q -n 20000000 31 | ``` 32 | -------------------------------------------------------------------------------- /goredis/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log/slog" 5 | "net" 6 | "os" 7 | "os/signal" 8 | 9 | "github.com/jauhararifin/goredis" 10 | ) 11 | 12 | func main() { 13 | logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})) 14 | 15 | address := "0.0.0.0:3100" 16 | logger.Info("starting server", slog.String("address", address)) 17 | 18 | listener, err := net.Listen("tcp", address) 19 | if err != nil { 20 | logger.Error( 21 | "cannot start tcp server", 22 | slog.String("address", address), 23 | slog.String("err", err.Error()), 24 | ) 25 | os.Exit(-1) 26 | } 27 | 28 | server := goredis.NewServer(listener, logger) 29 | go func() { 30 | if err := server.Start(); err != nil { 31 | logger.Error("server error", slog.String("err", err.Error())) 32 | os.Exit(1) 33 | } 34 | }() 35 | 36 | c := make(chan os.Signal, 1) 37 | signal.Notify(c, os.Interrupt) 38 | <-c 39 | 40 | if err := server.Stop(); err != nil { 41 | logger.Error("canot stop server", slog.String("err", err.Error())) 42 | os.Exit(1) 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /buff_writer.go: -------------------------------------------------------------------------------- 1 | package goredis 2 | 3 | import ( 4 | "io" 5 | "sync" 6 | "time" 7 | ) 8 | 9 | type bufferWriter struct { 10 | w io.Writer 11 | cond *sync.Cond 12 | buff []byte 13 | n int 14 | err error 15 | } 16 | 17 | func newBufferWriter(w io.Writer) *bufferWriter { 18 | cond := sync.NewCond(&sync.Mutex{}) 19 | return &bufferWriter{ 20 | w: w, 21 | cond: cond, 22 | buff: make([]byte, 4096), 23 | n: 0, 24 | err: nil, 25 | } 26 | } 27 | 28 | func (b *bufferWriter) Start() error { 29 | // TODO: Handle shutdown logic. 30 | // this implementation doesn't handle shutdown logic. 31 | // Because of that, this implementation leaks memory and 32 | // goroutines. 33 | 34 | b.cond.L.Lock() 35 | for { 36 | // TODO: avoid cycle when there is no available bytes. 37 | // This implementation will periodically try flushing 38 | // the available bytes. When the client is idle, this 39 | // implementation will still loop periodically. 40 | // This is wasting out CPU. To improve this, we can 41 | // use sync.Cond to sleep when there is no available 42 | // byte, and when someone fill the buffer, we can 43 | // wake up. 44 | for b.n == 0 { 45 | b.cond.Wait() 46 | 47 | b.cond.L.Unlock() 48 | time.Sleep(10 * time.Microsecond) 49 | b.cond.L.Lock() 50 | } 51 | 52 | if err := b.flush(); err != nil { 53 | b.cond.L.Unlock() 54 | return err 55 | } 56 | } 57 | } 58 | 59 | func (b *bufferWriter) Write(buff []byte) (int, error) { 60 | b.cond.L.Lock() 61 | defer b.cond.L.Unlock() 62 | 63 | totalWrite := 0 64 | 65 | for len(buff) > len(b.buff)-b.n && b.err == nil { 66 | var n int 67 | if b.n == 0 { 68 | n, b.err = b.w.Write(buff) 69 | } else { 70 | n = copy(b.buff[b.n:], buff) 71 | b.n += n 72 | b.err = b.flush() 73 | } 74 | totalWrite += n 75 | buff = buff[n:] 76 | } 77 | 78 | if b.err != nil { 79 | return totalWrite, b.err 80 | } 81 | 82 | n := copy(b.buff[b.n:], buff) 83 | b.n += n 84 | totalWrite += n 85 | 86 | b.cond.Signal() 87 | return totalWrite, nil 88 | } 89 | 90 | // lock invariant: b.lock should be acquired 91 | func (b *bufferWriter) flush() error { 92 | if b.err != nil { 93 | return b.err 94 | } 95 | if b.n == 0 { 96 | return nil 97 | } 98 | 99 | n, err := b.w.Write(b.buff[:b.n]) 100 | if n < b.n && err == nil { 101 | err = io.ErrShortWrite 102 | } 103 | if err != nil { 104 | if n > 0 && n < b.n { 105 | copy(b.buff[0:b.n-n], b.buff[n:b.n]) 106 | } 107 | b.n -= n 108 | b.err = err 109 | return err 110 | } 111 | 112 | b.n = 0 113 | return nil 114 | } 115 | -------------------------------------------------------------------------------- /resp.go: -------------------------------------------------------------------------------- 1 | package goredis 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "strings" 8 | ) 9 | 10 | type messageReader struct { 11 | r *bufio.Reader 12 | temp []byte 13 | stringBuilder strings.Builder 14 | } 15 | 16 | func newMessageReader(r io.Reader) *messageReader { 17 | return &messageReader{ 18 | r: bufio.NewReader(r), 19 | temp: make([]byte, 4096), 20 | stringBuilder: strings.Builder{}, 21 | } 22 | } 23 | 24 | func (m *messageReader) ReadArrayLen() (length int, err error) { 25 | b, err := m.r.ReadByte() 26 | if err != nil { 27 | return 0, err 28 | } 29 | 30 | if b != '*' { 31 | return 0, fmt.Errorf("malformed input") 32 | } 33 | 34 | length, err = m.readValueLength() 35 | if err != nil { 36 | return 0, err 37 | } 38 | 39 | return length, nil 40 | } 41 | 42 | func (m *messageReader) readValueLength() (int, error) { 43 | b, err := m.r.ReadByte() 44 | if err != nil { 45 | return 0, err 46 | } 47 | 48 | if b < '0' || b > '9' { 49 | return 0, fmt.Errorf("malformed input") 50 | } 51 | result := int(b - '0') 52 | 53 | for b != '\r' { 54 | b, err = m.r.ReadByte() 55 | if err != nil { 56 | return 0, err 57 | } 58 | 59 | if b != '\r' { 60 | if b < '0' || b > '9' { 61 | return 0, fmt.Errorf("malformed input") 62 | } 63 | result = result*10 + int(b-'0') 64 | } 65 | } 66 | 67 | b, err = m.r.ReadByte() 68 | if err != nil { 69 | return 0, err 70 | } 71 | if b != '\n' { 72 | return 0, fmt.Errorf("malformed input") 73 | } 74 | 75 | return result, nil 76 | } 77 | 78 | func (m *messageReader) readUntilCRLF(w io.Writer) error { 79 | b, err := m.r.ReadByte() 80 | if err != nil { 81 | return err 82 | } 83 | 84 | for b != '\r' { 85 | if _, err := w.Write([]byte{b}); err != nil { 86 | return err 87 | } 88 | 89 | b, err = m.r.ReadByte() 90 | if err != nil { 91 | return err 92 | } 93 | } 94 | 95 | b, err = m.r.ReadByte() 96 | if err != nil { 97 | return err 98 | } 99 | if b != '\n' { 100 | return fmt.Errorf("malformed input") 101 | } 102 | 103 | return nil 104 | } 105 | 106 | func (m *messageReader) skipCRLF() error { 107 | b, err := m.r.ReadByte() 108 | if err != nil { 109 | return err 110 | } 111 | if b != '\r' { 112 | return fmt.Errorf("malformed input") 113 | } 114 | 115 | b, err = m.r.ReadByte() 116 | if err != nil { 117 | return err 118 | } 119 | if b != '\n' { 120 | return fmt.Errorf("malformed input") 121 | } 122 | 123 | return nil 124 | } 125 | 126 | func (m *messageReader) ReadString() (string, error) { 127 | b, err := m.r.ReadByte() 128 | if err != nil { 129 | return "", err 130 | } 131 | 132 | if b == '$' { 133 | length, err := m.readValueLength() 134 | if err != nil { 135 | return "", err 136 | } 137 | 138 | m.stringBuilder.Reset() 139 | m.stringBuilder.Grow(length) 140 | if err := m.pipeToWriter(&m.stringBuilder, length); err != nil { 141 | return "", err 142 | } 143 | 144 | s := m.stringBuilder.String() 145 | if err := m.skipCRLF(); err != nil { 146 | return "", err 147 | } 148 | 149 | return s, nil 150 | } else if b == '+' { 151 | m.stringBuilder.Reset() 152 | if err := m.readUntilCRLF(&m.stringBuilder); err != nil { 153 | return "", err 154 | } 155 | return m.stringBuilder.String(), nil 156 | } else { 157 | return "", fmt.Errorf("malformed input") 158 | } 159 | } 160 | 161 | func (m *messageReader) pipeToWriter(w io.Writer, length int) error { 162 | remaining := length 163 | for remaining > 0 { 164 | n := remaining 165 | if n > len(m.temp) { 166 | n = len(m.temp) 167 | } 168 | 169 | if _, err := io.ReadFull(m.r, m.temp[:n]); err != nil { 170 | return err 171 | } 172 | remaining -= n 173 | 174 | if _, err := w.Write(m.temp[:n]); err != nil { 175 | return err 176 | } 177 | } 178 | 179 | return nil 180 | } 181 | -------------------------------------------------------------------------------- /swisstable/map.go: -------------------------------------------------------------------------------- 1 | package swisstable 2 | 3 | import "hash/maphash" 4 | 5 | const ( 6 | maxLoadFactor = float32(maxAvgGroupLoad) / float32(groupSize) 7 | ) 8 | 9 | // Map is an open-addressing hash map 10 | // based on Abseil's flat_hash_map. 11 | type Map struct { 12 | ctrl []metadata 13 | groups []group 14 | resident uint32 15 | dead uint32 16 | limit uint32 17 | } 18 | 19 | // metadata is the h2 metadata array for a group. 20 | // find operations first probe the controls bytes 21 | // to filter candidates before matching keys 22 | type metadata [groupSize]int8 23 | 24 | // group is a group of 16 key-value pairs 25 | type group struct { 26 | keys [groupSize]string 27 | values [groupSize]string 28 | } 29 | 30 | const ( 31 | h1Mask uint64 = 0xffff_ffff_ffff_ff80 32 | h2Mask uint64 = 0x0000_0000_0000_007f 33 | empty int8 = -128 // 0b1000_0000 34 | tombstone int8 = -2 // 0b1111_1110 35 | ) 36 | 37 | // h1 is a 57 bit hash prefix 38 | type h1 uint64 39 | 40 | // h2 is a 7 bit hash suffix 41 | type h2 int8 42 | 43 | // NewMap constructs a Map. 44 | func NewMap(sz uint32) (m *Map) { 45 | groups := numGroups(sz) 46 | m = &Map{ 47 | ctrl: make([]metadata, groups), 48 | groups: make([]group, groups), 49 | limit: groups * maxAvgGroupLoad, 50 | } 51 | for i := range m.ctrl { 52 | m.ctrl[i] = newEmptyMetadata() 53 | } 54 | return 55 | } 56 | 57 | var seed = maphash.MakeSeed() 58 | 59 | func (m *Map) hash(key string) uint64 { 60 | return maphash.String(seed, key) 61 | } 62 | 63 | // Get returns the |value| mapped by |key| if one exists. 64 | func (m *Map) Get(key string) (value string, ok bool) { 65 | hi, lo := splitHash(m.hash(key)) 66 | g := probeStart(hi, len(m.groups)) 67 | for { // inlined find loop 68 | matches := metaMatchH2(&m.ctrl[g], lo) 69 | for matches != 0 { 70 | s := nextMatch(&matches) 71 | if key == m.groups[g].keys[s] { 72 | value, ok = m.groups[g].values[s], true 73 | return 74 | } 75 | } 76 | // |key| is not in group |g|, 77 | // stop probing if we see an empty slot 78 | matches = metaMatchEmpty(&m.ctrl[g]) 79 | if matches != 0 { 80 | ok = false 81 | return 82 | } 83 | g += 1 // linear probing 84 | if g >= uint32(len(m.groups)) { 85 | g = 0 86 | } 87 | } 88 | } 89 | 90 | // Put attempts to insert |key| and |value| 91 | func (m *Map) Put(key, value string) { 92 | if m.resident >= m.limit { 93 | m.rehash(m.nextSize()) 94 | } 95 | hi, lo := splitHash(m.hash(key)) 96 | g := probeStart(hi, len(m.groups)) 97 | for { // inlined find loop 98 | matches := metaMatchH2(&m.ctrl[g], lo) 99 | for matches != 0 { 100 | s := nextMatch(&matches) 101 | if key == m.groups[g].keys[s] { // update 102 | m.groups[g].keys[s] = key 103 | m.groups[g].values[s] = value 104 | return 105 | } 106 | } 107 | // |key| is not in group |g|, 108 | // stop probing if we see an empty slot 109 | matches = metaMatchEmpty(&m.ctrl[g]) 110 | if matches != 0 { // insert 111 | s := nextMatch(&matches) 112 | m.groups[g].keys[s] = key 113 | m.groups[g].values[s] = value 114 | m.ctrl[g][s] = int8(lo) 115 | m.resident++ 116 | return 117 | } 118 | g += 1 // linear probing 119 | if g >= uint32(len(m.groups)) { 120 | g = 0 121 | } 122 | } 123 | } 124 | 125 | // find returns the location of |key| if present, or its insertion location if absent. 126 | // for performance, find is manually inlined into public methods. 127 | func (m *Map) find(key string, hi h1, lo h2) (g, s uint32, ok bool) { 128 | g = probeStart(hi, len(m.groups)) 129 | for { 130 | matches := metaMatchH2(&m.ctrl[g], lo) 131 | for matches != 0 { 132 | s = nextMatch(&matches) 133 | if key == m.groups[g].keys[s] { 134 | return g, s, true 135 | } 136 | } 137 | // |key| is not in group |g|, 138 | // stop probing if we see an empty slot 139 | matches = metaMatchEmpty(&m.ctrl[g]) 140 | if matches != 0 { 141 | s = nextMatch(&matches) 142 | return g, s, false 143 | } 144 | g += 1 // linear probing 145 | if g >= uint32(len(m.groups)) { 146 | g = 0 147 | } 148 | } 149 | } 150 | 151 | func (m *Map) nextSize() (n uint32) { 152 | n = uint32(len(m.groups)) * 2 153 | if m.dead >= (m.resident / 2) { 154 | n = uint32(len(m.groups)) 155 | } 156 | return 157 | } 158 | 159 | func (m *Map) rehash(n uint32) { 160 | groups, ctrl := m.groups, m.ctrl 161 | m.groups = make([]group, n) 162 | m.ctrl = make([]metadata, n) 163 | for i := range m.ctrl { 164 | m.ctrl[i] = newEmptyMetadata() 165 | } 166 | m.limit = n * maxAvgGroupLoad 167 | m.resident, m.dead = 0, 0 168 | for g := range ctrl { 169 | for s := range ctrl[g] { 170 | c := ctrl[g][s] 171 | if c == empty || c == tombstone { 172 | continue 173 | } 174 | m.Put(groups[g].keys[s], groups[g].values[s]) 175 | } 176 | } 177 | } 178 | 179 | func (m *Map) loadFactor() float32 { 180 | slots := float32(len(m.groups) * groupSize) 181 | return float32(m.resident-m.dead) / slots 182 | } 183 | 184 | // numGroups returns the minimum number of groups needed to store |n| elems. 185 | func numGroups(n uint32) (groups uint32) { 186 | groups = (n + maxAvgGroupLoad - 1) / maxAvgGroupLoad 187 | if groups == 0 { 188 | groups = 1 189 | } 190 | return 191 | } 192 | 193 | func newEmptyMetadata() (meta metadata) { 194 | for i := range meta { 195 | meta[i] = empty 196 | } 197 | return 198 | } 199 | 200 | func splitHash(h uint64) (h1, h2) { 201 | return h1((h & h1Mask) >> 7), h2(h & h2Mask) 202 | } 203 | 204 | func probeStart(hi h1, groups int) uint32 { 205 | return fastModN(uint32(hi), uint32(groups)) 206 | } 207 | 208 | // lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ 209 | func fastModN(x, n uint32) uint32 { 210 | return uint32((uint64(x) * uint64(n)) >> 32) 211 | } 212 | 213 | // randIntN returns a random number in the interval [0, n). 214 | func randIntN(n int) uint32 { 215 | return fastModN(fastrand(), uint32(n)) 216 | } 217 | 218 | -------------------------------------------------------------------------------- /server.go: -------------------------------------------------------------------------------- 1 | package goredis 2 | 3 | import ( 4 | "fmt" 5 | "hash/fnv" 6 | "io" 7 | "log/slog" 8 | "net" 9 | "sync" 10 | "sync/atomic" 11 | "unsafe" 12 | 13 | "github.com/jauhararifin/goredis/swisstable" 14 | ) 15 | 16 | const nShard = 1000 17 | 18 | type server struct { 19 | listener net.Listener 20 | logger *slog.Logger 21 | 22 | started atomic.Bool 23 | clients map[int64]net.Conn 24 | lastClientId int64 25 | clientsLock sync.Mutex 26 | shuttingDown bool 27 | 28 | dbLock [nShard]sync.RWMutex 29 | database [nShard]*swisstable.Map 30 | } 31 | 32 | func NewServer(listener net.Listener, logger *slog.Logger) *server { 33 | s := &server{ 34 | listener: listener, 35 | logger: logger, 36 | 37 | started: atomic.Bool{}, 38 | clients: make(map[int64]net.Conn, 100), 39 | lastClientId: 0, 40 | clientsLock: sync.Mutex{}, 41 | shuttingDown: false, 42 | 43 | dbLock: [nShard]sync.RWMutex{}, 44 | database: [nShard]*swisstable.Map{}, 45 | } 46 | 47 | for i := 0; i < nShard; i++ { 48 | s.database[i] = swisstable.NewMap(100) 49 | } 50 | 51 | return s 52 | } 53 | 54 | func (s *server) Start() error { 55 | if !s.started.CompareAndSwap(false, true) { 56 | return fmt.Errorf("server already started") 57 | } 58 | 59 | s.logger.Info("server started") 60 | for { 61 | conn, err := s.listener.Accept() 62 | if err != nil { 63 | s.clientsLock.Lock() 64 | isShuttingDown := s.shuttingDown 65 | s.clientsLock.Unlock() 66 | 67 | if !isShuttingDown { 68 | return err 69 | } 70 | return nil 71 | } 72 | 73 | s.clientsLock.Lock() 74 | s.lastClientId += 1 75 | clientId := s.lastClientId 76 | s.clients[clientId] = conn 77 | s.clientsLock.Unlock() 78 | go s.handleConn(clientId, conn) 79 | } 80 | } 81 | 82 | func (s *server) Stop() error { 83 | s.clientsLock.Lock() 84 | defer s.clientsLock.Unlock() 85 | 86 | if !s.started.Load() { 87 | return fmt.Errorf("server not started yet") 88 | } 89 | if s.shuttingDown { 90 | return fmt.Errorf("already shutting down") 91 | } 92 | s.shuttingDown = true 93 | 94 | for clientId, conn := range s.clients { 95 | s.logger.Info( 96 | "closing client", 97 | slog.Int64("clientId", clientId), 98 | ) 99 | if err := conn.Close(); err != nil { 100 | s.logger.Error( 101 | "cannot close client", 102 | slog.Int64("clientId", clientId), 103 | slog.String("err", err.Error()), 104 | ) 105 | } 106 | } 107 | clear(s.clients) 108 | 109 | if err := s.listener.Close(); err != nil { 110 | s.logger.Error( 111 | "cannot stop listener", 112 | slog.String("err", err.Error()), 113 | ) 114 | } 115 | 116 | return nil 117 | } 118 | 119 | func (s *server) handleConn(clientId int64, conn net.Conn) { 120 | s.logger.Info( 121 | "client connected", 122 | slog.Int64("id", clientId), 123 | slog.String("host", conn.RemoteAddr().String()), 124 | ) 125 | 126 | reader := newMessageReader(conn) 127 | writer := newBufferWriter(conn) 128 | go func() { 129 | // TODO: handle shutdown for the buffered writer. 130 | if err := writer.Start(); err != nil { 131 | s.logger.Error( 132 | "buffered writer error", 133 | slog.Int64("id", clientId), 134 | slog.String("err", err.Error()), 135 | ) 136 | } 137 | }() 138 | 139 | for { 140 | length, err := reader.ReadArrayLen() 141 | if err != nil { 142 | break 143 | } 144 | if length < 1 { 145 | break 146 | } 147 | commandName, err := reader.ReadString() 148 | if err != nil { 149 | break 150 | } 151 | unsafeToUpper(commandName) 152 | 153 | switch commandName { 154 | case "GET": 155 | err = s.handleGetCommand(reader, writer) 156 | case "SET": 157 | err = s.handleSetCommand(reader, writer) 158 | default: 159 | s.logger.Error("unknown command", slog.String("command", commandName), slog.Int64("clientId", clientId)) 160 | _, err = writer.Write([]byte("-ERR unknown command\r\n")) 161 | 162 | for i := 1; i < length; i++ { 163 | if _, err = reader.ReadString(); err != nil { 164 | break 165 | } 166 | } 167 | } 168 | 169 | if err != nil { 170 | s.logger.Error( 171 | "error writing to client", 172 | slog.Int64("clientId", clientId), 173 | slog.String("err", err.Error()), 174 | ) 175 | break 176 | } 177 | } 178 | 179 | s.clientsLock.Lock() 180 | if _, ok := s.clients[clientId]; !ok { 181 | s.clientsLock.Unlock() 182 | return 183 | } 184 | delete(s.clients, clientId) 185 | s.clientsLock.Unlock() 186 | 187 | s.logger.Info("client disconnecting", slog.Int64("clientId", clientId)) 188 | if err := conn.Close(); err != nil { 189 | s.logger.Error( 190 | "cannot close client", 191 | slog.Int64("clientId", clientId), 192 | slog.String("err", err.Error()), 193 | ) 194 | } 195 | } 196 | 197 | func unsafeToUpper(s string) { 198 | bytes := unsafe.Slice(unsafe.StringData(s), len(s)) 199 | 200 | for i := 0; i < len(bytes); i++ { 201 | b := bytes[i] 202 | if b >= 'a' && b <= 'z' { 203 | b = b + 'A' - 'a' 204 | bytes[i] = b 205 | } 206 | } 207 | } 208 | 209 | func (s *server) handleGetCommand(reader *messageReader, conn io.Writer) error { 210 | key, err := reader.ReadString() 211 | if err != nil { 212 | return err 213 | } 214 | 215 | shard := calculateShard(key) 216 | s.dbLock[shard].RLock() 217 | value, ok := s.database[shard].Get(key) 218 | s.dbLock[shard].RUnlock() 219 | 220 | if ok { 221 | resp := fmt.Sprintf("$%d\r\n%s\r\n", len(value), value) 222 | _, err = conn.Write([]byte(resp)) 223 | } else { 224 | _, err = conn.Write([]byte("_\r\n")) 225 | } 226 | return err 227 | } 228 | 229 | func calculateShard(s string) int { 230 | hasher := fnv.New64() 231 | _, _ = hasher.Write([]byte(s)) 232 | hash := hasher.Sum64() 233 | return int(hash % uint64(nShard)) 234 | } 235 | 236 | func (s *server) handleSetCommand(reader *messageReader, conn io.Writer) error { 237 | key, err := reader.ReadString() 238 | if err != nil { 239 | return err 240 | } 241 | 242 | value, err := reader.ReadString() 243 | if err != nil { 244 | return err 245 | } 246 | 247 | shard := calculateShard(key) 248 | s.dbLock[shard].Lock() 249 | s.database[shard].Put(key, value) 250 | s.dbLock[shard].Unlock() 251 | 252 | _, err = conn.Write([]byte("+OK\r\n")) 253 | return err 254 | } 255 | -------------------------------------------------------------------------------- /server_test.go: -------------------------------------------------------------------------------- 1 | package goredis 2 | 3 | import ( 4 | "bytes" 5 | "errors" 6 | "io" 7 | "log/slog" 8 | "math/rand" 9 | "net" 10 | "os" 11 | "runtime/pprof" 12 | "sync/atomic" 13 | "testing" 14 | "time" 15 | ) 16 | 17 | const sockfilePath string = "/tmp/redisexperiment.sock" 18 | 19 | func TestServerBootstrap(t *testing.T) { 20 | timer := time.AfterFunc(5*time.Second, func() { 21 | pprof.Lookup("goroutine").WriteTo(os.Stdout, 1) 22 | t.Errorf("test runs too long, something is wrong") 23 | t.FailNow() 24 | }) 25 | defer timer.Stop() 26 | 27 | _ = os.Remove(sockfilePath) 28 | listener, err := net.Listen("unix", sockfilePath) 29 | if err != nil { 30 | t.Errorf("cannot open sock file: %s", err.Error()) 31 | return 32 | } 33 | 34 | noopLogger := slog.New(slog.NewTextHandler(io.Discard, nil)) 35 | server := NewServer(listener, noopLogger) 36 | 37 | if err = server.Stop(); err == nil { 38 | t.Error("stopping server before it's started should return an error") 39 | return 40 | } 41 | 42 | // Starting the server 10 times. 43 | // By right, only one of them should success, and the rest should return 44 | // an error 45 | n := 10 46 | startErrorChan := make(chan error, n) 47 | for i := 0; i < n; i++ { 48 | go func() { 49 | err := server.Start() 50 | startErrorChan <- err 51 | }() 52 | } 53 | 54 | for i := 0; i < n-1; i++ { 55 | err := <-startErrorChan 56 | if err == nil { 57 | t.Error("there should be N-1 invocation of `Start` that returns error") 58 | return 59 | } 60 | } 61 | 62 | client, err := net.Dial("unix", sockfilePath) 63 | if err != nil { 64 | t.Errorf("cannot open sock file: %s", err.Error()) 65 | return 66 | } 67 | 68 | if _, err := client.Write([]byte("*3\r\n$3\r\nset\r\n$4\r\nkey1\r\n$6\r\nvalue1\r\n")); err != nil { 69 | t.Errorf("cannot send 'SET key1 value1' to server: %s", err.Error()) 70 | return 71 | } 72 | 73 | buff := make([]byte, 4096) 74 | nRead, err := io.ReadFull(client, buff[:5]) 75 | if err != nil { 76 | t.Errorf("cannot read server response: %s", err.Error()) 77 | return 78 | } 79 | 80 | if string(buff[:nRead]) != "+OK\r\n" { 81 | t.Errorf("calling SET should return 'OK' response, but istead it returns '%s'", string(buff[:nRead])) 82 | return 83 | } 84 | 85 | if _, err := client.Write([]byte("*2\r\n$3\r\nget\r\n$4\r\nkey1\r\n")); err != nil { 86 | t.Errorf("cannot send 'GET key1' to server: %s", err.Error()) 87 | return 88 | } 89 | 90 | nRead, err = io.ReadFull(client, buff[:12]) 91 | if err != nil { 92 | t.Errorf("cannot read server response: %s", err.Error()) 93 | return 94 | } 95 | 96 | if string(buff[:nRead]) != "$6\r\nvalue1\r\n" { 97 | t.Errorf("calling GET should return 'value1' response, but istead it returns '%s'", string(buff[:nRead])) 98 | return 99 | } 100 | 101 | // Stopping the server 10 times 102 | // By right, only one of them should success and the rest should return aerrors. 103 | stopErrorChan := make(chan error, n) 104 | for i := 0; i < n; i++ { 105 | go func() { 106 | err := server.Stop() 107 | stopErrorChan <- err 108 | }() 109 | } 110 | 111 | nSuccess := 0 112 | nError := 0 113 | for i := 0; i < n; i++ { 114 | err := <-stopErrorChan 115 | if err != nil { 116 | nError++ 117 | } else { 118 | nSuccess++ 119 | } 120 | } 121 | 122 | if nSuccess != 1 { 123 | t.Error("there should exactly one invocation of `Stop` that succeded") 124 | return 125 | } 126 | if nError != n-1 { 127 | t.Error("there should exactly n-1 invocation of `Stop` that fails") 128 | return 129 | } 130 | 131 | if err = <-startErrorChan; err != nil { 132 | t.Error("after stopped, the first call to `Start` should return nil") 133 | return 134 | } 135 | 136 | if _, err := client.Read(buff); err == nil || !errors.Is(err, io.EOF) { 137 | t.Error("after stop complete, all client should be disconnected") 138 | return 139 | } 140 | } 141 | 142 | // Initial benchmark: 143 | // goos: linux 144 | // goarch: amd64 145 | // pkg: github.com/jauhararifin/goredis 146 | // cpu: AMD Ryzen 9 7900X 12-Core Processor 147 | // BenchmarkRedisSet 148 | // BenchmarkRedisSet-24 9213043 1349 ns/op 741066 ops/sec 593 B/op 42 allocs/op 149 | // 150 | // After adding buffered reader: 151 | // goos: linux 152 | // goarch: amd64 153 | // pkg: github.com/jauhararifin/goredis 154 | // cpu: AMD Ryzen 9 7900X 12-Core Processor 155 | // BenchmarkRedisSet 156 | // BenchmarkRedisSet-24 11445344 928.4 ns/op 1077079 ops/sec 568 B/op 42 allocs/op 157 | // 158 | // After using buffered writer: 159 | // goos: linux 160 | // goarch: amd64 161 | // pkg: github.com/jauhararifin/goredis 162 | // cpu: AMD Ryzen 9 7900X 12-Core Processor 163 | // BenchmarkRedisSet 164 | // BenchmarkRedisSet-24 12226862 929.9 ns/op 1075353 ops/sec 563 B/op 42 allocs/op 165 | // 166 | // After using better buffered writer: 167 | // goos: linux 168 | // goarch: amd64 169 | // pkg: github.com/jauhararifin/goredis 170 | // cpu: AMD Ryzen 9 7900X 12-Core Processor 171 | // BenchmarkRedisSet 172 | // BenchmarkRedisSet-24 13067581 836.2 ns/op 1195954 ops/sec 650 B/op 42 allocs/op 173 | // 174 | // Try sync.Map instead of map[string]string + RWMutex 175 | // goos: linux 176 | // goarch: amd64 177 | // pkg: github.com/jauhararifin/goredis 178 | // cpu: AMD Ryzen 9 7900X 12-Core Processor 179 | // BenchmarkRedisSet 180 | // BenchmarkRedisSet-24 9602935 1243 ns/op 804487 ops/sec 628 B/op 47 allocs/op 181 | // 182 | // Use 1000 sharding: 183 | // Note that starting from here, the benchmark will be run using N=20000000 (20 million ops) instead of 10 seconds. The reason 184 | // is because 10 seconds testing translates into a lot of inserts, which can cause OOM. 185 | // goos: linux 186 | // goarch: amd64 187 | // pkg: github.com/jauhararifin/goredis 188 | // cpu: AMD Ryzen 9 7900X 12-Core Processor 189 | // BenchmarkRedisSet 190 | // BenchmarkRedisSet-24 20000000 175.8 ns/op 5687745 ops/sec 580 B/op 42 allocs/op 191 | // 192 | // Remove allocation by removing debug log 193 | // goos: linux 194 | // goarch: amd64 195 | // pkg: github.com/jauhararifin/goredis 196 | // cpu: AMD Ryzen 9 7900X 12-Core Processor 197 | // BenchmarkRedisSet 198 | // BenchmarkRedisSet-24 20000000 134.3 ns/op 7446129 ops/sec 316 B/op 36 allocs/op 199 | // 200 | // After reducing allocation: 201 | // goos: linux 202 | // goarch: amd64 203 | // pkg: github.com/jauhararifin/goredis 204 | // cpu: AMD Ryzen 9 7900X 12-Core Processor 205 | // BenchmarkRedisSet 206 | // BenchmarkRedisSet-24 20000000 99.85 ns/op 10015194 ops/sec 164 B/op 4 allocs/op 207 | // 208 | // It seems like this version has performance degradation, but actually if we benchmark it using `redis-benchmark`, it can reach 9Mil ops/sec: 209 | // redis-benchmark -h localhost -p 3100 -r 100000000000 -P 10000 -c 24 -t SET,GET -q -n 20000000 210 | // SET: 9985022.00 requests per second, p50=4.207 msec 211 | // GET: 16792612.00 requests per second, p50=6.151 msec 212 | // 213 | // Increasing the test pipeline size into 10000: 214 | // 215 | // goos: linux 216 | // goarch: amd64 217 | // pkg: github.com/jauhararifin/goredis 218 | // cpu: AMD Ryzen 9 7900X 12-Core Processor 219 | // BenchmarkRedisSet 220 | // BenchmarkRedisSet-24 20000000 86.44 ns/op 11568510 ops/sec 165 B/op 4 allocs/op 221 | // 222 | // Using swisstable: 223 | // goos: linux 224 | // goarch: amd64 225 | // pkg: github.com/jauhararifin/goredis 226 | // cpu: AMD Ryzen 9 7900X 12-Core Processor 227 | // BenchmarkRedisSet 228 | // BenchmarkRedisSet-24 20000000 74.30 ns/op 13459236 ops/sec 149 B/op 4 allocs/op 229 | func BenchmarkRedisSet(b *testing.B) { 230 | _ = os.Remove(sockfilePath) 231 | listener, err := net.Listen("unix", sockfilePath) 232 | if err != nil { 233 | b.Errorf("cannot open sock file: %s", err.Error()) 234 | return 235 | } 236 | 237 | noopLogger := slog.New(slog.NewTextHandler(io.Discard, nil)) 238 | server := NewServer(listener, noopLogger) 239 | 240 | go func() { 241 | if err := server.Start(); err != nil { 242 | b.Errorf("cannot start server: %s", err.Error()) 243 | } 244 | }() 245 | 246 | b.ResetTimer() 247 | 248 | id := atomic.Int64{} 249 | b.RunParallel(func(pb *testing.PB) { 250 | client, err := net.Dial("unix", sockfilePath) 251 | if err != nil { 252 | b.Errorf("cannot connect to server: %s", err.Error()) 253 | b.FailNow() 254 | return 255 | } 256 | 257 | randomizer := rand.New(rand.NewSource(id.Add(1))) 258 | 259 | pipelineSize := 10000 260 | 261 | buff := make([]byte, 409600) 262 | writeBuffer := bytes.Buffer{} 263 | count := 0 264 | 265 | for pb.Next() { 266 | writeBuffer.WriteString("*3\r\n$3\r\nset\r\n$12\r\n") 267 | for i := 0; i < 12; i++ { 268 | writeBuffer.WriteByte(byte(randomizer.Int31()%96 + 32)) 269 | } 270 | writeBuffer.WriteString("\r\n$12\r\n") 271 | for i := 0; i < 12; i++ { 272 | writeBuffer.WriteByte(byte(randomizer.Int31()%96 + 32)) 273 | } 274 | writeBuffer.WriteString("\r\n") 275 | count++ 276 | 277 | if count >= pipelineSize { 278 | if _, err := writeBuffer.WriteTo(client); err != nil { 279 | b.Errorf("cannot write to server: %s", err.Error()) 280 | return 281 | } 282 | 283 | if _, err := io.ReadFull(client, buff[:5*count]); err != nil { 284 | b.Errorf("cannot read from server: %s", err.Error()) 285 | return 286 | } 287 | 288 | count = 0 289 | } 290 | } 291 | 292 | if count > 0 { 293 | if _, err := writeBuffer.WriteTo(client); err != nil { 294 | b.Errorf("cannot write to server: %s", err.Error()) 295 | return 296 | } 297 | 298 | if _, err := io.ReadFull(client, buff[:5*count]); err != nil { 299 | b.Errorf("cannot read from server: %s", err.Error()) 300 | return 301 | } 302 | 303 | count = 0 304 | } 305 | 306 | if err := client.Close(); err != nil { 307 | b.Errorf("cannot close client: %s", err.Error()) 308 | return 309 | } 310 | }) 311 | b.StopTimer() 312 | 313 | if err := server.Stop(); err != nil { 314 | b.Errorf("cannot stop server: %s", err.Error()) 315 | return 316 | } 317 | 318 | throughput := float64(b.N) / b.Elapsed().Seconds() 319 | b.ReportMetric(throughput, "ops/sec") 320 | } 321 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------