-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fa4cd50
Showing
7 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package bucket | ||
|
||
func New() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |