Skip to content

Commit

Permalink
Add error handling mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas7788 committed Oct 25, 2017
1 parent 7a9a5c5 commit 510b0a1
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 42 deletions.
12 changes: 10 additions & 2 deletions src/adhandler/click_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ func ClickHandler(w http.ResponseWriter, r *http.Request) {
return
}
queryString := string(queryStringBytes)
paramMap, _ := url.ParseQuery(queryString)
paramMap, err := url.ParseQuery(queryString)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}

// search_id
var searchId string
Expand All @@ -33,7 +37,11 @@ func ClickHandler(w http.ResponseWriter, r *http.Request) {
// slot_id
var slotId uint32
if slotIds, exist := paramMap["slot_id"]; exist {
tmpInt, _ := strconv.ParseUint(slotIds[0], 10, 32)
tmpInt, err := strconv.ParseUint(slotIds[0], 10, 32)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
slotId = uint32(tmpInt)
}

Expand Down
32 changes: 26 additions & 6 deletions src/adhandler/conversion_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ func ConversionHandler(w http.ResponseWriter, r *http.Request) {
i := r.Form["i"][0]
queryStringBytes, err := base64.StdEncoding.DecodeString(i)
if err != nil {
w.Write([]byte("{\"status\": 1}"))
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
queryString := string(queryStringBytes)
paramMap, _ := url.ParseQuery(queryString)
paramMap, err := url.ParseQuery(queryString)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}

// search_id
var searchId string
Expand All @@ -34,7 +38,11 @@ func ConversionHandler(w http.ResponseWriter, r *http.Request) {
// slot_id
var slotId uint32
if slotIds, exist := paramMap["slot_id"]; exist {
tmpInt, _ := strconv.ParseUint(slotIds[0], 10, 32)
tmpInt, err := strconv.ParseUint(slotIds[0], 10, 32)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
slotId = uint32(tmpInt)
}

Expand All @@ -53,7 +61,11 @@ func ConversionHandler(w http.ResponseWriter, r *http.Request) {
// os
var os uint32
if osString, exist := paramMap["os"]; exist {
tmpInt, _ := strconv.ParseUint(osString[0], 10, 32)
tmpInt, err := strconv.ParseUint(osString[0], 10, 32)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
os = uint32(tmpInt)
}

Expand All @@ -66,14 +78,22 @@ func ConversionHandler(w http.ResponseWriter, r *http.Request) {
// unit_id
var unitId uint32
if unitIdString, exist := paramMap["unit_id"]; exist {
tmpInt, _ := strconv.ParseUint(unitIdString[0], 10, 32)
tmpInt, err := strconv.ParseUint(unitIdString[0], 10, 32)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
unitId = uint32(tmpInt)
}

// creative_id
var creativeId uint32
if creativeIdString, exist := paramMap["creative_id"]; exist {
tmp, _ := strconv.ParseUint(creativeIdString[0], 10, 32)
tmp, err := strconv.ParseUint(creativeIdString[0], 10, 32)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
creativeId = uint32(tmp)
}

Expand Down
32 changes: 26 additions & 6 deletions src/adhandler/impression_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ func ImpressionHandler(w http.ResponseWriter, r *http.Request) {
i := r.Form["i"][0]
queryStringBytes, err := base64.StdEncoding.DecodeString(i)
if err != nil {
w.Write([]byte("{\"status\": 1}"))
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
queryString := string(queryStringBytes)
paramMap, _ := url.ParseQuery(queryString)
paramMap, err := url.ParseQuery(queryString)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}

// search_id
var searchId string
Expand All @@ -34,7 +38,11 @@ func ImpressionHandler(w http.ResponseWriter, r *http.Request) {
// slot_id
var slotId uint32
if slotIds, exist := paramMap["slot_id"]; exist {
tmpInt, _ := strconv.ParseUint(slotIds[0], 10, 32)
tmpInt, err := strconv.ParseUint(slotIds[0], 10, 32)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
slotId = uint32(tmpInt)
}

Expand All @@ -53,7 +61,11 @@ func ImpressionHandler(w http.ResponseWriter, r *http.Request) {
// os
var os uint32
if osString, exist := paramMap["os"]; exist {
tmpInt, _ := strconv.ParseUint(osString[0], 10, 32)
tmpInt, err := strconv.ParseUint(osString[0], 10, 32)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
os = uint32(tmpInt)
}

Expand All @@ -66,14 +78,22 @@ func ImpressionHandler(w http.ResponseWriter, r *http.Request) {
// unit_id
var unitId uint32
if unitIdString, exist := paramMap["unit_id"]; exist {
tmpInt, _ := strconv.ParseUint(unitIdString[0], 10, 32)
tmpInt, err := strconv.ParseUint(unitIdString[0], 10, 32)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
unitId = uint32(tmpInt)
}

// creative_id
var creativeId uint32
if creativeIdString, exist := paramMap["creative_id"]; exist {
tmp, _ := strconv.ParseUint(creativeIdString[0], 10, 32)
tmp, err := strconv.ParseUint(creativeIdString[0], 10, 32)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
creativeId = uint32(tmp)
}

Expand Down
24 changes: 20 additions & 4 deletions src/adhandler/search_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) {
req := new(adserver.Request)
// slot_id
if len(r.Form["slot_id"]) > 0 {
slotId, _ := strconv.ParseUint(r.Form["slot_id"][0], 10, 32)
slotId, err := strconv.ParseUint(r.Form["slot_id"][0], 10, 32)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
req.SlotId = uint32(slotId)
}
// ad_num
if len(r.Form["ad_num"]) > 0 {
reqAdNum, _ := strconv.ParseUint(r.Form["ad_num"][0], 10, 32)
reqAdNum, err := strconv.ParseUint(r.Form["ad_num"][0], 10, 32)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
req.ReqAdNum = uint32(reqAdNum)
}
// ip
Expand All @@ -37,7 +45,11 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) {
}
// os
if len(r.Form["os"]) > 0 {
os, _ := strconv.ParseUint(r.Form["os"][0], 10, 32)
os, err := strconv.ParseUint(r.Form["os"][0], 10, 32)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
req.Os = uint32(os)
}
// os_version
Expand Down Expand Up @@ -135,7 +147,11 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) {
req.SearchId, req.SlotId, req.ReqAdNum, req.Ip, req.DeviceId, req.Os, req.OsVersion,
unitIdsStr, creativeIdsStr,resAdNum))

resBytes, _ := json.Marshal(res)
resBytes, err := json.Marshal(res)
if err != nil {
w.Write([]byte("{\"status\": 1," + "\"error\":" + err.Error() + "}"))
return
}
w.Write(resBytes)
}

Expand Down
39 changes: 29 additions & 10 deletions src/adserver/ad_dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,18 @@ func NewAdDict(dictFileName string) *AdDict {
}

// 初始化之后首次加载广告信息
func (ad *AdDict) Load() {
adDataInfo := ad.loadAdDict()
func (ad *AdDict) Load() error {
adDataInfo, err := ad.loadAdDict()
if err != nil {
return err
}
ad.AdDataArray[ad.CurrentIndex] = adDataInfo
fileStat, _ := os.Stat(ad.FileName)
fileStat, err := os.Stat(ad.FileName)
if err != nil {
return err
}
ad.LastModifiedTime = fileStat.ModTime().Unix()
return nil
}

// 启动定时器,用于定期重新加载广告信息
Expand All @@ -83,7 +90,10 @@ func (ad *AdDict) StartReloadTimer() {
if currentModifiedTime > ad.LastModifiedTime {
AdServerLog.Info(fmt.Sprintf("start reload ad info dict at %s",
t1.Format("2006-01-02 03:04:05")))
adDataInfo := ad.loadAdDict()
adDataInfo,err := ad.loadAdDict()
if err != nil {
continue
}
nextIndex := 1 - ad.CurrentIndex
ad.AdDataArray[nextIndex] = adDataInfo
ad.CurrentIndex = nextIndex
Expand All @@ -98,12 +108,12 @@ func (ad *AdDict) GetCurrentAdData() *AdDataInfo {
return ad.AdDataArray[ad.CurrentIndex]
}

func (ad *AdDict) loadAdDict() *AdDataInfo {
func (ad *AdDict) loadAdDict() (*AdDataInfo, error) {
dictFile, err := os.Open(ad.FileName)
if err != nil {
AdServerLog.Error(fmt.Sprintf(
"open file error, name=%s\n", ad.FileName))
panic(-1)
return nil, err
}
defer dictFile.Close()

Expand All @@ -117,11 +127,20 @@ func (ad *AdDict) loadAdDict() *AdDataInfo {
}
lineString := string(line)
lines := strings.Split(lineString, "\t")
level, _ := strconv.Atoi(lines[0])
level, err := strconv.Atoi(lines[0])
if err != nil {
return nil, err
}
if level == 1 {
// ad unit info
unitId, _ := strconv.ParseUint(lines[1], 10, 32)
creativeId, _ := strconv.ParseUint(lines[2], 10, 32)
unitId, err := strconv.ParseUint(lines[1], 10, 32)
if err != nil {
return nil, err
}
creativeId, err := strconv.ParseUint(lines[2], 10, 32)
if err != nil {
return nil, err
}
adUnit := AdUnitInfo{
UnitId: uint32(unitId),
CreativeId: uint32(creativeId),
Expand Down Expand Up @@ -179,5 +198,5 @@ func (ad *AdDict) loadAdDict() *AdDataInfo {
}
AdServerLog.Info(fmt.Sprintf(
"read ad info file success, lineNum=%d\n", lineNum))
return adDataInfo
return adDataInfo, nil
}
32 changes: 21 additions & 11 deletions src/adserver/location_dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ func init() {
}

// 初始化之后首次加载Ip字典信息
func (ipDict *IpDict) Load() {
ipDataInfo := LoadLocationDict(GlobalConfObject.GeoBlockFileName,
func (ipDict *IpDict) Load() error {
ipDataInfo,err := LoadLocationDict(GlobalConfObject.GeoBlockFileName,
GlobalConfObject.GeoLocationFileName)
if err != nil {
return err
}
ipDict.IpDataArray[ipDict.CurrentIndex] = ipDataInfo
blockFileStat, _ := os.Stat(GlobalConfObject.GeoBlockFileName)
locationFileStat, _ := os.Stat(GlobalConfObject.GeoLocationFileName)
ipDict.BlockLastModifiedTime = blockFileStat.ModTime().Unix()
ipDict.LocationLastModifiedTime = locationFileStat.ModTime().Unix()
return nil
}

func NewIpDataInfo() *IpDataInfo {
Expand All @@ -69,16 +73,19 @@ func NewIpDataInfo() *IpDataInfo {
}
}

func LoadLocationDict(blockFileName, locationFileName string) *IpDataInfo {
func LoadLocationDict(blockFileName, locationFileName string) (*IpDataInfo, error) {
dictFile, err := os.Open(blockFileName)
if err != nil {
AdServerLog.Error(fmt.Sprintf("open file error, name=%s\n", blockFileName))
panic(-1)
return nil, err
}
defer dictFile.Close()

ipDataInfo := NewIpDataInfo()
geoLocationMap := loadGeoLocation(locationFileName)
geoLocationMap, err := loadGeoLocation(locationFileName)
if err != nil {
return nil, err
}
br := bufio.NewReader(dictFile)
for {
line, _, err := br.ReadLine()
Expand Down Expand Up @@ -128,7 +135,7 @@ func LoadLocationDict(blockFileName, locationFileName string) *IpDataInfo {
AdServerLog.Info(fmt.Sprintf(
"location dict size=%d\n", len(ipDataInfo.ipPairs)))

return ipDataInfo
return ipDataInfo, nil
}

// 启动定时器,用于定期重新加载Ip字典信息
Expand All @@ -146,9 +153,12 @@ func (locationDict *IpDict) StartReloadTimer() {
if blockCurrentModifiedTime > locationDict.BlockLastModifiedTime || locationCurrentModifiedTime > locationDict.LocationLastModifiedTime {
AdServerLog.Info(fmt.Sprintf("start reload ad info dict at %s",
t1.Format("2006-01-02 03:04:05")))
LoadLocationDict(
_, err := LoadLocationDict(
GlobalConfObject.GeoBlockFileName,
GlobalConfObject.GeoLocationFileName)
if err != nil {
continue
}
nextIndex := 1 - locationDict.CurrentIndex
locationDict.CurrentIndex = nextIndex
locationDict.BlockLastModifiedTime = blockCurrentModifiedTime
Expand Down Expand Up @@ -185,12 +195,12 @@ func (ipDataInfo *IpDataInfo) SearchLocationByIp(ipString string) *LocationInfo
return nil
}

func loadGeoLocation(fileName string) map[uint64]*GeoLocationInfo {
func loadGeoLocation(fileName string) (map[uint64]*GeoLocationInfo, error) {
dictFile, err := os.Open(fileName)
if err != nil {
AdServerLog.Error(fmt.Sprintf(
"open file error, name=%s\n", fileName))
panic(-1)
"open file error, name=%s\n", fileName))
return nil, err
}
defer dictFile.Close()

Expand Down Expand Up @@ -225,5 +235,5 @@ func loadGeoLocation(fileName string) map[uint64]*GeoLocationInfo {
AdServerLog.Info(fmt.Sprintf(
"load dict success, file=%s, lineNum=%d\n",
fileName, lineNum))
return geoLocationMap
return geoLocationMap, nil
}
Loading

0 comments on commit 510b0a1

Please sign in to comment.