0%

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

func callFunctionWithTimeout[R any, T func() R](f T, timeout time.Duration, defaultValue R) R {
var result R
var mu sync.Mutex
done := make(chan bool, 1)

go func() {
result = f()
mu.Lock()
defer mu.Unlock()
done <- true
}()

select {
case <-done:
return result
case <-time.After(timeout):
mu.Lock()
defer mu.Unlock()
return defaultValue
}
}

阅读全文 »

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
public class Tmp {
@Getter
public static enum Goods {
Food(1, "food"),
Car(2, "car");

Goods(int id, String name) {
this.id = id;
this.name = name;
}

int id;
String name;
}

@Data
public static class GoodsDTO {
Goods type;
}

public static void main(String[] args) {
// 假设是其他服务传递来的,这里用 new 模拟
GoodsDTO goodsDTO = new GoodsDTO();
goodsDTO.getType().getName();
}

}

我们的场景是枚举和dto都是服务端提供的包文件,但是如果服务端发生了升级。如果客户端调用的时候出现了未知的枚举,会产生null的错误异常,会阻止程序的正常运行。现在希望是可以有默认值,所以枚举类型需要单独增加一个enum,作为默认值。但是由于服务端在正常情况下不会设置为默认值,而是有可能设置一个客户端不存在,但是服务端存在的正常类型,这时候需要在dto做手脚了,首先enum本身是一个基原类型的组合,所以我们可以在封装dto存储一个基原类型,而枚举的获取是在调用方通进行转化,而转化代码本身本身是可以兼容未知类型的。所以这种情况下的使用是不会出现null的。不会因为某些情况下,服务端版本因为高于客户端版本,导致客户端出现null的错误。

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

public class Tmp {
@Getter
public static enum Goods {
Food(1, "food"),
Car(2, "car"),
Unknown(0, "unknown");

Goods(int id, String name) {
this.id = id;
this.name = name;
}

int id;
String name;

public static Goods getValueOf(int value) {
return Arrays.stream(Goods.values()).filter(v -> v.getId() == value).findFirst().orElse(Unknown);
}
}

@Data
public static class GoodsDTO {
int typeId;

public Goods getType() {
return Goods.getValueOf(this.typeId);
}
}

public static void main(String[] args) {
// 假设是其他服务传递来的,这里用 new 模拟
GoodsDTO goodsDTO = new GoodsDTO();
System.out.println(goodsDTO.getType().getName());;
}

}

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
type JSONStats struct {
Keys []string
Types map[string][]string
}

func parseJSON(prefix string, jsonData []byte, stats *JSONStats) error {
var data interface{}
err := json.Unmarshal(jsonData, &data)
if err != nil {
return err
}
parseJSONObject(prefix, data, stats)
return nil
}

func parseJSONObject(prefix string, jsonObj interface{}, stats *JSONStats) {
switch obj := jsonObj.(type) {
case map[string]interface{}:
for key, value := range obj {
if !slices.Contains(stats.Keys, prefix+key) {
stats.Keys = append(stats.Keys, prefix+key)
}
switch value.(type) {
case []interface{}:
if !slices.Contains(stats.Types[prefix+key], "array") {
stats.Types[prefix+key] = append(stats.Types[prefix+key], "array")
}
parseJSONArray(prefix+key+".[]", value.([]interface{}), stats)
case map[string]interface{}:
if !slices.Contains(stats.Types[prefix+key], "object") {
stats.Types[prefix+key] = append(stats.Types[prefix+key], "object")
}
parseJSONObject(prefix+key+".", value.(map[string]interface{}), stats)
default:
nodeType := fmt.Sprintf("%T", value)
if !slices.Contains(stats.Types[prefix+key], nodeType) {
stats.Types[prefix+key] = append(stats.Types[prefix+key], fmt.Sprintf("%T", value))
}
}
}
}
}

func parseJSONArray(prefix string, jsonArray []interface{}, stats *JSONStats) {
for _, item := range jsonArray {
switch item.(type) {
case []interface{}:
if _, ok := stats.Types[prefix]; !ok {
stats.Keys = append(stats.Keys, prefix)
stats.Types[prefix] = append(stats.Types[prefix], "array")
}
parseJSONArray(prefix+".[]", item.([]interface{}), stats)
case map[string]interface{}:
parseJSONObject(prefix, item.(map[string]interface{}), stats)
default:
if _, ok := stats.Types[prefix]; !ok {
stats.Keys = append(stats.Keys, prefix)
}
nodeType := fmt.Sprintf("%T", item)
if !slices.Contains(stats.Types[prefix], nodeType) {
stats.Types[prefix] = append(stats.Types[prefix], nodeType)
}
}
}
}

About A fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet.

阅读全文 »

i7 8565U 换成 amd 7840hs , 单核提高至原来的2.x倍,多核提高至原来的7.x倍。很多软件的响应已经和原来变得不一样了。