/
main.go
168 lines (149 loc) · 3.85 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// pic_cache project main.go
package main
import (
"encoding/base64"
"errors"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
)
var APP_DIR, CACHE_DIR string
var makingId map[string]*sync.Mutex
var makingIdLock *sync.RWMutex
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
exePath, _ := exec.LookPath(os.Args[0])
APP_DIR, _ := filepath.Split(exePath)
CACHE_DIR = filepath.Join(APP_DIR, "cache")
if err := os.MkdirAll(CACHE_DIR, 0777); err != nil {
log.Fatalln("Create cache dir error!", err)
} else {
log.Println("Cache dir:", CACHE_DIR)
}
makingId = make(map[string]*sync.Mutex)
makingIdLock = &sync.RWMutex{}
http.HandleFunc("/pic", getPic)
http.HandleFunc("/job", getPicJob)
http.ListenAndServe(":2537", nil)
}
//图片代理
func getPic(w http.ResponseWriter, req *http.Request) {
var cacheContent *[]byte
var err error
req.ParseForm()
picUrlcoded := req.Form.Get("url")
if picUrlcoded == "" {
w.Write([]byte("param error"))
return
}
coding := base64.NewEncoding("VPQRXAZabBCDNkYcWMIist5EFLvlmnGHu34wxyz0hSTJKOdefgU6j12opqr978-_")
picUrldecoded, err := coding.DecodeString(picUrlcoded)
if err != nil {
log.Println("param decode error, param coded:", picUrlcoded)
w.WriteHeader(http.StatusBadRequest)
return
}
picUrl := string(picUrldecoded)
picUrl = strings.TrimRight(picUrl, string(0x0)) //rtrim tailing zero
picId := getCacheId(picUrl)
log.Println("requesting pic id:", picId)
if !cacheExist(picId) {
log.Println("cache miss, id:", picId)
if err := makeCache(picUrl); err != nil {
log.Println("make cache error, id:", picId, "error:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
} else {
log.Println("cache hit, pic id:", picId)
}
//读取缓存
cacheContent, err = cacheRead(picId)
if err != nil {
log.Println("cache read error, id:", picId, "error:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(*cacheContent)
log.Println("serve pic done, id:", picId)
}
//任务预处理
func getPicJob(w http.ResponseWriter, req *http.Request) {
req.ParseForm()
for _, url := range req.PostForm["url"] {
go func() {
url = strings.TrimRight(url, string(0x0))
picId := getCacheId(url)
if !cacheExist(picId) {
makeCache(url)
}
}()
}
w.Write([]byte("Job Received!"))
}
//制作缓存
func makeCache(url string) (err error) {
var respBody []byte
var mcLock *sync.Mutex
var ok bool
picId := getCacheId(url)
makingIdLock.RLock() //读写锁
if mcLock, ok = makingId[picId]; !ok {
makingIdLock.RUnlock()
makingIdLock.Lock()
mcLock = &sync.Mutex{}
makingIdLock.Unlock()
} else {
makingIdLock.RUnlock()
}
mcLock.Lock()
defer mcLock.Unlock()
if cacheExist(picId) {
//可能别的goroutine也在做这个id的cache,拿到锁正是别人做完的时候
log.Println("make cache job done by other goroutine")
return nil
}
log.Println("making cache, url:", url)
//请求远端
for i := 0; i < 2; i++ { //最大重试次数
resp, err := http.Get(url)
if err != nil {
log.Println("request remote fail, exiting, url:", url)
return err
}
if resp.StatusCode != 200 {
if resp.StatusCode == 404 { //404 not retry
log.Println("request remote fail, 404")
break
} else {
log.Println("request remote fail, code:", resp.StatusCode, "waiting for retry")
time.Sleep(time.Duration((i+1)*500) * time.Millisecond)
}
} else {
respBody, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close()
log.Println("request remote done, size:", len(respBody), "id:", picId)
break
}
resp.Body.Close()
}
if len(respBody) == 0 {
return errors.New("makeCache empty response, url:" + url)
}
//存入缓存
err = cacheWrite(picId, &respBody)
if err != nil {
log.Println("cache save fail, id:", picId)
return err
} else {
log.Println("cache save succeed, id:", picId)
return nil
}
}