Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tnngo committed May 8, 2019
0 parents commit fa4cd50
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 0 deletions.
Empty file added README.md
Empty file.
5 changes: 5 additions & 0 deletions bucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bucket

func New() {

}
65 changes: 65 additions & 0 deletions queue/queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package queue

// element 元素
type element struct {
next *element
value interface{}
}

// queue 为令牌桶算法专有队列, 因此设计为有界队列
type queue struct {
length, currentLen int

// 入队游标
enqCursor int

// 出队游标
deqCursor int

e *element
}

// New 初始化队列
func New(length int) *queue {
if length < 1 {
panic("有界队列的长度不可以小于1")
}
return &queue{
length: length,
currentLen: 0,
enqCursor: 0,
deqCursor: 0,
}
}

// addElement 添加元素
func (e *element) addElement(v interface{}) {
for ; e != nil; e = e.next {
if e.next == nil {
e.next = &element{
next: nil,
value: v,
}
break
}
}
}

func (q *queue) Put(v interface{}) {
if q.Len() == 0 {
q.e = &element{
next: nil,
value: v,
}
q.currentLen = 1
return
}
q.e.addElement(v)
q.enqCursor++
q.currentLen++
}

// Len 返回队列的长度
func (q *queue) Len() int {
return q.currentLen
}
95 changes: 95 additions & 0 deletions queue/queue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package queue

import (
"reflect"
"testing"
)

func TestNew(t *testing.T) {
type args struct {
length int
}
tests := []struct {
name string
args args
want *queue
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := New(tt.args.length); !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() = %v, want %v", got, tt.want)
}
})
}
}

func Test_element_addElement(t *testing.T) {
type fields struct {
next *element
value interface{}
}
type args struct {
v interface{}
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &element{
next: tt.fields.next,
value: tt.fields.value,
}
e.addElement(tt.args.v)
})
}
}

func Test_queue_Put(t *testing.T) {
t.Run("queue.Put", func(t *testing.T) {
q := New(100)
q.Put("111")
q.Put("222")
q.Put("333")
t.Logf("%+v", q.e)
t.Logf("%+v", q.e.next)
t.Logf("%+v", q.e.next.next)
})
}

func Test_queue_Len(t *testing.T) {
type fields struct {
length int
currentLen int
enqCursor int
deqCursor int
e *element
}
tests := []struct {
name string
fields fields
want int
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
q := &queue{
length: tt.fields.length,
currentLen: tt.fields.currentLen,
enqCursor: tt.fields.enqCursor,
deqCursor: tt.fields.deqCursor,
e: tt.fields.e,
}
if got := q.Len(); got != tt.want {
t.Errorf("queue.Len() = %v, want %v", got, tt.want)
}
})
}
}
18 changes: 18 additions & 0 deletions server_test/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"log"
"net"
)

func main() {
conn, err := net.Dial("tcp", ":8080")
defer conn.Close()
if err != nil {
log.Println(err)
return
}

// to server
conn.Write([]byte("Hello Golang"))
}
31 changes: 31 additions & 0 deletions server_test/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"log"
"net"
)

func handle(conn net.Conn) {
defer conn.Close()
buf := make([]byte, 1024)
conn.Read(buf)

log.Println(string(buf))
}

func main() {
ln, err := net.Listen("tcp", ":8080")
if err != nil {
log.Panic(err)
}

for {
conn, err := ln.Accept()
if err != nil {
log.Println(err)
conn.Close()
continue
}
go handle(conn)
}
}
18 changes: 18 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package bucket

import (
"math/rand"
"time"
)

var seed = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

func token(length int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
v := make([]uint8, 0)
for i := 0; i < length && length > 0; i++ {
rm := r.Int(71)
v = append(v, seed[rm])
}
return string(v)
}

0 comments on commit fa4cd50

Please sign in to comment.