Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Add the panic recover for goroutine and mock RPC code for test #83

Merged
merged 14 commits into from
Aug 25, 2023
Empty file removed cmd/follow/report.txt
Empty file.
2 changes: 1 addition & 1 deletion cmd/follow/service/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// Action Function for the follow/close operation
func (s *FollowService) Action(req *follow.ActionRequest) error {
// 限流
if err := cache.Limit(s.ctx, 200, 1*time.Second); err != nil {
if err := cache.Limit(s.ctx, constants.ActionRate, constants.Interval); err != nil {
return err
}

Expand Down
13 changes: 10 additions & 3 deletions cmd/follow/service/follow_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package service
import (
"errors"
"sync"
"time"

"github.com/cloudwego/kitex/pkg/klog"
"github.com/ozline/tiktok/cmd/follow/dal/cache"
Expand All @@ -12,12 +11,13 @@ import (
"github.com/ozline/tiktok/cmd/follow/rpc"
"github.com/ozline/tiktok/kitex_gen/follow"
"github.com/ozline/tiktok/kitex_gen/user"
"github.com/ozline/tiktok/pkg/constants"
)

// FollowList View the follow list
func (s *FollowService) FollowList(req *follow.FollowListRequest) (*[]*follow.User, error) {
// 限流
if err := cache.Limit(s.ctx, 100, 1*time.Second); err != nil {
if err := cache.Limit(s.ctx, constants.FollowListRate, constants.Interval); err != nil {
return nil, err
}

Expand Down Expand Up @@ -49,7 +49,14 @@ func (s *FollowService) FollowList(req *follow.FollowListRequest) (*[]*follow.Us
for _, id := range *followList {
wg.Add(1)
go func(id int64, req *follow.FollowListRequest, userList *[]*follow.User, wg *sync.WaitGroup, mu *sync.Mutex) {
defer wg.Done()
defer func() {
// 协程内部使用recover捕获可能在调用逻辑中发生的panic
if e := recover(); e != nil {
// 某个服务调用协程报错,在这里打印一些错误日志
klog.Info("recover panic:", e)
}
wg.Done()
}()

user, err := rpc.GetUser(s.ctx, &user.InfoRequest{
UserId: id,
Expand Down
13 changes: 10 additions & 3 deletions cmd/follow/service/follower_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package service
import (
"errors"
"sync"
"time"

"github.com/cloudwego/kitex/pkg/klog"
"github.com/ozline/tiktok/cmd/follow/dal/cache"
Expand All @@ -12,12 +11,13 @@ import (
"github.com/ozline/tiktok/cmd/follow/rpc"
"github.com/ozline/tiktok/kitex_gen/follow"
"github.com/ozline/tiktok/kitex_gen/user"
"github.com/ozline/tiktok/pkg/constants"
)

// FollowerList View fan list
func (s *FollowService) FollowerList(req *follow.FollowerListRequest) (*[]*follow.User, error) {
// 限流
if err := cache.Limit(s.ctx, 100, 1*time.Second); err != nil {
if err := cache.Limit(s.ctx, constants.FollowerListRate, constants.Interval); err != nil {
return nil, err
}

Expand Down Expand Up @@ -49,7 +49,14 @@ func (s *FollowService) FollowerList(req *follow.FollowerListRequest) (*[]*follo
for _, id := range *followerList {
wg.Add(1)
go func(id int64, req *follow.FollowerListRequest, userList *[]*follow.User, wg *sync.WaitGroup, mu *sync.Mutex) {
defer wg.Done()
defer func() {
// 协程内部使用recover捕获可能在调用逻辑中发生的panic
if e := recover(); e != nil {
// 某个服务调用协程报错,在这里打印一些错误日志
klog.Info("recover panic:", e)
}
wg.Done()
}()

user, err := rpc.GetUser(s.ctx, &user.InfoRequest{
UserId: id,
Expand Down
13 changes: 10 additions & 3 deletions cmd/follow/service/friend_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package service
import (
"errors"
"sync"
"time"

"github.com/cloudwego/kitex/pkg/klog"
"github.com/ozline/tiktok/cmd/follow/dal/cache"
Expand All @@ -13,12 +12,13 @@ import (
"github.com/ozline/tiktok/kitex_gen/chat"
"github.com/ozline/tiktok/kitex_gen/follow"
"github.com/ozline/tiktok/kitex_gen/user"
"github.com/ozline/tiktok/pkg/constants"
)

// FriendList Viewing friends list
func (s *FollowService) FriendList(req *follow.FriendListRequest) (*[]*follow.FriendUser, error) {
// 限流
if err := cache.Limit(s.ctx, 100, 1*time.Second); err != nil {
if err := cache.Limit(s.ctx, constants.FriendListRate, constants.Interval); err != nil {
return nil, err
}

Expand Down Expand Up @@ -52,7 +52,14 @@ func (s *FollowService) FriendList(req *follow.FriendListRequest) (*[]*follow.Fr
for _, userID := range *userList {
wg.Add(1)
go func(id int64, req *follow.FriendListRequest, userList *[]*follow.FriendUser, wg *sync.WaitGroup, mu *sync.Mutex) {
defer wg.Done()
defer func() {
// 协程内部使用recover捕获可能在调用逻辑中发生的panic
if e := recover(); e != nil {
// 某个服务调用协程报错,在这里打印一些错误日志
klog.Info("recover panic:", e)
}
wg.Done()
}()

user, err := rpc.GetUser(s.ctx, &user.InfoRequest{
UserId: id,
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
)

require (
bou.ke/monkey v1.0.2
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/alibaba/sentinel-golang v1.0.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down Expand Up @@ -94,7 +95,7 @@ require (
go.uber.org/zap v1.21.0 // indirect
golang.org/x/arch v0.2.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sync v0.1.0
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
Expand Down
7 changes: 7 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@ const (
// follow type
FollowAction = 1
UnFollowAction = 2

// follow limit
Interval = 1 * time.Second
ActionRate = 100
FollowListRate = 200
FollowerListRate = 200
FriendListRate = 200
)
10 changes: 10 additions & 0 deletions test/follow/follow_list_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package main

import (
"context"
"testing"
"time"

"bou.ke/monkey"
"github.com/ozline/tiktok/cmd/follow/rpc"
"github.com/ozline/tiktok/kitex_gen/follow"
"github.com/ozline/tiktok/kitex_gen/user"
)

func testFollowList(t *testing.T) {
monkey.Patch(rpc.GetUser, func(ctx context.Context, req *user.InfoRequest) (*user.User, error) {
return &user.User{Id: touserid}, nil
})

defer monkey.UnpatchAll()

_, err := followService.FollowList(&follow.FollowListRequest{
UserId: id,
Token: token,
Expand Down
9 changes: 9 additions & 0 deletions test/follow/follower_list_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package main

import (
"context"
"testing"
"time"

"bou.ke/monkey"
"github.com/ozline/tiktok/cmd/follow/rpc"
"github.com/ozline/tiktok/kitex_gen/follow"
"github.com/ozline/tiktok/kitex_gen/user"
)

func testFollowerList(t *testing.T) {
monkey.Patch(rpc.GetUser, func(ctx context.Context, req *user.InfoRequest) (*user.User, error) {
return &user.User{Id: touserid}, nil
})

defer monkey.UnpatchAll()
_, err := followService.FollowerList(&follow.FollowerListRequest{
UserId: id,
Token: token,
Expand Down
14 changes: 14 additions & 0 deletions test/follow/friend_list_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package main

import (
"context"
"testing"
"time"

"bou.ke/monkey"
"github.com/ozline/tiktok/cmd/follow/rpc"
"github.com/ozline/tiktok/kitex_gen/chat"
"github.com/ozline/tiktok/kitex_gen/follow"
"github.com/ozline/tiktok/kitex_gen/user"
)

func testFriendList(t *testing.T) {
monkey.Patch(rpc.GetUser, func(ctx context.Context, req *user.InfoRequest) (*user.User, error) {
return &user.User{Id: touserid}, nil
})

monkey.Patch(rpc.GetMessage, func(ctx context.Context, req *chat.MessageListRequest, uid, tid int64) (string, int64, error) {
return "hello", 1, nil
})

defer monkey.UnpatchAll()
_, err := followService.FriendList(&follow.FriendListRequest{
UserId: id,
Token: token,
Expand Down
Loading