Author: ephuizi

  • vps测试

    最方便,便宜是搬瓦工(bandwagonhost),就是速度不快,有时候连不上稳定性不够。平常查资料,用谷歌也还可以的。

    采用的方案是:搬瓦工 洛杉矶机房 openvz  年付费19.9vps ,Shadowsocks server +kcptun

    openvz vps bandwagonhost

    最近推出了kvm cn2的线路,买了一个测试了一下:Shadowsocks server +bbr 测试了一下感觉。也是和上面的速度差不多(同样是洛杉矶机房)。没有感觉到网上别人测试得到的成倍提升。可能分配vps的时候运气比较差。

     

    宇宙最快:Google云平台建的虚拟机。

    开启了bbr+shadowsocks速度是搬瓦工的十倍。机房选择的是asia-east1-a,网上说这是台湾机房。

    客户端测试延迟是25ms,上YouTube:

    google大法好

    就是Google收费太贵了,费用由vm和流量两部分构成:

    vm采用的按时收费,我选的最低配置:1共享vcpu +0.6GB内存+标准永久10GB磁盘:有效的每小时费率为 $0.007(每月 730 小时)

    网络流量计费:有效的每小时费率为 $0.023。(google 产品流量不计费,例如:上youtube的流量不计费)

    还好Google提供开户绑定信用卡免费试用一年,现在我就在用。

  • go函数返回数组

    Defining a function that returns a slice of variable size in golang

    First of all, slices are already of “variable size”: [100]int and […]int are array type definitions.
    []int is the correct syntax for a slice, and you could implement the function as:

    func BuildSlice(size int) []int {
        return make([]int, size)
    }
    The built-in function make takes a type T, which must be a slice, map or channel type, optionally followed by a type-specific list of expressions. It returns a value of type T (not *T). The memory is initialized as described in the section on initial values.
    
    Call             Type T     Result
    
    make(T, n)       slice      slice of type T with length n and capacity n
    make(T, n, m)    slice      slice of type T with length n and capacity m
    
    The size arguments n and m must be of integer type or untyped. A constant size argument must be non-negative and representable by a value of type int. If both n and m are provided and are constant, then n must be no larger than m. If n is negative or larger than m at run time, a run-time panic occurs.
    
    s := make([]int, 10, 100)       // slice with len(s) == 10, cap(s) == 100
    s := make([]int, 1e3)           // slice with len(s) == cap(s) == 1000
    s := make([]int, 1<<63) // illegal: len(s) is not representable by a value of type int s := make([]int, 10, 0) // illegal: len(s) > cap(s)
    
    func obtainNext(str string) []int {
       sLen := len(str)
       next := make([]int, sLen)
       next[0] = -1
       k, j := -1, 0
       for j < sLen-1 {
          if k == -1 || str[j] == str[k] {
             k++
             j++
             next[j] = k
          } else {
             k = next[k]
          }
       }
       return next
    }
  • 闭包

    以前看js的时候,老是说,闭包闭包,但是一直搞不清楚,感觉就像个函数。最近看go,看到这篇文章,感觉有点明了。
    Go by Example 中文:闭包
    在我看来,闭包就是匿名函数使用了外部变量。

    在wiki里的描述:
    在计算机科学中,闭包(英语:Closure),又称词法闭包(Lexical Closure)或函数闭包(function closures),是引用了自由变量的函数。这个被引用的自由变量将和这个函数一同存在,即使已经离开了创造它的环境也不例外。
    在没有闭包的语言中,变量的生命周期只限于创建它的环境。但在有闭包的语言中,只要有一个闭包引用了这个变量,它就会一直存在。清理不被任何函数引用的变量的工作通常由垃圾回收完成

    package main
    
    import "fmt"
    
    func intSeq() func() int {
       i := 0
       return func() int {
          i = i + 1
          return i
       }
    }
    
    func main() {
       nextInt := intSeq();
       fmt.Println(nextInt())
       fmt.Println(nextInt())
       fmt.Println(nextInt())
       fmt.Println(nextInt())
       fmt.Println(nextInt())
    
       fmt.Println("-------")
       nextInt2 := intSeq();
       fmt.Println(nextInt2())
    
       fmt.Println("-------")
    }
  • 堆排序

    关键:k[i]>=k[2i]&&k[i]>=k[2*i+1] 父节点大于子节点这是大根堆。

    堆可以被看成是一棵树,结点在堆中的高度可以被定义为从本结点到叶子结点的最长简单下降路径上边的数目;定义堆的高度为树根的高度。我们将看到,堆结构上的一些基本操作的运行时间至多是与树的高度成正比,为O(lgn)

    利用堆排序,分两步:

    1构建堆

    2获取堆顶

    package main
    
    import (
       "fmt"
    )
    
    func main() {
       arr := []int{0, 1, 2, 3, 4, 5, 6, 7}
    
       for length := len(arr); length > 0; length-- {
          buildBigHeap(arr, length)
          fmt.Print(arr)
          change(&arr[0], &arr[length-1])
          fmt.Print("***")
          fmt.Print(arr)
          fmt.Println("-----------")
       }
    
    }
    
    /**
    建立大顶堆
     */
    func buildBigHeap(arr []int, len int) {
       //for parent node
       //k[i]>=k[2i]&&k[i]>=k[2*i+1]
    
       count := 0
       for to := len - 1; to > 0; to-- {
          node := to
          for parent := obtainParent(node); node > 0; node, parent = parent, obtainParent(node) {
             if arr[parent] < arr[node] {
                change(&arr[parent], &arr[node])
                count++
             }
          }
       }
       fmt.Println(count)
    }
    
    func change(a *int, b *int) {
       temp := *a;
       *a = *b
       *b = temp
    }
    
    /**
    获取父节点下标
     */
    func obtainParent(child int) int {
       //数组下标从1开始
       //parent=node/2
       //从0开始
       //parent+1=(node+1)/2==>parent=(node+1)/2-1
    
       return (child+1)/2 - 1
    }
  • 搭建WordPress博客

    环境:

    os:

    lsb_release -a
    LSB Version: :core-4.1-amd64:core-4.1-noarch
    Distributor ID: CentOS
    Description: CentOS Linux release 7.4.1708 (Core)
    Release: 7.4.1708
    Codename: Core

    查看是否安装有php

    yum list |grep php

    查看php版本

    php -v

    卸载旧版php

    yum remove php

    yum remove php*

    安装php7

    安装相关rpm
    rpm -Uvh http://mirror.centos.org/centos/7/extras/x86_64/Packages/epel-release-7-9.noarch.rpm
    rpm -Uvh http://repo.webtatic.com/yum/el7/x86_64/RPMS/webtatic-release-7-3.noarch.rpm
    rpm -Uvh http://mirror.centos.org/centos/7/os/x86_64/Packages/centos-release-7-4.1708.el7.centos.x86_64.rpm

    (RPM 可以到 https://pkgs.org/ 查找)

    我安装的模块:

    yum install php70w
    yum install php70w-common
    yum install php70w-fpm
    yum install php70w-pdo
    yum install php70w-xml
    yum install php70w-mbstring

    yum install php70w-mysql

    安装nginx

    yum install nginx

    账号统一使用nginx

    配置nginx
    /etc/nginx/conf.d/ssl.afonddream.com.conf
    /etc/nginx/cert #ssl 证书位置
    user nginx;

    http
    #使用sock与php-fpm链接
    # Upstream to abstract backend connection(s) for php
    upstream php {
    server unix:/dev/shm/php-cgi.sock;
    # server 127.0.0.1:9000;
    }

    server {
    listen 443;
    server_name afonddream.com www.afonddream.com

    ##nginx日志不转化成ascii编码
    log_escape_non_ascii off;

    ssl on;

    ssl_certificate cert/www.afonddream.com.pem;
    ssl_certificate_key cert/www.afonddream.com.key;

    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    root /www/wordpress-blog;
    ## This should be in your http block and if it is, it’s not needed here.
    index index.php;

    location = /favicon.ico {
    log_not_found off;
    access_log off;
    }

    location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
    }

    location / {
    # This is cool because no php is touched for static content.
    # include the “?$args” part so non-default permalinks doesn’t break when using query string
    try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
    #NOTE: You should have “cgi.fix_pathinfo = 0;” in php.ini
    include fastcgi.conf;
    fastcgi_intercept_errors on;
    fastcgi_pass php;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
    }
    }

    配置php-fpm
    /etc/php-fpm.d/www.conf
    ; RPM: apache Choosed to be able to access some dir as httpd
    user = nginx
    ; RPM: Keep a group allowed to write in log dir.
    group = nginx

    ;use socket instead of tcp
    ;listen = 127.0.0.1:9000
    listen = /dev/shm/php-cgi.sock

    listen.owner = nginx
    listen.group = nginx

    修改nginx 默认上传大小,默认是1M:
    http{} 添加 client_max_body_size 20m 代表最大限制20M

    修改php默认的文件上传大小,默认是是2M
    ; Maximum size of POST data that PHP will accept.
    post_max_size = 20M

    ; Maximum allowed size for uploaded files.
    ; http://php.net/upload-max-filesize
    upload_max_filesize = 20M

    wordpress:5分钟安装教程

    直接在服务器上修改wp-config.php ,填上你的数据库信息。确保防火墙和阿里云安全组已经开放相应的端口。http 80 https 443 .

    然后浏览器访问:wp-admin/install.php 以便启动安装程序.

    注意:
    1使用相同的账号,避免权限问题,这里都是使用nginx

    2低版本的php-pfm,可能不支持socket形式,与nginx连接